UNPKG

4.47 kBTypeScriptView Raw
1/**
2 * @param value Markdown to parse (`string` or `Buffer`).
3 * @param [encoding] Character encoding to understand `value` as when it’s a `Buffer` (`string`, default: `'utf8'`).
4 * @param [options] Configuration
5 */
6export const fromMarkdown: ((
7 value: Value,
8 encoding: Encoding,
9 options?: Options | undefined
10) => Root) &
11 ((value: Value, options?: Options | undefined) => Root)
12export type Encoding = import('micromark-util-types').Encoding
13export type Event = import('micromark-util-types').Event
14export type ParseOptions = import('micromark-util-types').ParseOptions
15export type Token = import('micromark-util-types').Token
16export type TokenizeContext = import('micromark-util-types').TokenizeContext
17export type Value = import('micromark-util-types').Value
18export type UnistParent = import('unist').Parent
19export type Point = import('unist').Point
20export type PhrasingContent = import('mdast').PhrasingContent
21export type Content = import('mdast').Content
22export type Node = Root | Content
23export type Parent = Extract<Node, UnistParent>
24export type Break = import('mdast').Break
25export type Blockquote = import('mdast').Blockquote
26export type Code = import('mdast').Code
27export type Definition = import('mdast').Definition
28export type Emphasis = import('mdast').Emphasis
29export type Heading = import('mdast').Heading
30export type HTML = import('mdast').HTML
31export type Image = import('mdast').Image
32export type ImageReference = import('mdast').ImageReference
33export type InlineCode = import('mdast').InlineCode
34export type Link = import('mdast').Link
35export type LinkReference = import('mdast').LinkReference
36export type List = import('mdast').List
37export type ListItem = import('mdast').ListItem
38export type Paragraph = import('mdast').Paragraph
39export type Root = import('mdast').Root
40export type Strong = import('mdast').Strong
41export type Text = import('mdast').Text
42export type ThematicBreak = import('mdast').ThematicBreak
43export type Fragment = UnistParent & {
44 type: 'fragment'
45 children: Array<PhrasingContent>
46}
47export type _CompileDataFields = {
48 expectingFirstListItemValue: boolean | undefined
49 flowCodeInside: boolean | undefined
50 setextHeadingSlurpLineEnding: boolean | undefined
51 atHardBreak: boolean | undefined
52 referenceType: 'collapsed' | 'full'
53 inReference: boolean | undefined
54 characterReferenceType:
55 | 'characterReferenceMarkerHexadecimal'
56 | 'characterReferenceMarkerNumeric'
57}
58export type CompileData = Record<string, unknown> & Partial<_CompileDataFields>
59export type Transform = (tree: Root) => Root | void
60export type Handle = (this: CompileContext, token: Token) => void
61/**
62 * Token types mapping to handles
63 */
64export type Handles = Record<string, Handle>
65export type NormalizedExtension = Record<
66 string,
67 Record<string, unknown> | Array<unknown>
68> & {
69 canContainEols: Array<string>
70 transforms: Array<Transform>
71 enter: Handles
72 exit: Handles
73}
74/**
75 * An mdast extension changes how markdown tokens are turned into mdast.
76 */
77export type Extension = Partial<NormalizedExtension>
78export type OnEnterError = (
79 this: Omit<CompileContext, 'sliceSerialize'>,
80 left: Token | undefined,
81 right: Token
82) => void
83export type OnExitError = (
84 this: Omit<CompileContext, 'sliceSerialize'>,
85 left: Token,
86 right: Token
87) => void
88/**
89 * mdast compiler context
90 */
91export type CompileContext = {
92 stack: Array<Node | Fragment>
93 tokenStack: Array<[Token, OnEnterError | undefined]>
94 /**
95 * Set data into the key-value store.
96 */
97 setData: (key: string, value?: unknown) => void
98 /**
99 * Get data from the key-value store.
100 */
101 getData: <K extends string>(key: K) => CompileData[K]
102 /**
103 * Capture some of the output data.
104 */
105 buffer: (this: CompileContext) => void
106 /**
107 * Stop capturing and access the output data.
108 */
109 resume: (this: CompileContext) => string
110 /**
111 * Enter a token.
112 */
113 enter: <N extends Node>(
114 this: CompileContext,
115 node: N,
116 token: Token,
117 onError?: OnEnterError | undefined
118 ) => N
119 /**
120 * Exit a token.
121 */
122 exit: (
123 this: CompileContext,
124 token: Token,
125 onError?: OnExitError | undefined
126 ) => Node
127 /**
128 * Get the string value of a token.
129 */
130 sliceSerialize: TokenizeContext['sliceSerialize']
131 /**
132 * Configuration.
133 */
134 config: NormalizedExtension
135}
136export type FromMarkdownOptions = {
137 mdastExtensions?: Array<Extension | Array<Extension>>
138}
139export type Options = ParseOptions & FromMarkdownOptions