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 967cda7

Browse filesBrowse files
committed
add feature to allow specifying language on import snippet markdown helper
1 parent 0d56a99 commit 967cda7
Copy full SHA for 967cda7
Expand file treeCollapse file tree

17 files changed

+222
-95
lines changed

‎package.json

Copy file name to clipboardExpand all lines: package.json
+13-10Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@
3232
]
3333
},
3434
"devDependencies": {
35+
"@types/escape-html": "^0.0.20",
36+
"@types/fs-extra": "^5.0.4",
37+
"@types/globby": "^8.0.0",
38+
"@types/hash-sum": "^1.0.0",
39+
"@types/lru-cache": "^4.1.1",
40+
"@types/node": "^10.12.12",
41+
"@types/semver": "^5.5.0",
42+
"@vue/test-utils": "^1.0.0-beta.29",
3543
"conventional-changelog-cli": "^1.3.22",
3644
"eslint": "^4.19.1",
3745
"eslint-plugin-jest": "^21.15.1",
3846
"eslint-plugin-vue-libs": "^3.0.0",
47+
"inquirer": "^6.2.0",
3948
"lerna": "^3.6.0",
4049
"lint-staged": "^7.0.4",
4150
"minimist": "^1.2.0",
42-
"yorkie": "^1.0.3",
43-
"inquirer": "^6.2.0",
44-
"typescript": "^3.2.2",
45-
"@types/node": "^10.12.12",
46-
"@types/lru-cache": "^4.1.1",
47-
"@types/fs-extra": "^5.0.4",
48-
"@types/semver": "^5.5.0",
49-
"@types/hash-sum": "^1.0.0",
50-
"@types/globby": "^8.0.0",
51-
"@types/escape-html": "^0.0.20"
51+
"ts-jest": "^24.0.0",
52+
"ts-lint": "^4.5.1",
53+
"typescript": "^3.3.3",
54+
"yorkie": "^1.0.3"
5255
}
5356
}
+15-14Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`containers danger 1`] = `
4-
<div class="danger custom-block">
5-
<p class="custom-block-title">WARNING</p>
6-
<p>I am a danger</p>
4+
"<div class=\\"danger custom-block\\"><p class=\\"custom-block-title\\">WARNING</p>
5+
<p>I am a danger</p>
76
</div>
7+
"
88
`;
99
1010
exports[`containers tip 1`] = `
11-
<div class="tip custom-block">
12-
<p class="custom-block-title">TIP</p>
13-
<p>I am a tip</p>
11+
"<div class=\\"tip custom-block\\"><p class=\\"custom-block-title\\">TIP</p>
12+
<p>I am a tip</p>
1413
</div>
14+
"
1515
`;
1616
1717
exports[`containers tip-override 1`] = `
18-
<div class="tip custom-block">
19-
<p class="custom-block-title">提示</p>
20-
<p>I am a tip</p>
18+
"<div class=\\"tip custom-block\\"><p class=\\"custom-block-title\\">提示</p>
19+
<p>I am a tip</p>
2120
</div>
21+
"
2222
`;
2323
2424
exports[`containers v-pre 1`] = `
25-
<div v-pre>
26-
<p>I am a v-pre</p>
25+
"<div v-pre>
26+
<p>I am a v-pre</p>
2727
</div>
28+
"
2829
`;
2930

