Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix($markdown): Fix four spaces codeblocks rendering (Closes #1921) #1958

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}