Skip to content

Commit 5af283d

Browse files
committed
feat: insert code block, closes #13
1 parent aadf01b commit 5af283d

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,27 @@ const component = vmark(input)
5858
// Get the component in SFC format
5959
```
6060

61+
### Insert code block
6162

63+
Automatically transform:
64+
65+
````markdown
66+
```vue {insert: 'below'}
67+
<button>Button</button>
68+
```
69+
````
70+
71+
Into:
72+
73+
````markdown
74+
```vue
75+
<button>Button</button>
76+
```
77+
78+
<button>Button</button>
79+
````
80+
81+
`insert` can also be `above`.
6282

6383
## API
6484

lib/markdown/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ module.exports = (options, extend) => {
1111
extend(md)
1212
}
1313

14+
// Load this plugin at the end in case other plugins change `md.rules.fence`
15+
md.use(require('./insert-code-plugin'))
16+
1417
return md
1518
}

lib/markdown/insert-code-plugin.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = md => {
2+
const RE = /\s*{([^}]+)}/
3+
4+
const parseOptions = str => {
5+
if (!RE.test(str)) {
6+
return {}
7+
}
8+
const [, options] = RE.exec(str)
9+
const fn = new Function(`return {${options}}`) // eslint-disable-line no-new-func
10+
return fn()
11+
}
12+
13+
const { fence } = md.renderer.rules
14+
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
15+
const token = tokens[idx]
16+
const info = parseOptions(token.info)
17+
if (!info.insert) {
18+
return self.renderToken(token)
19+
}
20+
const res = fence(tokens, idx, options, env, self)
21+
if (info.insert === 'above') {
22+
return `${token.content}${res}`
23+
}
24+
if (info.insert === 'below') {
25+
return `${res}${token.content}`
26+
}
27+
return res
28+
}
29+
}

test/__snapshots__/index.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ exports[`basic 1`] = `
99
"
1010
`;
1111

12+
exports[`insert code above 1`] = `
13+
"<template>
14+
<div class=\\"vmark\\"><h1>hello</h1>
15+
<button>Button</button>
16+
<pre><code class=\\"language-vue\\">&lt;button&gt;Button&lt;/button&gt;
17+
</code></pre>
18+
</div>
19+
</template>
20+
21+
"
22+
`;
23+
24+
exports[`insert code below 1`] = `
25+
"<template>
26+
<div class=\\"vmark\\"><h1>hello</h1>
27+
<pre><code class=\\"language-vue\\">&lt;button&gt;Button&lt;/button&gt;
28+
</code></pre>
29+
<button>Button</button>
30+
</div>
31+
</template>
32+
33+
"
34+
`;
35+
1236
exports[`with tags 1`] = `
1337
"<template>
1438
<div class=\\"vmark\\"><div class=\\"foo\\">{{ foo }}</div>

test/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,19 @@ snapshot('wrap html', `
2727
`, {
2828
wrapHTML: html => `<foo>${html}</foo>`
2929
})
30+
31+
snapshot('insert code above', `
32+
# hello
33+
34+
\`\`\`vue {insert: 'above'}
35+
<button>Button</button>
36+
\`\`\`
37+
`)
38+
39+
snapshot('insert code below', `
40+
# hello
41+
42+
\`\`\`vue {insert: 'below'}
43+
<button>Button</button>
44+
\`\`\`
45+
`)

0 commit comments

Comments
 (0)