3031
exports[`containers warning 1`] = `
31-
<div class="warning custom-block">
32-
<p class="custom-block-title">WARNING</p>
33-
<p>I am a warning</p>
32+
"<div class=\\"warning custom-block\\"><p class=\\"custom-block-title\\">WARNING</p>
33+
<p>I am a warning</p>
3434
</div>
35+
"
3536
`;
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`highlight should highlight code 1`] = `
4-
<pre v-pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
4+
"<pre v-pre class=\\"language-js\\"><code><span class=\\"token keyword\\">new</span> <span class=\\"token class-name\\">Vue</span><span class=\\"token punctuation\\">(</span><span class=\\"token punctuation\\">)</span>
55
</code></pre>
6+
"
67
`;
+9-13Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`highlightLines highlight multiple lines 1`] = `
4-
<div class="highlight-lines">
5-
<div class="highlighted">&nbsp;</div>
6-
<div class="highlighted">&nbsp;</div>
7-
<br>
8-
<div class="highlighted">&nbsp;</div>
9-
<div class="highlighted">&nbsp;</div>
10-
<br>
11-
<br>
12-
</div>const app = new Vue({ render, router }) app.$mount('#app')
4+
"<div class=\\"highlight-lines\\"><div class=\\"highlighted\\">&nbsp;</div><div class=\\"highlighted\\">&nbsp;</div><br><div class=\\"highlighted\\">&nbsp;</div><div class=\\"highlighted\\">&nbsp;</div><br><br></div>const app = new Vue({
5+
render,
6+
router
7+
})
8+
9+
app.$mount('#app')
10+
"
1311
`;
1412
1513
exports[`highlightLines highlight single line 1`] = `
16-
<div class="highlight-lines">
17-
<div class="highlighted">&nbsp;</div>
18-
<br>
19-
</div>new Vue()
14+
"<div class=\\"highlight-lines\\"><div class=\\"highlighted\\">&nbsp;</div><br></div>new Vue()
15+
"
2016
`;
+11-6Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`hoist Should keep script and style when not using hoist 1`] = `
4-
<h1>H1</h1>
5-
<script src="vue.js"></script>
4+
"<h1>H1</h1>
5+
<script src=\\"vue.js\\"></script>
66
<style>
77
.vue {
88
font-size: 16px;
99
}
1010
</style>
1111
<h2>H2</h2>
12+
"
1213
`;
1314
1415
exports[`hoist Should miss script and style when using hoist 1`] = `
15-
<h1>H1</h1>
16+
"<h1>H1</h1>
1617
<h2>H2</h2>
18+
"
1719
`;
1820
1921
exports[`hoist Should miss script and style when using hoist 2`] = `
2022
Object {
23+
"__data_block": Object {},
2124
"hoistedTags": Array [
22-
<script src="vue.js"></script>,
23-
<style>
25+
"<script src=\\"vue.js\\"></script>
26+
",
27+
"<style>
2428
.vue {
2529
font-size: 16px;
2630
}
27-
</style>,
31+
</style>
32+
",
2833
],
2934
}
3035
`;
+4-18Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`lineNumbers should lineNumbers work with highlightLines 1`] = `
4-
<!--beforebegin-->
5-
<div class="language-js line-numbers-mode">
6-
<!--afterbegin-->
7-
<div class="highlight-lines">
8-
<div class="highlighted">&nbsp;</div>
9-
<br>
10-
</div>new Vue()
11-
<div class="line-numbers-wrapper"></div>
12-
<!--beforeend-->
13-
</div>
14-
<!--afterend-->
4+
"<!--beforebegin--><div class=\\"language-js line-numbers-mode\\"><!--afterbegin--><div class=\\"highlight-lines\\"><div class=\\"highlighted\\">&nbsp;</div><br></div>new Vue()
5+
<div class=\\"line-numbers-wrapper\\"></div><!--beforeend--></div><!--afterend-->"
156
`;
167
178
exports[`lineNumbers should render lineNumbers 1`] = `
18-
<!--beforebegin-->
19-
<div class="language-js line-numbers-mode">
20-
<!--afterbegin--><pre><code class="language-js">new Vue()
9+
"<!--beforebegin--><div class=\\"language-js line-numbers-mode\\"><!--afterbegin--><pre><code class=\\"language-js\\">new Vue()
2110
</code></pre>
22-
<div class="line-numbers-wrapper"></div>
23-
<!--beforeend-->
24-
</div>
25-
<!--afterend-->
11+
<div class=\\"line-numbers-wrapper\\"></div><!--beforeend--></div><!--afterend-->"
2612
`;
+6-15Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`link should render external links correctly 1`] = `
4-
<p>
5-
<a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer">vue
6-
<OutboundLink/>
7-
</a>
8-
</p>
4+
"<p><a href=\\"https://vuejs.org/\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">vue<OutboundLink/></a></p>
5+
"
96
`;
107
118
exports[`link should render external links correctly 2`] = `
12-
<p>
13-
<a href="http://vuejs.org/" target="_blank" rel="noopener noreferrer">vue
14-
<OutboundLink/>
15-
</a>
16-
</p>
9+
"<p><a href=\\"http://vuejs.org/\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">vue<OutboundLink/></a></p>
10+
"
1711
`;
1812
1913
exports[`link should render external links correctly 3`] = `
20-
<p>
21-
<a href="https://google.com" target="_blank" rel="noopener noreferrer">some <strong>link</strong> with <code>code</code>
22-
<OutboundLink/>
23-
</a>
24-
</p>
14+
"<p><a href=\\"https://google.com\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">some <strong>link</strong> with <code>code</code><OutboundLink/></a></p>
15+
"
2516
`;

‎packages/@vuepress/markdown/__tests__/__snapshots__/snippet.spec.js.snap

Copy file name to clipboardExpand all lines: packages/@vuepress/markdown/__tests__/__snapshots__/snippet.spec.js.snap
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ exports[`snippet import snippet 1`] = `
77
</code></pre>
88
`;
99

