Skip to content

Commit 19c16ee

Browse files
committed
Refactor types to use CompileData interface
1 parent 9c53c68 commit 19c16ee

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

index.d.ts

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
} from 'mdast'
88
import type {Program} from 'estree-jsx'
99

10+
import type {Tag} from './lib/index.js'
11+
1012
// Expose JavaScript API.
1113
export {mdxJsxFromMarkdown, mdxJsxToMarkdown} from './lib/index.js'
1214

@@ -78,3 +80,19 @@ declare module 'hast' {
7880
mdxJsxFlowElement: MdxJsxFlowElement
7981
}
8082
}
83+
84+
// Add custom data tracked to turn markdown into a tree.
85+
declare module 'mdast-util-from-markdown' {
86+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
87+
interface CompileData {
88+
/**
89+
* Current MDX JSX tag.
90+
*/
91+
mdxJsxTag?: Tag | undefined
92+
93+
/**
94+
* Current stack of open MDX JSX tags.
95+
*/
96+
mdxJsxTagStack?: Tag[] | undefined
97+
}
98+
}

lib/index.js

-21
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ export function mdxJsxFromMarkdown() {
163163
start: token.start,
164164
end: token.end
165165
}
166-
// @ts-expect-error: to do: register.
167166
if (!this.getData('mdxJsxTagStack')) this.setData('mdxJsxTagStack', [])
168-
// @ts-expect-error: to do: register.
169167
this.setData('mdxJsxTag', tag)
170168
this.buffer()
171169
}
@@ -175,7 +173,6 @@ export function mdxJsxFromMarkdown() {
175173
* @type {FromMarkdownHandle}
176174
*/
177175
function enterMdxJsxTagClosingMarker(token) {
178-
// @ts-expect-error: to do: register.
179176
const stack = /** @type {Array<Tag>} */ (this.getData('mdxJsxTagStack'))
180177

181178
if (stack.length === 0) {
@@ -192,7 +189,6 @@ export function mdxJsxFromMarkdown() {
192189
* @type {FromMarkdownHandle}
193190
*/
194191
function enterMdxJsxTagAnyAttribute(token) {
195-
// @ts-expect-error: to do: register.
196192
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
197193

198194
if (tag.close) {
@@ -209,7 +205,6 @@ export function mdxJsxFromMarkdown() {
209205
* @type {FromMarkdownHandle}
210206
*/
211207
function enterMdxJsxTagSelfClosingMarker(token) {
212-
// @ts-expect-error: to do: register.
213208
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
214209

215210
if (tag.close) {
@@ -226,7 +221,6 @@ export function mdxJsxFromMarkdown() {
226221
* @type {FromMarkdownHandle}
227222
*/
228223
function exitMdxJsxTagClosingMarker() {
229-
// @ts-expect-error: to do: register.
230224
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
231225
tag.close = true
232226
}
@@ -236,7 +230,6 @@ export function mdxJsxFromMarkdown() {
236230
* @type {FromMarkdownHandle}
237231
*/
238232
function exitMdxJsxTagNamePrimary(token) {
239-
// @ts-expect-error: to do: register.
240233
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
241234
tag.name = this.sliceSerialize(token)
242235
}
@@ -246,7 +239,6 @@ export function mdxJsxFromMarkdown() {
246239
* @type {FromMarkdownHandle}
247240
*/
248241
function exitMdxJsxTagNameMember(token) {
249-
// @ts-expect-error: to do: register.
250242
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
251243
tag.name += '.' + this.sliceSerialize(token)
252244
}
@@ -256,7 +248,6 @@ export function mdxJsxFromMarkdown() {
256248
* @type {FromMarkdownHandle}
257249
*/
258250
function exitMdxJsxTagNameLocal(token) {
259-
// @ts-expect-error: to do: register.
260251
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
261252
tag.name += ':' + this.sliceSerialize(token)
262253
}
@@ -266,7 +257,6 @@ export function mdxJsxFromMarkdown() {
266257
* @type {FromMarkdownHandle}
267258
*/
268259
function enterMdxJsxTagAttribute(token) {
269-
// @ts-expect-error: to do: register.
270260
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
271261
enterMdxJsxTagAnyAttribute.call(this, token)
272262
tag.attributes.push({type: 'mdxJsxAttribute', name: '', value: null})
@@ -277,7 +267,6 @@ export function mdxJsxFromMarkdown() {
277267
* @type {FromMarkdownHandle}
278268
*/
279269
function enterMdxJsxTagExpressionAttribute(token) {
280-
// @ts-expect-error: to do: register.
281270
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
282271
enterMdxJsxTagAnyAttribute.call(this, token)
283272
tag.attributes.push({type: 'mdxJsxExpressionAttribute', value: ''})
@@ -289,7 +278,6 @@ export function mdxJsxFromMarkdown() {
289278
* @type {FromMarkdownHandle}
290279
*/
291280
function exitMdxJsxTagExpressionAttribute(token) {
292-
// @ts-expect-error: to do: register.
293281
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
294282
const tail = /** @type {MdxJsxExpressionAttribute} */ (
295283
tag.attributes[tag.attributes.length - 1]
@@ -310,7 +298,6 @@ export function mdxJsxFromMarkdown() {
310298
* @type {FromMarkdownHandle}
311299
*/
312300
function exitMdxJsxTagAttributeNamePrimary(token) {
313-
// @ts-expect-error: to do: register.
314301
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
315302
const node = /** @type {MdxJsxAttribute} */ (
316303
tag.attributes[tag.attributes.length - 1]
@@ -323,7 +310,6 @@ export function mdxJsxFromMarkdown() {
323310
* @type {FromMarkdownHandle}
324311
*/
325312
function exitMdxJsxTagAttributeNameLocal(token) {
326-
// @ts-expect-error: to do: register.
327313
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
328314
const node = /** @type {MdxJsxAttribute} */ (
329315
tag.attributes[tag.attributes.length - 1]
@@ -336,7 +322,6 @@ export function mdxJsxFromMarkdown() {
336322
* @type {FromMarkdownHandle}
337323
*/
338324
function exitMdxJsxTagAttributeValueLiteral() {
339-
// @ts-expect-error: to do: register.
340325
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
341326
tag.attributes[tag.attributes.length - 1].value = parseEntities(
342327
this.resume(),
@@ -349,7 +334,6 @@ export function mdxJsxFromMarkdown() {
349334
* @type {FromMarkdownHandle}
350335
*/
351336
function exitMdxJsxTagAttributeValueExpression(token) {
352-
// @ts-expect-error: to do: register.
353337
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
354338
const tail = /** @type {MdxJsxAttribute} */ (
355339
tag.attributes[tag.attributes.length - 1]
@@ -372,7 +356,6 @@ export function mdxJsxFromMarkdown() {
372356
* @type {FromMarkdownHandle}
373357
*/
374358
function exitMdxJsxTagSelfClosingMarker() {
375-
// @ts-expect-error: to do: register.
376359
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
377360

378361
tag.selfClosing = true
@@ -383,9 +366,7 @@ export function mdxJsxFromMarkdown() {
383366
* @type {FromMarkdownHandle}
384367
*/
385368
function exitMdxJsxTag(token) {
386-
// @ts-expect-error: to do: register.
387369
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
388-
// @ts-expect-error: to do: register.
389370
const stack = /** @type {Array<Tag>} */ (this.getData('mdxJsxTagStack'))
390371
const tail = stack[stack.length - 1]
391372

@@ -436,7 +417,6 @@ export function mdxJsxFromMarkdown() {
436417
* @type {OnEnterError}
437418
*/
438419
function onErrorRightIsTag(closing, open) {
439-
// @ts-expect-error: to do: register.
440420
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
441421
const place = closing ? ' before the end of `' + closing.type + '`' : ''
442422
const position = closing
@@ -460,7 +440,6 @@ export function mdxJsxFromMarkdown() {
460440
* @type {OnExitError}
461441
*/
462442
function onErrorLeftIsTag(a, b) {
463-
// @ts-expect-error: to do: register.
464443
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
465444
throw new VFileMessage(
466445
'Expected the closing tag `' +

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@types/hast": "^2.0.0",
4141
"@types/mdast": "^3.0.0",
4242
"ccount": "^2.0.0",
43+
"mdast-util-from-markdown": "^1.1.0",
4344
"mdast-util-to-markdown": "^1.3.0",
4445
"parse-entities": "^4.0.0",
4546
"stringify-entities": "^4.0.0",
@@ -51,7 +52,6 @@
5152
"@types/tape": "^4.0.0",
5253
"acorn": "^8.0.0",
5354
"c8": "^7.0.0",
54-
"mdast-util-from-markdown": "^1.1.0",
5555
"micromark-extension-mdx-jsx": "^1.0.0",
5656
"micromark-extension-mdx-md": "^1.0.0",
5757
"prettier": "^2.0.0",

0 commit comments

Comments
 (0)