Skip to content

Commit d0f2e42

Browse files
authoredJun 30, 2020
fix($markdown): line highlighting not working correctly when importing code snippets (#2441)
1 parent 924281e commit d0f2e42

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed
 

‎packages/@vuepress/markdown/__tests__/__snapshots__/snippet.spec.js.snap

+46-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`snippet import snippets when the file has a space in the file path 1`] = `
3+
exports[`snippet import snippet 1`] = `
4+
<pre><code class="language-js">export default function () {
5+
// ..
6+
}
7+
</code></pre>
8+
`;
9+
10+
exports[`snippet import snippet with highlight multiple lines 1`] = `
411
<div class="highlight-lines">
512
<div class="highlighted">&nbsp;</div>
613
<div class="highlighted">&nbsp;</div>
@@ -10,15 +17,34 @@ exports[`snippet import snippets when the file has a space in the file path 1`]
1017
}
1118
`;
1219
13-
exports[`snippet import snippet 1`] = `
14-
<pre><code class="language-js">export default function () {
15-
// ..
20+
exports[`snippet import snippet with highlight single and multiple lines 1`] = `
21+
<div class="highlight-lines">
22+
<div class="highlighted">&nbsp;</div>
23+
<div class="highlighted">&nbsp;</div>
24+
<div class="highlighted">&nbsp;</div><br>
25+
</div>export default function () {
26+
// ..
1627
}
17-
</code></pre>
1828
`;
1929
20-
exports[`snippet import snippet with region and highlight 1`] = `
21-
<pre><code class="language-js{1,3}">function foo () {
30+
exports[`snippet import snippet with highlight single line 1`] = `
31+
<div class="highlight-lines">
32+
<div class="highlighted">&nbsp;</div><br>
33+
<div class="highlighted">&nbsp;</div><br>
34+
</div>export default function () {
35+
// ..
36+
}
37+
`;
38+
39+
exports[`snippet import snippet with indented region 1`] = `
40+
<pre><code class="language-html">&lt;section&gt;
41+
&lt;h1&gt;Hello World&lt;/h1&gt;
42+
&lt;/section&gt;
43+
&lt;div&gt;Lorem Ipsum&lt;/div&gt;</code></pre>
44+
`;
45+
46+
exports[`snippet import snippet with region 1`] = `
47+
<pre><code class="language-js">function foo () {
2248
return ({
2349
dest: '../../vuepress',
2450
locales: {
@@ -48,34 +74,8 @@ exports[`snippet import snippet with region and highlight 1`] = `
4874
}</code></pre>
4975
`;
5076
51-
exports[`snippet import snippet with highlight multiple lines 1`] = `
52-
<div class="highlight-lines">
53-
<div class="highlighted">&nbsp;</div>
54-
<div class="highlighted">&nbsp;</div>
55-
<div class="highlighted">&nbsp;</div><br>
56-
</div>export default function () {
57-
// ..
58-
}
59-
`;
60-
61-
exports[`snippet import snippet with highlight single line 1`] = `
62-
<div class="highlight-lines">
63-
<div class="highlighted">&nbsp;</div><br>
64-
<div class="highlighted">&nbsp;</div><br>
65-
</div>export default function () {
66-
// ..
67-
}
68-
`;
69-
70-
exports[`snippet import snippet with indented region 1`] = `
71-
<pre><code class="language-html">&lt;section&gt;
72-
&lt;h1&gt;Hello World&lt;/h1&gt;
73-
&lt;/section&gt;
74-
&lt;div&gt;Lorem Ipsum&lt;/div&gt;</code></pre>
75-
`;
76-
77-
exports[`snippet import snippet with region 1`] = `
78-
<pre><code class="language-js">function foo () {
77+
exports[`snippet import snippet with region and highlight 1`] = `
78+
<pre><code class="language-js{1,3}">function foo () {
7979
return ({
8080
dest: '../../vuepress',
8181
locales: {
@@ -105,7 +105,7 @@ exports[`snippet import snippet with region 1`] = `
105105
}</code></pre>
106106
`;
107107
108-
exports[`snippet import snippet with region and single line highlight > 10 1`] = `
108+
exports[`snippet import snippet with region and single line highlight > 10 1`] = `
109109
<pre><code class="language-js{11}">function foo () {
110110
return ({
111111
dest: '../../vuepress',
@@ -135,3 +135,13 @@ exports[`snippet import snippet with region and single line highlight > 10 1`]
135135
})
136136
}</code></pre>
137137
`;
138+
139+
exports[`snippet import snippets when the file has a space in the file path 1`] = `
140+
<div class="highlight-lines">
141+
<div class="highlighted">&nbsp;</div>
142+
<div class="highlighted">&nbsp;</div>
143+
<div class="highlighted">&nbsp;</div><br>
144+
</div>export default function () {
145+
// ..
146+
}
147+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.js{1-2,3}

‎packages/@vuepress/markdown/__tests__/snippet.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ describe('snippet', () => {
2525
expect(output).toMatchSnapshot()
2626
})
2727

28+
test('import snippet with highlight single and multiple lines', () => {
29+
const input = getFragment(__dirname, 'code-snippet-highlightLines-single-and-multiple.md')
30+
const output = mdH.render(input)
31+
expect(output).toMatchSnapshot()
32+
})
33+
2834
test('import snippets when the file has a space in the file path', () => {
2935
const input = getFragment(__dirname, 'code-snippet-with-space-in-path.md')
3036
const output = mdH.render(input)

‎packages/@vuepress/markdown/lib/snippet.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module.exports = function snippet (md, options = {}) {
137137
*
138138
* captures: ['/path/to/file.extension', 'extension', '#region', '{meta}']
139139
*/
140-
const rawPathRegexp = /^(.+(?:\.([a-z]+)))(?:(#[\w-]+))?(?: ?({\d+(?:[,-]\d+)?}))?$/
140+
const rawPathRegexp = /^(.+(?:\.([a-z]+)))(?:(#[\w-]+))?(?: ?({\d+(?:[,-]\d+)*}))?$/
141141

142142
const rawPath = state.src.slice(start, end).trim().replace(/^@/, root).trim()
143143
const [filename = '', extension = '', region = '', meta = ''] = (rawPathRegexp.exec(rawPath) || []).slice(1)

0 commit comments

Comments
 (0)
Please sign in to comment.