10+
exports[`snippet import snippet with highlight lines and language specified as ruby 1`] = `
11+
<div class="highlight-lines">
12+
<div class="highlighted">&nbsp;</div>
13+
<br>
14+
<div class="highlighted">&nbsp;</div>
15+
<br>
16+
</div>def vuepress puts 'vuepress' end
17+
`;
18+
1019
exports[`snippet import snippet with highlight multiple lines 1`] = `
1120
<div class="highlight-lines">
1221
<div class="highlighted">&nbsp;</div>
@@ -24,3 +33,10 @@ exports[`snippet import snippet with highlight single line 1`] = `
2433
<br>
2534
</div>export default function () { // .. }
2635
`;
36+
37+
exports[`snippet import snippet with language specified as ruby 1`] = `
38+
<pre><code class="language-ruby">def vuepress
39+
puts 'vuepress'
40+
end
41+
</code></pre>
42+
`;
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<<< @/packages/@vuepress/core/__test__/markdown/fragments/snippet.js{1-3}
1+
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.js{1-3}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.rb{1,3} lang=ruby
2+
3+
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<<< @/packages/@vuepress/core/__test__/markdown/fragments/snippet.js{1,3}
1+
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.js{1,3}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.rb lang=ruby
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<<< @/packages/@vuepress/core/__test__/markdown/fragments/snippet.js
1+
<<< @/packages/@vuepress/markdown/__tests__/fragments/snippet.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def vuepress
2+
puts 'vuepress'
3+
end

‎packages/@vuepress/markdown/__tests__/snippet.spec.js

Copy file name to clipboardExpand all lines: packages/@vuepress/markdown/__tests__/snippet.spec.js
+22-2Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { Md, getFragment } from './util'
1+
import {
2+
Md,
3+
getFragment
4+
} from './util'
25
import snippet from '../lib/snippet.js'
36
import highlightLines from '../lib/highlightLines.js'
47

58
const md = Md().use(snippet)
6-
const mdH = Md().use(snippet).use(highlightLines)
9+
const mdH = Md()
10+
.use(highlightLines)
11+
.use(snippet)
712

813
describe('snippet', () => {
914
test('import snippet', async () => {
@@ -23,4 +28,19 @@ describe('snippet', () => {
2328
const output = mdH.render(input)
2429
expect(output).toMatchSnapshot()
2530
})
31+
32+
test('import snippet with language specified as ruby', async () => {
33+
const input = await getFragment('code-snippet-specify-language-ruby')
34+
35+
const output = md.render(input)
36+
expect(output).toMatchSnapshot()
37+
expect(output).toMatch(/ruby/)
38+
})
39+
40+
test('import snippet with highlight lines and language specified as ruby', async () => {
41+
const input = await getFragment('code-snippet-highlightLines-single-specify-language-ruby')
42+
43+
const output = mdH.render(input)
44+
expect(output).toMatchSnapshot()
45+
})
2646
})

‎packages/@vuepress/markdown/lib/snippet.js

Copy file name to clipboardExpand all lines: packages/@vuepress/markdown/lib/snippet.js
+20-6Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ const { fs } = require('@vuepress/shared-utils')
22

33
module.exports = function snippet (md, options = {}) {
44
const root = options.root || process.cwd()
5+
56
function parser (state, startLine, endLine, silent) {
6-
const CH = '<'.charCodeAt(0)
77
const pos = state.bMarks[startLine] + state.tShift[startLine]
88
const max = state.eMarks[startLine]
99

10-
// if it's indented more than 3 spaces, it should be a code block
10+
// if it's indented more than 3 spaces,
11+
// it should be a code block
1112
if (state.sCount[startLine] - state.blkIndent >= 4) {
1213
return false
1314
}
1415

15-
for (let i = 0; i < 3; ++i) {
16-
const ch = state.src.charCodeAt(pos + i)
17-
if (ch !== CH || pos + i >= max) return false
16+
if (!/^<<</.test(state.src)) {
17+
return false
1818
}
1919

2020
if (silent) {
@@ -28,10 +28,24 @@ module.exports = function snippet (md, options = {}) {
2828
const content = fs.existsSync(filename) ? fs.readFileSync(filename).toString() : 'Not found: ' + filename
2929
const meta = rawPath.replace(filename, '')
3030

31+
const langExtWithMeta = () => {
32+
if (meta && /lang/.test(meta)) {
33+
const langSet = meta.match(/lang=\w+/)[0]
34+
if (langSet) {
35+
const lang = langSet.split('=')[1]
36+
const extractedMeta = meta.replace(langSet, '')
37+
38+
return lang + extractedMeta
39+
}
40+
}
41+
42+
return filename.split('.').pop() + meta
43+
}
44+
3145
state.line = startLine + 1
3246

3347
const token = state.push('fence', 'code', 0)
34-
token.info = filename.split('.').pop() + meta
48+
token.info = langExtWithMeta()
3549
token.content = content
3650
token.markup = '```'
3751
token.map = [startLine, startLine + 1]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.