Skip to content

Commit b73bdc5

Browse files
committed
test: Code snippet & highlighted code snippets
1 parent 4dadbee commit b73bdc5

File tree

7 files changed

+60
-5
lines changed

7 files changed

+60
-5
lines changed

lib/markdown/snippet.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const fs = require('fs')
22

3-
module.exports = function codeFrame(md, options = {}) {
3+
module.exports = function snippet (md, options = {}) {
44
const root = options.root || process.cwd()
5-
function parser(state, startLine, endLine, silent) {
5+
function parser (state, startLine, endLine, silent) {
66
const CH = '<'.charCodeAt(0)
77
const pos = state.bMarks[startLine] + state.tShift[startLine]
88
const max = state.eMarks[startLine]
@@ -28,9 +28,9 @@ module.exports = function codeFrame(md, options = {}) {
2828
const content = fs.existsSync(filename) ? fs.readFileSync(filename).toString() : 'Not found: ' + filename
2929
const meta = rawPath.replace(filename, '')
3030

31-
state.line = startLine + 1;
31+
state.line = startLine + 1
3232

33-
token = state.push('fence', 'code', 0)
33+
const token = state.push('fence', 'code', 0)
3434
token.info = filename.split('.').pop() + meta
3535
token.content = content
3636
token.markup = '```'
@@ -39,5 +39,5 @@ module.exports = function codeFrame(md, options = {}) {
3939
return true
4040
}
4141

42-
md.block.ruler.before('fence', 'code-frame', parser)
42+
md.block.ruler.before('fence', 'snippet', parser)
4343
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snippet import snippet 1`] = `
4+
<pre><code class="language-js">export default function () {
5+
// ..
6+
}</code></pre>
7+
`;
8+
9+
exports[`snippet import snippet with highlight multiple lines 1`] = `
10+
<div class="highlight-lines">
11+
<div class="highlighted">&nbsp;</div>
12+
<div class="highlighted">&nbsp;</div>
13+
<div class="highlighted">&nbsp;</div>
14+
</div>export default function () { // .. }
15+
`;
16+
17+
exports[`snippet import snippet with highlight single line 1`] = `
18+
<div class="highlight-lines">
19+
<div class="highlighted">&nbsp;</div>
20+
<br>
21+
<div class="highlighted">&nbsp;</div>
22+
</div>export default function () { // .. }
23+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/test/markdown/fragments/snippet.js{1-3}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/test/markdown/fragments/snippet.js{1,3}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/test/markdown/fragments/snippet.js

test/markdown/fragments/snippet.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function () {
2+
// ..
3+
}

test/markdown/snippet.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Md, getFragment } from './util'
2+
import snippet from '@/markdown/snippet.js'
3+
import highlightLines from '@/markdown/highlightLines.js'
4+
5+
const md = Md().use(snippet)
6+
const mdH = Md().use(snippet).use(highlightLines)
7+
8+
describe('snippet', () => {
9+
test('import snippet', async () => {
10+
const input = await getFragment('code-snippet')
11+
const output = md.render(input)
12+
expect(output).toMatchSnapshot()
13+
})
14+
15+
test('import snippet with highlight single line', async () => {
16+
const input = await getFragment('code-snippet-highlightLines-single')
17+
const output = mdH.render(input)
18+
expect(output).toMatchSnapshot()
19+
})
20+
21+
test('import snippet with highlight multiple lines', async () => {
22+
const input = await getFragment('code-snippet-highlightLines-multiple')
23+
const output = mdH.render(input)
24+
expect(output).toMatchSnapshot()
25+
})
26+
})

0 commit comments

Comments
 (0)