1
1
import MarkdownIt from 'markdown-it'
2
+ import { RenderRule } from 'markdown-it/lib/renderer'
2
3
import Token from 'markdown-it/lib/token'
3
4
import container from 'markdown-it-container'
4
5
5
6
export const containerPlugin = ( md : MarkdownIt ) => {
6
- md . use ( ...createContainer ( 'tip' , 'TIP' ) )
7
- . use ( ...createContainer ( 'info' , 'INFO' ) )
8
- . use ( ...createContainer ( 'warning' , 'WARNING' ) )
9
- . use ( ...createContainer ( 'danger' , 'DANGER' ) )
10
- . use ( ...createContainer ( 'details' , 'Details' ) )
7
+ md . use ( ...createContainer ( 'tip' , 'TIP' , md ) )
8
+ . use ( ...createContainer ( 'info' , 'INFO' , md ) )
9
+ . use ( ...createContainer ( 'warning' , 'WARNING' , md ) )
10
+ . use ( ...createContainer ( 'danger' , 'DANGER' , md ) )
11
+ . use ( ...createContainer ( 'details' , 'Details' , md ) )
11
12
// explicitly escape Vue syntax
12
13
. use ( container , 'v-pre' , {
13
14
render : ( tokens : Token [ ] , idx : number ) =>
14
15
tokens [ idx ] . nesting === 1 ? `<div v-pre>\n` : `</div>\n`
15
16
} )
16
17
}
17
18
18
- type ContainerArgs = [
19
- typeof container ,
20
- string ,
21
- {
22
- render ( tokens : Token [ ] , idx : number ) : string
23
- }
24
- ]
19
+ type ContainerArgs = [ typeof container , string , { render : RenderRule } ]
25
20
26
- function createContainer ( klass : string , defaultTitle : string ) : ContainerArgs {
21
+ function createContainer (
22
+ klass : string ,
23
+ defaultTitle : string ,
24
+ md : MarkdownIt
25
+ ) : ContainerArgs {
27
26
return [
28
27
container ,
29
28
klass ,
@@ -32,14 +31,11 @@ function createContainer(klass: string, defaultTitle: string): ContainerArgs {
32
31
const token = tokens [ idx ]
33
32
const info = token . info . trim ( ) . slice ( klass . length ) . trim ( )
34
33
if ( token . nesting === 1 ) {
34
+ const title = md . renderInline ( info || defaultTitle )
35
35
if ( klass === 'details' ) {
36
- return `<details class="${ klass } custom-block">${
37
- info ? `<summary>${ info } </summary>` : `<summary>Details</summary>`
38
- } \n`
36
+ return `<details class="${ klass } custom-block"><summary>${ title } </summary>\n`
39
37
}
40
- return `<div class="${ klass } custom-block"><p class="custom-block-title">${
41
- info || defaultTitle
42
- } </p>\n`
38
+ return `<div class="${ klass } custom-block"><p class="custom-block-title">${ title } </p>\n`
43
39
} else {
44
40
return klass === 'details' ? `</details>\n` : `</div>\n`
45
41
}
0 commit comments