Skip to content

Commit ea4b85b

Browse files
committed
support highlighting lines
1 parent 1d726c5 commit ea4b85b

File tree

8 files changed

+172
-24
lines changed

8 files changed

+172
-24
lines changed

docs/kitchen/sink.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bar: 234
1414
const a = 123
1515
```
1616

17-
``` html
17+
``` html{2,10}
1818
<div id="example">
1919
<p>Original message: "{{ message }}"</p>
2020
<p>Computed reversed message: "{{ reversedMessage }}"</p>
@@ -37,10 +37,6 @@ var vm = new Vue({
3737
</script>
3838
```
3939

40-
## Dynamic Component
41-
42-
<demo-1 vue></demo-1>
43-
4440
## Interpolation
4541

4642
- Foo is {{ $page.frontmatter.foo }}

docs/markdown.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
## Custom Containers
1212

1313
## Configuration
14+
15+
## Syntax Highlighting
16+
17+
Highlight lines

lib/default-theme/Layout.vue

+1-17
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,4 @@ export default {
3535
</script>
3636

3737
<style src="prismjs/themes/prism-tomorrow.css"></style>
38-
<style src="./nprogress.css"></style>
39-
<style>
40-
.theme-container {
41-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
42-
}
43-
44-
pre[class*="language-"] {
45-
line-height: 1.4;
46-
border-radius: 5px;
47-
}
48-
49-
pre[class*="language-"] code {
50-
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace;
51-
font-size: 14px;
52-
-webkit-font-smoothing: antialiased;
53-
}
54-
</style>
38+
<style src="./styles.css"></style>

lib/default-theme/styles.css

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
.theme-container {
2+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
3+
}
4+
5+
pre, pre[class*="language-"] {
6+
background-color: #2d2d2d;
7+
color: white;
8+
line-height: 1.4;
9+
border-radius: 5px;
10+
padding: 1.25rem 1.575rem;
11+
white-space: pre-wrap;
12+
word-break: break-word;
13+
overflow: auto;
14+
}
15+
16+
pre code, pre[class*="language-"] code {
17+
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace;
18+
font-size: 14px;
19+
-webkit-font-smoothing: antialiased;
20+
}
21+
22+
.highlighted-line {
23+
background-color: #14161a;
24+
display: block;
25+
margin: 1px -1.575rem;
26+
padding: 1px 1.575rem;
27+
}
28+
29+
/*
30+
Copied from nprogress since it doens't
31+
allow programmatic configuration of color
32+
*/
33+
34+
/* Make clicks pass-through */
35+
#nprogress {
36+
pointer-events: none;
37+
}
38+
39+
#nprogress .bar {
40+
background: #41b883;
41+
42+
position: fixed;
43+
z-index: 1031;
44+
top: 0;
45+
left: 0;
46+
47+
width: 100%;
48+
height: 2px;
49+
}
50+
51+
/* Fancy blur effect */
52+
#nprogress .peg {
53+
display: block;
54+
position: absolute;
55+
right: 0px;
56+
width: 100px;
57+
height: 100%;
58+
box-shadow: 0 0 10px #41b883, 0 0 5px #41b883;
59+
opacity: 1.0;
60+
61+
-webkit-transform: rotate(3deg) translate(0px, -4px);
62+
-ms-transform: rotate(3deg) translate(0px, -4px);
63+
transform: rotate(3deg) translate(0px, -4px);
64+
}
65+
66+
/* Remove these to get rid of the spinner */
67+
#nprogress .spinner {
68+
display: block;
69+
position: fixed;
70+
z-index: 1031;
71+
top: 15px;
72+
right: 15px;
73+
}
74+
75+
#nprogress .spinner-icon {
76+
width: 18px;
77+
height: 18px;
78+
box-sizing: border-box;
79+
80+
border: solid 2px transparent;
81+
border-top-color: #41b883;
82+
border-left-color: #41b883;
83+
border-radius: 50%;
84+
85+
-webkit-animation: nprogress-spinner 400ms linear infinite;
86+
animation: nprogress-spinner 400ms linear infinite;
87+
}
88+
89+
.nprogress-custom-parent {
90+
overflow: hidden;
91+
position: relative;
92+
}
93+
94+
.nprogress-custom-parent #nprogress .spinner,
95+
.nprogress-custom-parent #nprogress .bar {
96+
position: absolute;
97+
}
98+
99+
@-webkit-keyframes nprogress-spinner {
100+
0% { -webkit-transform: rotate(0deg); }
101+
100% { -webkit-transform: rotate(360deg); }
102+
}
103+
@keyframes nprogress-spinner {
104+
0% { transform: rotate(0deg); }
105+
100% { transform: rotate(360deg); }
106+
}

