Skip to content

Commit 4f180a0

Browse files
authored
[feature] Fix indent in imported code snippets (#377)
Automatically fix the indentation of imported code snippets (useful when importing partial snippet files).
1 parent a9c2efb commit 4f180a0

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"docsearch.js": "^2.6.3",
3131
"eslint": "^5.16.0",
3232
"expose-loader": "^0.7.5",
33+
"fix-indents": "^0.1.2092",
3334
"indent-string": "^4.0.0",
3435
"js-cookie": "^2.2.0",
3536
"lunr": "^2.3.6",

src/.vuepress/markdown/code-snippet.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { fs, path } = require('@vuepress/shared-utils');
2+
const fixIndents = require('fix-indents');
23

34
module.exports = function snippet(md, options = {}) {
45
const root = options.root || process.cwd();
@@ -47,12 +48,14 @@ module.exports = function snippet(md, options = {}) {
4748
rawPath = rawPath.replace(snippetId, '');
4849
}
4950

51+
// Extract line highligts (if present)
5052
let highlights = rawPath.match(/{.*}/);
5153
if (highlights && highlights[0]) {
5254
highlights = highlights[0];
5355
rawPath = rawPath.replace(highlights, '');
5456
}
5557

58+
// Extract language highlighting (if present)
5659
let language = rawPath.match(/\[([a-z]*)\]/);
5760
if (language && language[0]) {
5861
rawPath = rawPath.replace(language[0], '');
@@ -84,6 +87,11 @@ module.exports = function snippet(md, options = {}) {
8487
content = content.replace(/.*snippet:start(:\d+)?.*\n/g, '');
8588
content = content.replace(/.*snippet:end.*\n/g, '');
8689

90+
// Fix indentation in code content
91+
content = fixIndents(content, {
92+
countSpaces: 2
93+
});
94+
8795
// Extract meta (line highlight)
8896
const fileExtension = filename.split('.').pop();
8997
const meta = `${language ? language : fileExtension}${

0 commit comments

Comments
 (0)