-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
48 lines (43 loc) · 1.18 KB
/
index.js
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
44
45
46
47
48
const container = require('markdown-it-container')
function call (target, ...args) {
if (typeof target === 'function') {
return target(...args)
} else {
return target
}
}
module.exports = (options, context) => ({
multiple: true,
extendMarkdown (md) {
const {
validate,
marker,
before,
after,
type = '',
defaultTitle = type.toUpperCase()
} = options
if (!type) return
let { render } = options
if (!render) {
if (before !== undefined && after !== undefined) {
render = (tokens, index) => {
const token = tokens[index]
return token.nesting === 1 ? call(before, token) : call(after, token)
}
} else {
render = (tokens, index) => {
const token = tokens[index]
let title = token.info.trim().slice(type.length).trim() || defaultTitle
if (title) title = `<p class="custom-block-title">${title}</p>`
if (token.nesting === 1) {
return `<div class="${type} custom-block">${title}\n`
} else {
return `</div>\n`
}
}
}
}
md.use(container, type, { render, validate, marker })
}
})