6767 * Transform the children of an mdast parent to hast.
6868 * @property {<Type extends HastNodes>(from: MdastNodes, to: Type) => HastElement | Type } applyData
6969 * Honor the `data` of `from`, and generate an element instead of `node`.
70- * @property {Record<string, MdastFootnoteDefinition> } footnoteById
70+ * @property {Map<string, MdastDefinition> } definitionById
71+ * Definitions by their identifier.
72+ * @property {Map<string, MdastFootnoteDefinition> } footnoteById
7173 * Footnote definitions by their identifier.
72- * @property {Record <string, number> } footnoteCounts
74+ * @property {Map <string, number> } footnoteCounts
7375 * Counts for how often the same footnote was called.
7476 * @property {Array<string> } footnoteOrder
7577 * Identifiers of order when footnote calls first appear in tree order.
8486 * @property {<Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | null | undefined) => Array<HastText | Type> } wrap
8587 * Wrap `nodes` with line endings between each node, adds initial/final line endings when `loose`.
8688 *
87- * @property {(identifier: string) => MdastDefinition | undefined } definition
88- * Definition cache.
89- *
90- * To do: expose map, document.
91- *
9289 * @typedef Options
9390 * Configuration (optional).
9491 * @property {boolean | null | undefined } [allowDangerousHtml=false]
123120 */
124121
125122import { visit } from 'unist-util-visit'
126- import { pointEnd , pointStart , position } from 'unist-util-position'
127- import { definitions } from 'mdast-util-definitions'
123+ import { position } from 'unist-util-position'
128124import { handlers } from './handlers/index.js'
129125
130126const own = { } . hasOwnProperty
@@ -141,100 +137,47 @@ const own = {}.hasOwnProperty
141137 */
142138export function createState ( tree , options ) {
143139 const settings = options || { }
144- /** @type {Record<string, MdastFootnoteDefinition> } */
145- const footnoteById = { }
140+ /** @type {Map<string, MdastDefinition> } */
141+ const definitionById = new Map ( )
142+ /** @type {Map<string, MdastFootnoteDefinition> } */
143+ const footnoteById = new Map ( )
144+ /** @type {Map<string, number> } */
145+ const footnoteCounts = new Map ( )
146146
147147 /** @type {State } */
148148 const state = {
149- options : settings ,
150- // @ts -expect-error: fix `null` handling?
151- handlers : { ...handlers , ...settings . handlers } ,
152-
153- // To do: next major: replace utility with `definitionById` object, so we
154- // only walk once (as we need footnotes too).
155- definition : definitions ( tree ) ,
149+ all : allBound ,
150+ applyData,
151+ definitionById,
156152 footnoteById,
157- /** @type { Array<string> } */
153+ footnoteCounts ,
158154 footnoteOrder : [ ] ,
159- /** @type {Record<string, number> } */
160- footnoteCounts : { } ,
161-
162- patch,
163- applyData,
155+ // @ts -expect-error: fix `null` handling?
156+ handlers : { ...handlers , ...settings . handlers } ,
164157 // @ts -expect-error: fix `null` handling.
165158 one : oneBound ,
166- all : allBound ,
159+ options : settings ,
160+ patch,
167161 // @ts -expect-error: fix `null` handling.
168- wrap,
169- // To do: next major: remove `augment`.
170- augment
162+ wrap
171163 }
172164
173- visit ( tree , 'footnoteDefinition' , function ( definition ) {
174- const id = String ( definition . identifier ) . toUpperCase ( )
165+ visit ( tree , function ( node ) {
166+ if ( node . type === 'definition' || node . type === 'footnoteDefinition' ) {
167+ const map = node . type === 'definition' ? definitionById : footnoteById
168+ const id = String ( node . identifier ) . toUpperCase ( )
175169
176- // Mimick CM behavior of link definitions.
177- // See: <https://github.com/syntax-tree/mdast-util-definitions/blob/8290999/index.js#L26>.
178- if ( ! own . call ( footnoteById , id ) ) {
179- footnoteById [ id ] = definition
170+ // Mimick CM behavior of link definitions.
171+ // See: <https://github.com/syntax-tree/mdast-util-definitions/blob/9032189/lib/index.js#L20-L21>.
172+ if ( ! map . has ( id ) ) {
173+ // @ts -expect-error: node type matches map.
174+ map . set ( id , node )
175+ }
180176 }
181177 } )
182178
183179 return state
184180
185- /**
186- * Finalise the created `right`, a hast node, from `left`, an mdast node.
187- *
188- * @param {MdastNodeWithData | PositionLike | null | undefined } left
189- * @param {HastElementContent } right
190- * @returns {HastElementContent }
191- */
192- /* c8 ignore start */
193- // To do: next major: remove.
194- function augment ( left , right ) {
195- // Handle `data.hName`, `data.hProperties, `data.hChildren`.
196- if ( left && 'data' in left && left . data ) {
197- /** @type {MdastData } */
198- const data = left . data
199-
200- if ( data . hName ) {
201- if ( right . type !== 'element' ) {
202- right = {
203- type : 'element' ,
204- tagName : '' ,
205- properties : { } ,
206- children : [ ]
207- }
208- }
209-
210- right . tagName = data . hName
211- }
212-
213- if ( right . type === 'element' && data . hProperties ) {
214- right . properties = { ...right . properties , ...data . hProperties }
215- }
216-
217- if ( 'children' in right && right . children && data . hChildren ) {
218- right . children = data . hChildren
219- }
220- }
221-
222- if ( left ) {
223- const ctx = 'type' in left ? left : { position : left }
224- // @ts -expect-error: fine, can be removed when this function is removed.
225- const start = pointStart ( ctx )
226- // @ts -expect-error: fine, can be removed when this function is removed.
227- const end = pointEnd ( ctx )
228-
229- if ( start && end ) {
230- right . position = { start, end}
231- }
232- }
233-
234- return right
235- }
236- /* c8 ignore stop */
237-
238181 /**
239182 * Transform an mdast node into a hast node.
240183 *
0 commit comments