Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix($markdown): line highlighting not working correctly when importing code snippets #2441

Merged
merged 2 commits into from Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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