Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix($markdown): line highlighting not working correctly when importin…
…g code snippets (#2441)
  • Loading branch information
diogotcorreia committed Jun 30, 2020
1 parent 924281e commit d0f2e42
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
@@ -1,6 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snippet import snippets when the file has a space in the file path 1`] = `
exports[`snippet import snippet 1`] = `
<pre><code class="language-js">export default function () {
// ..
}
</code></pre>
`;

exports[`snippet import snippet with highlight multiple lines 1`] = `
<div class="highlight-lines">
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div>
Expand All @@ -10,15 +17,34 @@ exports[`snippet import snippets when the file has a space in the file path 1`]
}
`;
exports[`snippet import snippet 1`] = `
<pre><code class="language-js">export default function () {
// ..
exports[`snippet import snippet with highlight single and multiple lines 1`] = `
<div class="highlight-lines">
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div><br>
</div>export default function () {
// ..
}
</code></pre>
`;
exports[`snippet import snippet with region and highlight 1`] = `
<pre><code class="language-js{1,3}">function foo () {
exports[`snippet import snippet with highlight single line 1`] = `
<div class="highlight-lines">
<div class="highlighted">&nbsp;</div><br>
<div class="highlighted">&nbsp;</div><br>
</div>export default function () {
// ..
}
`;
exports[`snippet import snippet with indented region 1`] = `
<pre><code class="language-html">&lt;section&gt;
&lt;h1&gt;Hello World&lt;/h1&gt;
&lt;/section&gt;
&lt;div&gt;Lorem Ipsum&lt;/div&gt;</code></pre>
`;
exports[`snippet import snippet with region 1`] = `
<pre><code class="language-js">function foo () {
return ({
dest: '../../vuepress',
locales: {
Expand Down Expand Up @@ -48,34 +74,8 @@ exports[`snippet import snippet with region and highlight 1`] = `
}</code></pre>
`;
exports[`snippet import snippet with highlight multiple lines 1`] = `
<div class="highlight-lines">
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div><br>
</div>export default function () {
// ..
}
`;
exports[`snippet import snippet with highlight single line 1`] = `
<div class="highlight-lines">
<div class="highlighted">&nbsp;</div><br>
<div class="highlighted">&nbsp;</div><br>
</div>export default function () {
// ..
}
`;
exports[`snippet import snippet with indented region 1`] = `
<pre><code class="language-html">&lt;section&gt;
&lt;h1&gt;Hello World&lt;/h1&gt;
&lt;/section&gt;
&lt;div&gt;Lorem Ipsum&lt;/div&gt;</code></pre>
`;
exports[`snippet import snippet with region 1`] = `
<pre><code class="language-js">function foo () {
exports[`snippet import snippet with region and highlight 1`] = `
<pre><code class="language-js{1,3}">function foo () {
return ({
dest: '../../vuepress',
locales: {
Expand Down Expand Up @@ -105,7 +105,7 @@ exports[`snippet import snippet with region 1`] = `
}</code></pre>
`;
exports[`snippet import snippet with region and single line highlight > 10 1`] = `
exports[`snippet import snippet with region and single line highlight > 10 1`] = `
<pre><code class="language-js{11}">function foo () {
return ({
dest: '../../vuepress',
Expand Down Expand Up @@ -135,3 +135,13 @@ exports[`snippet import snippet with region and single line highlight > 10 1`]
})
}</code></pre>
`;
exports[`snippet import snippets when the file has a space in the file path 1`] = `
<div class="highlight-lines">
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div>
<div class="highlighted">&nbsp;</div><br>
</div>export default function () {
// ..
}
`;
@@ -0,0 +1 @@
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.js{1-2,3}
6 changes: 6 additions & 0 deletions packages/@vuepress/markdown/__tests__/snippet.spec.js
Expand Up @@ -25,6 +25,12 @@ describe('snippet', () => {
expect(output).toMatchSnapshot()
})

test('import snippet with highlight single and multiple lines', () => {
const input = getFragment(__dirname, 'code-snippet-highlightLines-single-and-multiple.md')
const output = mdH.render(input)
expect(output).toMatchSnapshot()
})

test('import snippets when the file has a space in the file path', () => {
const input = getFragment(__dirname, 'code-snippet-with-space-in-path.md')
const output = mdH.render(input)
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/markdown/lib/snippet.js
Expand Up @@ -137,7 +137,7 @@ module.exports = function snippet (md, options = {}) {
*
* captures: ['/path/to/file.extension', 'extension', '#region', '{meta}']
*/
const rawPathRegexp = /^(.+(?:\.([a-z]+)))(?:(#[\w-]+))?(?: ?({\d+(?:[,-]\d+)?}))?$/
const rawPathRegexp = /^(.+(?:\.([a-z]+)))(?:(#[\w-]+))?(?: ?({\d+(?:[,-]\d+)*}))?$/

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

0 comments on commit d0f2e42

Please sign in to comment.