@@ -7,9 +7,9 @@ import type {
7
7
} from 'markdown-it'
8
8
import MarkdownIt from 'markdown-it'
9
9
10
- export type PluginSimple = ( ( md : MarkdownItAsync ) => void ) | MarkdownItPluginSimple
11
- export type PluginWithOptions < T = any > = ( ( md : MarkdownItAsync , options ?: T ) => void ) | MarkdownItPluginWithOptions < T >
12
- export type PluginWithParams = ( ( md : MarkdownItAsync , ...params : any [ ] ) => void ) | MarkdownItPluginWithParams
10
+ export type PluginSimple = ( ( md : MarkdownItAsync ) => void )
11
+ export type PluginWithOptions < T = any > = ( ( md : MarkdownItAsync , options ?: T ) => void )
12
+ export type PluginWithParams = ( ( md : MarkdownItAsync , ...params : any [ ] ) => void )
13
13
14
14
export interface MarkdownItAsyncOptions extends Omit < Options , 'highlight' > {
15
15
/**
@@ -31,37 +31,42 @@ function randStr(): string {
31
31
return Math . random ( ) . toString ( 36 ) . slice ( 2 ) + Math . random ( ) . toString ( 36 ) . slice ( 2 )
32
32
}
33
33
34
- export type MarkdownItASyncPlaceholderMap = Map < string , [ promise : Promise < string > , str : string , lang : string , attrs : string ] >
34
+ export type MarkdownItAsyncPlaceholderMap = Map < string , [ promise : Promise < string > , str : string , lang : string , attrs : string ] >
35
35
36
36
export class MarkdownItAsync extends MarkdownIt {
37
- map : MarkdownItASyncPlaceholderMap
37
+ placeholderMap : MarkdownItAsyncPlaceholderMap
38
38
39
39
constructor ( presetName : PresetName , options ?: MarkdownItAsyncOptions )
40
40
constructor ( options ?: MarkdownItAsyncOptions )
41
41
constructor ( ...args : any [ ] ) {
42
- const map : MarkdownItASyncPlaceholderMap = new Map ( )
42
+ const map : MarkdownItAsyncPlaceholderMap = new Map ( )
43
43
const options = args . length === 2 ? args [ 1 ] : args [ 0 ]
44
44
if ( options && 'highlight' in options )
45
45
options . highlight = wrapHightlight ( options . highlight , map )
46
46
super ( ...args as [ ] )
47
- this . map = map
47
+ this . placeholderMap = map
48
48
}
49
49
50
50
use ( plugin : PluginSimple ) : this
51
+ use ( plugin : MarkdownItPluginSimple ) : this
51
52
use < T = any > ( plugin : PluginWithOptions < T > , options ?: T ) : this
52
- use ( plugin : PluginWithParams , ...params : any [ ] ) : this {
53
- return super . use ( plugin as any , ...params )
53
+ use < T = any > ( plugin : MarkdownItPluginWithOptions < T > , options ?: T ) : this
54
+ use ( plugin : PluginWithParams , ...params : any [ ] ) : this
55
+ use ( plugin : MarkdownItPluginWithParams , ...params : any [ ] ) : this
56
+ // implementation
57
+ use ( plugin : any , ...params : any [ ] ) : this {
58
+ return super . use ( plugin , ...params )
54
59
}
55
60
56
61
async renderAsync ( src : string , env ?: any ) : Promise < string > {
57
- this . options . highlight = wrapHightlight ( this . options . highlight , this . map )
62
+ this . options . highlight = wrapHightlight ( this . options . highlight , this . placeholderMap )
58
63
const result = this . render ( src , env )
59
64
return replaceAsync ( result , placeholderRe , async ( match , id ) => {
60
- if ( ! this . map . has ( id ) )
65
+ if ( ! this . placeholderMap . has ( id ) )
61
66
throw new Error ( `Unknown highlight placeholder id: ${ id } ` )
62
- const [ promise , _str , lang , _attrs ] = this . map . get ( id ) !
67
+ const [ promise , _str , lang , _attrs ] = this . placeholderMap . get ( id ) !
63
68
const result = await promise || ''
64
- this . map . delete ( id )
69
+ this . placeholderMap . delete ( id )
65
70
if ( result . startsWith ( '<pre' ) )
66
71
return result
67
72
else
@@ -106,7 +111,7 @@ type NotNull<T> = T extends null | undefined ? never : T
106
111
107
112
const wrappedSet = new WeakSet < NotNull < Options [ 'highlight' ] > > ( )
108
113
109
- function wrapHightlight ( highlight : MarkdownItAsyncOptions [ 'highlight' ] , map : MarkdownItASyncPlaceholderMap ) : Options [ 'highlight' ] {
114
+ function wrapHightlight ( highlight : MarkdownItAsyncOptions [ 'highlight' ] , map : MarkdownItAsyncPlaceholderMap ) : Options [ 'highlight' ] {
110
115
if ( ! highlight )
111
116
return undefined
112
117
0 commit comments