Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b73bdc5

Browse filesBrowse files
committed
test: Code snippet & highlighted code snippets
1 parent 4dadbee commit b73bdc5
Copy full SHA for b73bdc5

File tree

7 files changed

+60
-5
lines changed
Filter options

7 files changed

+60
-5
lines changed

‎lib/markdown/snippet.js

Copy file name to clipboardExpand all lines: lib/markdown/snippet.js
+5-5Lines changed: 5 additions & 5 deletions
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
}
+23Lines changed: 23 additions & 0 deletions
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+
`;
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/test/markdown/fragments/snippet.js{1-3}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/test/markdown/fragments/snippet.js{1,3}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/test/markdown/fragments/snippet.js

‎test/markdown/fragments/snippet.js

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function () {
2+
// ..
3+
}

‎test/markdown/snippet.spec.js

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.