forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd-plugin-heading.js
More file actions
43 lines (33 loc) · 949 Bytes
/
md-plugin-heading.js
File metadata and controls
43 lines (33 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Manages headings
*/
const { slugify } = require('./utils')
const titleRE = /<\/?[^>]+(>|$)/g
function parseContent (str) {
const title = String(str)
.replace(titleRE, '')
.trim()
return {
id: slugify(title),
title
}
}
module.exports = function (md) {
md.renderer.rules.heading_open = (tokens, idx, options, env, self) => {
const token = tokens[idx]
const content = tokens[idx + 1]
.children
.reduce((acc, t) => acc + t.content, '')
const { id, title } = parseContent(content)
token.attrSet('id', id)
token.attrSet('class', `doc-heading doc-${token.tag}`)
token.attrSet('@click', `copyHeading(\`${id}\`)`)
if (token.tag === 'h2') {
md.$data.toc.push(`{id:\`${id}\`,title:\`${title}\`}`)
}
else if (token.tag === 'h3') {
md.$data.toc.push(`{id:\`${id}\`,title:\`${title}\`, sub: true}`)
}
return self.renderToken(tokens, idx, options)
}
}