67
67
* Transform the children of an mdast parent to hast.
68
68
* @property {<Type extends HastNodes>(from: MdastNodes, to: Type) => HastElement | Type } applyData
69
69
* 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
71
73
* Footnote definitions by their identifier.
72
- * @property {Record <string, number> } footnoteCounts
74
+ * @property {Map <string, number> } footnoteCounts
73
75
* Counts for how often the same footnote was called.
74
76
* @property {Array<string> } footnoteOrder
75
77
* Identifiers of order when footnote calls first appear in tree order.
84
86
* @property {<Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | null | undefined) => Array<HastText | Type> } wrap
85
87
* Wrap `nodes` with line endings between each node, adds initial/final line endings when `loose`.
86
88
*
87
- * @property {(identifier: string) => MdastDefinition | undefined } definition
88
- * Definition cache.
89
- *
90
- * To do: expose map, document.
91
- *
92
89
* @typedef Options
93
90
* Configuration (optional).
94
91
* @property {boolean | null | undefined } [allowDangerousHtml=false]
123
120
*/
124
121
125
122
import { 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'
128
124
import { handlers } from './handlers/index.js'
129
125
130
126
const own = { } . hasOwnProperty
@@ -141,100 +137,47 @@ const own = {}.hasOwnProperty
141
137
*/
142
138
export function createState ( tree , options ) {
143
139
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 ( )
146
146
147
147
/** @type {State } */
148
148
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,
156
152
footnoteById,
157
- /** @type { Array<string> } */
153
+ footnoteCounts ,
158
154
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 } ,
164
157
// @ts -expect-error: fix `null` handling.
165
158
one : oneBound ,
166
- all : allBound ,
159
+ options : settings ,
160
+ patch,
167
161
// @ts -expect-error: fix `null` handling.
168
- wrap,
169
- // To do: next major: remove `augment`.
170
- augment
162
+ wrap
171
163
}
172
164
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 ( )
175
169
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
+ }
180
176
}
181
177
} )
182
178
183
179
return state
184
180
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
-
238
181
/**
239
182
* Transform an mdast node into a hast node.
240
183
*
0 commit comments