lib/markdown/highlight.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (str, lang) => {
1414
}
1515
if (prism.languages[lang]) {
1616
const res = prism.highlight(str, prism.languages[lang], lang)
17-
return `<pre class="language-${lang}"><code v-pre>${res}</code></pre>`
17+
return `<pre v-pre class="language-${lang}"><code>${res}</code></pre>`
1818
}
1919
return ''
2020
}

lib/markdown/highlightLines.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// forked from https://github.com/egoist/markdown-it-highlight-lines
2+
3+
const RE = /{([\d,-]+)}/
4+
const wrapperRE = /^<pre .*?><code>/
5+
6+
module.exports = md => {
7+
const fence = md.renderer.rules.fence
8+
md.renderer.rules.fence = (...args) => {
9+
const [tokens, idx, options] = args
10+
const token = tokens[idx]
11+
12+
if (!token.info || !RE.test(token.info)) {
13+
return fence(...args)
14+
}
15+
16+
const langName = token.info.replace(RE, '').trim()
17+
const lineNumbers = RE.exec(token.info)[1]
18+
.split(',')
19+
.map(v => v.split('-').map(v => parseInt(v, 10)))
20+
21+
const code = options.highlight
22+
? options.highlight(token.content, langName)
23+
: token.content
24+
25+
const rawCode = code.replace(wrapperRE, '')
26+
const leadingWrapper = code.match(wrapperRE)[0]
27+
28+
const codeSplits = rawCode.split('\n').map((split, index) => {
29+
const lineNumber = index + 1
30+
const inRange = lineNumbers.some(([start, end]) => {
31+
if (start && end) {
32+
return lineNumber >= start && lineNumber <= end
33+
}
34+
return lineNumber === start
35+
})
36+
if (inRange) {
37+
return {
38+
code: `<span class="highlighted-line">${split}</span>`,
39+
highlighted: true
40+
}
41+
}
42+
return {
43+
code: split
44+
}
45+
})
46+
let highlightedCode = leadingWrapper
47+
codeSplits.forEach(split => {
48+
if (split.highlighted) {
49+
highlightedCode += split.code
50+
} else {
51+
highlightedCode += `${split.code}\n`
52+
}
53+
})
54+
return highlightedCode
55+
}
56+
}

lib/markdown/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const highlight = require('./highlight')
2+
const highlightLines = require('./highlightLines')
23
const convertRouterLink = require('./link')
34
const anchor = require('markdown-it-anchor')
45
const container = require('markdown-it-container')
@@ -15,6 +16,7 @@ module.exports = ({ markdown = {}}) => {
1516
highlight
1617
})
1718
.use(convertRouterLink)
19+
.use(highlightLines)
1820
.use(anchor, Object.assign({ permalink: true, permalinkBefore: true }, markdown.anchor))
1921
.use(toc, Object.assign({ includeLevel: [2, 3] }, markdown.toc))
2022
.use(container, 'tip')

lib/webpack/baseConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function createBaseConfig ({
77
publicPath,
88
themePath,
99
notFoundPath
10-
}, { debug }) {
10+
}, { debug } = {}) {
1111
const markdown = require('../markdown')(siteConfig)
1212
const Config = require('webpack-chain')
1313
const { VueLoaderPlugin } = require('vue-loader')

0 commit comments

Comments
 (0)