|
1 | 1 | const { fs, path } = require('@vuepress/shared-utils'); |
| 2 | +const fixIndents = require('fix-indents'); |
2 | 3 |
|
3 | 4 | module.exports = function snippet(md, options = {}) { |
4 | 5 | const root = options.root || process.cwd(); |
@@ -47,12 +48,14 @@ module.exports = function snippet(md, options = {}) { |
47 | 48 | rawPath = rawPath.replace(snippetId, ''); |
48 | 49 | } |
49 | 50 |
|
| 51 | + // Extract line highligts (if present) |
50 | 52 | let highlights = rawPath.match(/{.*}/); |
51 | 53 | if (highlights && highlights[0]) { |
52 | 54 | highlights = highlights[0]; |
53 | 55 | rawPath = rawPath.replace(highlights, ''); |
54 | 56 | } |
55 | 57 |
|
| 58 | + // Extract language highlighting (if present) |
56 | 59 | let language = rawPath.match(/\[([a-z]*)\]/); |
57 | 60 | if (language && language[0]) { |
58 | 61 | rawPath = rawPath.replace(language[0], ''); |
@@ -84,6 +87,11 @@ module.exports = function snippet(md, options = {}) { |
84 | 87 | content = content.replace(/.*snippet:start(:\d+)?.*\n/g, ''); |
85 | 88 | content = content.replace(/.*snippet:end.*\n/g, ''); |
86 | 89 |
|
| 90 | + // Fix indentation in code content |
| 91 | + content = fixIndents(content, { |
| 92 | + countSpaces: 2 |
| 93 | + }); |
| 94 | + |
87 | 95 | // Extract meta (line highlight) |
88 | 96 | const fileExtension = filename.split('.').pop(); |
89 | 97 | const meta = `${language ? language : fileExtension}${ |
|
0 commit comments