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): support path without file extension when importing code snippets #2677

Merged
merged 2 commits into from
Feb 18, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ exports[`snippet import snippet 1`] = `
// ..
}
</code></pre>
<pre><code># Sample snippet file with no filename extension.
FROM ubuntu:latest
CMD echo hello!
</code></pre>
`;

exports[`snippet import snippet with highlight multiple lines 1`] = `
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/markdown/__tests__/fragments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sample snippet file with no filename extension.
FROM ubuntu:latest
CMD echo hello!
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep it, it would make sure import path with file extension works fine


<<< @/packages/@vuepress/markdown/__tests__/fragments/Dockerfile
2 changes: 1 addition & 1 deletion packages/@vuepress/markdown/lib/snippet.js
Original file line number Diff line number Diff line change
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