Skip to content

Commit

Permalink
fix($markdown): Fix four spaces codeblocks rendering (Closes #1921) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pyaillet authored and kefranabg committed Dec 9, 2019
1 parent bd71e43 commit 7bc5825
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`preWrapper should wrap code with quadruple space 1`] = `
<!--beforebegin-->
<div class="language- extra-class">
<!--afterbegin--><pre><code>new Vue()
</code></pre>
<!--beforeend-->
</div>
<!--afterend-->
`;

exports[`preWrapper should wrap code with triple back quote 1`] = `
<!--beforebegin-->
<div class="language-js extra-class">
<!--afterbegin--><pre><code class="language-js">new Vue()
</code></pre>
<!--beforeend-->
</div>
<!--afterend-->
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``` js
new Vue()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new Vue()
24 changes: 24 additions & 0 deletions packages/@vuepress/markdown/__tests__/preWrapper.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
8 changes: 5 additions & 3 deletions packages/@vuepress/markdown/lib/preWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
// 4. <!--afterend-->

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 `<!--beforebegin--><div class="language-${token.info.trim()} extra-class">`
+ `<!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
}
const { fence, code_block: codeBlock } = md.renderer.rules
md.renderer.rules.fence = wrap(fence)
md.renderer.rules.code_block = wrap(codeBlock)
}

0 comments on commit 7bc5825

Please sign in to comment.