Skip to content

Commit 8723ee2

Browse files
committed
fix(types): .use overrides
1 parent ed930b9 commit 8723ee2

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

build.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineBuildConfig({
1818
// Override `options` type on dist dts only
1919
const newContent = content.replace(
2020
'class MarkdownItAsync extends MarkdownIt {',
21-
'class MarkdownItAsync extends MarkdownIt {\n options: MarkdownItAsyncOptions',
21+
'class MarkdownItAsync extends MarkdownIt {\n // @ts-ignore\n options: MarkdownItAsyncOptions',
2222
)
2323
if (content === newContent)
2424
throw new Error(`Failed to replace for ${file}`)

src/index.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import type {
77
} from 'markdown-it'
88
import MarkdownIt from 'markdown-it'
99

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)
1313

1414
export interface MarkdownItAsyncOptions extends Omit<Options, 'highlight'> {
1515
/**
@@ -31,37 +31,42 @@ function randStr(): string {
3131
return Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
3232
}
3333

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]>
3535

3636
export class MarkdownItAsync extends MarkdownIt {
37-
map: MarkdownItASyncPlaceholderMap
37+
placeholderMap: MarkdownItAsyncPlaceholderMap
3838

3939
constructor(presetName: PresetName, options?: MarkdownItAsyncOptions)
4040
constructor(options?: MarkdownItAsyncOptions)
4141
constructor(...args: any[]) {
42-
const map: MarkdownItASyncPlaceholderMap = new Map()
42+
const map: MarkdownItAsyncPlaceholderMap = new Map()
4343
const options = args.length === 2 ? args[1] : args[0]
4444
if (options && 'highlight' in options)
4545
options.highlight = wrapHightlight(options.highlight, map)
4646
super(...args as [])
47-
this.map = map
47+
this.placeholderMap = map
4848
}
4949

5050
use(plugin: PluginSimple): this
51+
use(plugin: MarkdownItPluginSimple): this
5152
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)
5459
}
5560

5661
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)
5863
const result = this.render(src, env)
5964
return replaceAsync(result, placeholderRe, async (match, id) => {
60-
if (!this.map.has(id))
65+
if (!this.placeholderMap.has(id))
6166
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)!
6368
const result = await promise || ''
64-
this.map.delete(id)
69+
this.placeholderMap.delete(id)
6570
if (result.startsWith('<pre'))
6671
return result
6772
else
@@ -106,7 +111,7 @@ type NotNull<T> = T extends null | undefined ? never : T
106111

107112
const wrappedSet = new WeakSet<NotNull<Options['highlight']>>()
108113

109-
function wrapHightlight(highlight: MarkdownItAsyncOptions['highlight'], map: MarkdownItASyncPlaceholderMap): Options['highlight'] {
114+
function wrapHightlight(highlight: MarkdownItAsyncOptions['highlight'], map: MarkdownItAsyncPlaceholderMap): Options['highlight'] {
110115
if (!highlight)
111116
return undefined
112117

0 commit comments

Comments
 (0)