diff --git a/package-lock.json b/package-lock.json index a7d21e21d..a36bef670 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4517,6 +4517,14 @@ "pinkie-promise": "^2.0.0" } }, + "fix-indents": { + "version": "0.1.2092", + "resolved": "https://registry.npmjs.org/fix-indents/-/fix-indents-0.1.2092.tgz", + "integrity": "sha1-IqgWVu7EPOPIAxKWh+CmODWGeMs=", + "requires": { + "prelude-ls": "^1.1.2" + } + }, "flat": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz", diff --git a/package.json b/package.json index 19c126a09..c6afec482 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "docsearch.js": "^2.6.3", "eslint": "^5.16.0", "expose-loader": "^0.7.5", + "fix-indents": "^0.1.2092", "indent-string": "^4.0.0", "js-cookie": "^2.2.0", "lunr": "^2.3.6", diff --git a/src/.vuepress/markdown/code-snippet.js b/src/.vuepress/markdown/code-snippet.js index d5f8a4b21..6e10d1059 100644 --- a/src/.vuepress/markdown/code-snippet.js +++ b/src/.vuepress/markdown/code-snippet.js @@ -1,4 +1,5 @@ const { fs, path } = require('@vuepress/shared-utils'); +const fixIndents = require('fix-indents'); module.exports = function snippet(md, options = {}) { const root = options.root || process.cwd(); @@ -47,12 +48,14 @@ module.exports = function snippet(md, options = {}) { rawPath = rawPath.replace(snippetId, ''); } + // Extract line highligts (if present) let highlights = rawPath.match(/{.*}/); if (highlights && highlights[0]) { highlights = highlights[0]; rawPath = rawPath.replace(highlights, ''); } + // Extract language highlighting (if present) let language = rawPath.match(/\[([a-z]*)\]/); if (language && language[0]) { rawPath = rawPath.replace(language[0], ''); @@ -84,6 +87,11 @@ module.exports = function snippet(md, options = {}) { content = content.replace(/.*snippet:start(:\d+)?.*\n/g, ''); content = content.replace(/.*snippet:end.*\n/g, ''); + // Fix indentation in code content + content = fixIndents(content, { + countSpaces: 2 + }); + // Extract meta (line highlight) const fileExtension = filename.split('.').pop(); const meta = `${language ? language : fileExtension}${