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 1 commit
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()
})
})
9 changes: 6 additions & 3 deletions packages/@vuepress/markdown/lib/preWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
// 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 = md.renderer.rules.fence
Copy link
Collaborator

@kefranabg kefranabg Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think you could make a small improvement :

const { fence, codeBlock } = md.renderer.rules
md.renderer.rules.fence = wrap(fence)
md.renderer.rules.code_block = wrap(codeBlock)

What do you think @pyaillet ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to rename codeBlock to code_block to do this.
And sadly, the linter doesn't like it:

vuepress/packages/@vuepress/markdown/lib/preWrapper.js
  18:18  error  Identifier 'code_block' is not in camel case  camelcase

✖ 1 problem (1 error, 0 warnings)

Copy link
Collaborator

@kefranabg kefranabg Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure, I missed that 😉
const { fence, code_block: codeBlock } = md.renderer.rules should work

md.renderer.rules.fence = wrap(fence)
const codeBlock = md.renderer.rules.code_block
md.renderer.rules.code_block = wrap(codeBlock)
}