diff --git a/packages/@vuepress/markdown/__tests__/__snapshots__/preWrapper.spec.js.snap b/packages/@vuepress/markdown/__tests__/__snapshots__/preWrapper.spec.js.snap new file mode 100644 index 0000000000..562cc6c467 --- /dev/null +++ b/packages/@vuepress/markdown/__tests__/__snapshots__/preWrapper.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`preWrapper should wrap code with quadruple space 1`] = ` + +
+
new Vue()
+
+ +
+ +`; + +exports[`preWrapper should wrap code with triple back quote 1`] = ` + +
+
new Vue()
+
+ +
+ +`; diff --git a/packages/@vuepress/markdown/__tests__/fragments/code-prewrapper-with-quotes.md b/packages/@vuepress/markdown/__tests__/fragments/code-prewrapper-with-quotes.md new file mode 100644 index 0000000000..af4a84a927 --- /dev/null +++ b/packages/@vuepress/markdown/__tests__/fragments/code-prewrapper-with-quotes.md @@ -0,0 +1,3 @@ +``` js +new Vue() +``` diff --git a/packages/@vuepress/markdown/__tests__/fragments/code-prewrapper-with-spaces.md b/packages/@vuepress/markdown/__tests__/fragments/code-prewrapper-with-spaces.md new file mode 100644 index 0000000000..a679a1721a --- /dev/null +++ b/packages/@vuepress/markdown/__tests__/fragments/code-prewrapper-with-spaces.md @@ -0,0 +1 @@ + new Vue() diff --git a/packages/@vuepress/markdown/__tests__/preWrapper.spec.js b/packages/@vuepress/markdown/__tests__/preWrapper.spec.js new file mode 100644 index 0000000000..9ba0c482a4 --- /dev/null +++ b/packages/@vuepress/markdown/__tests__/preWrapper.spec.js @@ -0,0 +1,24 @@ +import { getFragment } from '@vuepress/test-utils' +import { Md } from './util' +import preWrapper from '../lib/preWrapper.js' + +const md = Md() +const mdP = Md().use(preWrapper) + +describe('preWrapper', () => { + test('should wrap code with triple back quote', () => { + const input = getFragment(__dirname, 'code-prewrapper-with-quotes.md') + const output1 = md.render(input) + const output2 = mdP.render(input) + expect(output1 === output2).toBe(false) + expect(output2).toMatchSnapshot() + }) + + test('should wrap code with quadruple space', () => { + const input = getFragment(__dirname, 'code-prewrapper-with-spaces.md') + const output1 = md.render(input) + const output2 = mdP.render(input) + expect(output1 === output2).toBe(false) + expect(output2).toMatchSnapshot() + }) +}) diff --git a/packages/@vuepress/markdown/lib/preWrapper.js b/packages/@vuepress/markdown/lib/preWrapper.js index 35f7ed7750..e9862939ff 100644 --- a/packages/@vuepress/markdown/lib/preWrapper.js +++ b/packages/@vuepress/markdown/lib/preWrapper.js @@ -8,12 +8,14 @@ // 4. module.exports = md => { - const fence = md.renderer.rules.fence - md.renderer.rules.fence = (...args) => { + const wrap = (wrapped) => (...args) => { const [tokens, idx] = args const token = tokens[idx] - const rawCode = fence(...args) + const rawCode = wrapped(...args) return `
` + `${rawCode}
` } + const { fence, code_block: codeBlock } = md.renderer.rules + md.renderer.rules.fence = wrap(fence) + md.renderer.rules.code_block = wrap(codeBlock) }