Skip to content

Commit 1ea759d

Browse files
committed
Update @types/mdast, mdast utilities
1 parent b5dfc57 commit 1ea759d

File tree

4 files changed

+354
-309
lines changed

4 files changed

+354
-309
lines changed

index.d.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,19 @@ export interface MdxJsxTextElementHast extends HastParent {
187187
// Add nodes to mdast content.
188188
declare module 'mdast' {
189189
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
190-
interface StaticPhrasingContentMap {
190+
interface RootContentMap {
191+
/**
192+
* MDX JSX element node, occurring in flow (block).
193+
*/
194+
mdxJsxFlowElement: MdxJsxFlowElement
195+
/**
196+
* MDX JSX element node, occurring in text (phrasing).
197+
*/
198+
mdxJsxTextElement: MdxJsxTextElement
199+
}
200+
201+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
202+
interface PhrasingContentMap {
191203
/**
192204
* MDX JSX element node, occurring in text (phrasing).
193205
*/

lib/index.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ import {parseEntities} from 'parse-entities'
6060
import {stringifyPosition} from 'unist-util-stringify-position'
6161
import {VFileMessage} from 'vfile-message'
6262
import {stringifyEntitiesLight} from 'stringify-entities'
63-
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
64-
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
65-
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
6663

6764
// To do: next major: use `state`, use utilities from state, rename `safeOptions` to `info`.
6865

@@ -163,8 +160,8 @@ export function mdxJsxFromMarkdown() {
163160
start: token.start,
164161
end: token.end
165162
}
166-
if (!this.getData('mdxJsxTagStack')) this.setData('mdxJsxTagStack', [])
167-
this.setData('mdxJsxTag', tag)
163+
if (!this.data.mdxJsxTagStack) this.data.mdxJsxTagStack = []
164+
this.data.mdxJsxTag = tag
168165
this.buffer()
169166
}
170167

@@ -173,7 +170,7 @@ export function mdxJsxFromMarkdown() {
173170
* @type {FromMarkdownHandle}
174171
*/
175172
function enterMdxJsxTagClosingMarker(token) {
176-
const stack = /** @type {Array<Tag>} */ (this.getData('mdxJsxTagStack'))
173+
const stack = /** @type {Array<Tag>} */ (this.data.mdxJsxTagStack)
177174

178175
if (stack.length === 0) {
179176
throw new VFileMessage(
@@ -189,7 +186,7 @@ export function mdxJsxFromMarkdown() {
189186
* @type {FromMarkdownHandle}
190187
*/
191188
function enterMdxJsxTagAnyAttribute(token) {
192-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
189+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
193190

194191
if (tag.close) {
195192
throw new VFileMessage(
@@ -205,7 +202,7 @@ export function mdxJsxFromMarkdown() {
205202
* @type {FromMarkdownHandle}
206203
*/
207204
function enterMdxJsxTagSelfClosingMarker(token) {
208-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
205+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
209206

210207
if (tag.close) {
211208
throw new VFileMessage(
@@ -221,7 +218,7 @@ export function mdxJsxFromMarkdown() {
221218
* @type {FromMarkdownHandle}
222219
*/
223220
function exitMdxJsxTagClosingMarker() {
224-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
221+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
225222
tag.close = true
226223
}
227224

@@ -230,7 +227,7 @@ export function mdxJsxFromMarkdown() {
230227
* @type {FromMarkdownHandle}
231228
*/
232229
function exitMdxJsxTagNamePrimary(token) {
233-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
230+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
234231
tag.name = this.sliceSerialize(token)
235232
}
236233

@@ -239,7 +236,7 @@ export function mdxJsxFromMarkdown() {
239236
* @type {FromMarkdownHandle}
240237
*/
241238
function exitMdxJsxTagNameMember(token) {
242-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
239+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
243240
tag.name += '.' + this.sliceSerialize(token)
244241
}
245242

@@ -248,7 +245,7 @@ export function mdxJsxFromMarkdown() {
248245
* @type {FromMarkdownHandle}
249246
*/
250247
function exitMdxJsxTagNameLocal(token) {
251-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
248+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
252249
tag.name += ':' + this.sliceSerialize(token)
253250
}
254251

@@ -257,7 +254,7 @@ export function mdxJsxFromMarkdown() {
257254
* @type {FromMarkdownHandle}
258255
*/
259256
function enterMdxJsxTagAttribute(token) {
260-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
257+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
261258
enterMdxJsxTagAnyAttribute.call(this, token)
262259
tag.attributes.push({type: 'mdxJsxAttribute', name: '', value: null})
263260
}
@@ -267,7 +264,7 @@ export function mdxJsxFromMarkdown() {
267264
* @type {FromMarkdownHandle}
268265
*/
269266
function enterMdxJsxTagExpressionAttribute(token) {
270-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
267+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
271268
enterMdxJsxTagAnyAttribute.call(this, token)
272269
tag.attributes.push({type: 'mdxJsxExpressionAttribute', value: ''})
273270
this.buffer()
@@ -278,7 +275,7 @@ export function mdxJsxFromMarkdown() {
278275
* @type {FromMarkdownHandle}
279276
*/
280277
function exitMdxJsxTagExpressionAttribute(token) {
281-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
278+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
282279
const tail = /** @type {MdxJsxExpressionAttribute} */ (
283280
tag.attributes[tag.attributes.length - 1]
284281
)
@@ -296,7 +293,7 @@ export function mdxJsxFromMarkdown() {
296293
* @type {FromMarkdownHandle}
297294
*/
298295
function exitMdxJsxTagAttributeNamePrimary(token) {
299-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
296+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
300297
const node = /** @type {MdxJsxAttribute} */ (
301298
tag.attributes[tag.attributes.length - 1]
302299
)
@@ -308,7 +305,7 @@ export function mdxJsxFromMarkdown() {
308305
* @type {FromMarkdownHandle}
309306
*/
310307
function exitMdxJsxTagAttributeNameLocal(token) {
311-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
308+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
312309
const node = /** @type {MdxJsxAttribute} */ (
313310
tag.attributes[tag.attributes.length - 1]
314311
)
@@ -320,7 +317,7 @@ export function mdxJsxFromMarkdown() {
320317
* @type {FromMarkdownHandle}
321318
*/
322319
function exitMdxJsxTagAttributeValueLiteral() {
323-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
320+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
324321
tag.attributes[tag.attributes.length - 1].value = parseEntities(
325322
this.resume(),
326323
{nonTerminated: false}
@@ -332,7 +329,7 @@ export function mdxJsxFromMarkdown() {
332329
* @type {FromMarkdownHandle}
333330
*/
334331
function exitMdxJsxTagAttributeValueExpression(token) {
335-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
332+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
336333
const tail = /** @type {MdxJsxAttribute} */ (
337334
tag.attributes[tag.attributes.length - 1]
338335
)
@@ -352,7 +349,7 @@ export function mdxJsxFromMarkdown() {
352349
* @type {FromMarkdownHandle}
353350
*/
354351
function exitMdxJsxTagSelfClosingMarker() {
355-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
352+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
356353

357354
tag.selfClosing = true
358355
}
@@ -362,8 +359,8 @@ export function mdxJsxFromMarkdown() {
362359
* @type {FromMarkdownHandle}
363360
*/
364361
function exitMdxJsxTag(token) {
365-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
366-
const stack = /** @type {Array<Tag>} */ (this.getData('mdxJsxTagStack'))
362+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
363+
const stack = /** @type {Array<Tag>} */ (this.data.mdxJsxTagStack)
367364
const tail = stack[stack.length - 1]
368365

369366
if (tag.close && tail.name !== tag.name) {
@@ -413,7 +410,7 @@ export function mdxJsxFromMarkdown() {
413410
* @type {OnEnterError}
414411
*/
415412
function onErrorRightIsTag(closing, open) {
416-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
413+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
417414
const place = closing ? ' before the end of `' + closing.type + '`' : ''
418415
const position = closing
419416
? {start: closing.start, end: closing.end}
@@ -436,7 +433,7 @@ export function mdxJsxFromMarkdown() {
436433
* @type {OnExitError}
437434
*/
438435
function onErrorLeftIsTag(a, b) {
439-
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
436+
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
440437
throw new VFileMessage(
441438
'Expected the closing tag `' +
442439
serializeAbbreviatedTag(tag) +
@@ -523,8 +520,8 @@ export function mdxJsxToMarkdown(options) {
523520
: false
524521
const depth = inferDepth(context)
525522
const currentIndent = createIndent(depth)
526-
const trackerOneLine = track(safeOptions)
527-
const trackerMultiLine = track(safeOptions)
523+
const trackerOneLine = context.createTracker(safeOptions)
524+
const trackerMultiLine = context.createTracker(safeOptions)
528525
/** @type {Array<string>} */
529526
const serializedAttributes = []
530527
const prefix = (flow ? currentIndent : '') + '<' + (node.name || '')
@@ -633,7 +630,11 @@ export function mdxJsxToMarkdown(options) {
633630
if (node.children && node.children.length > 0) {
634631
if (node.type === 'mdxJsxTextElement') {
635632
value += tracker.move(
636-
containerPhrasing(node, context, {
633+
// @ts-expect-error: `containerPhrasing` is typed correctly, but TS
634+
// generates *hardcoded* types, which means that our dynamically added
635+
// directives are not present.
636+
// At some point, TS should fix that, and `from-markdown` should be fine.
637+
context.containerPhrasing(node, {
637638
...tracker.current(),
638639
before: '>',
639640
after: '<'
@@ -697,7 +698,7 @@ function containerFlow(parent, state, info) {
697698
const serializedChild =
698699
child.type === 'mdxJsxFlowElement'
699700
? result
700-
: indentLines(result, function (line, _, blank) {
701+
: state.indentLines(result, function (line, _, blank) {
701702
return (blank ? '' : currentIndent) + line
702703
})
703704

package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@
3737
],
3838
"dependencies": {
3939
"@types/estree-jsx": "^1.0.0",
40-
"@types/hast": "^2.0.0",
41-
"@types/mdast": "^3.0.0",
42-
"@types/unist": "^2.0.0",
40+
"@types/hast": "^3.0.0",
41+
"@types/mdast": "^4.0.0",
42+
"@types/unist": "^3.0.0",
4343
"ccount": "^2.0.0",
44-
"mdast-util-from-markdown": "^1.1.0",
45-
"mdast-util-to-markdown": "^1.3.0",
44+
"mdast-util-from-markdown": "^2.0.0",
45+
"mdast-util-to-markdown": "^2.0.0",
4646
"parse-entities": "^4.0.0",
4747
"stringify-entities": "^4.0.0",
48-
"unist-util-remove-position": "^4.0.0",
49-
"unist-util-stringify-position": "^3.0.0",
50-
"vfile-message": "^3.0.0"
48+
"unist-util-remove-position": "^5.0.0",
49+
"unist-util-stringify-position": "^4.0.0",
50+
"vfile-message": "^4.0.0"
5151
},
5252
"devDependencies": {
5353
"@types/node": "^20.0.0",
5454
"acorn": "^8.0.0",
55-
"c8": "^7.0.0",
56-
"micromark-extension-mdx-jsx": "^1.0.0",
57-
"micromark-extension-mdx-md": "^1.0.0",
55+
"c8": "^8.0.0",
56+
"micromark-extension-mdx-jsx": "^2.0.0",
57+
"micromark-extension-mdx-md": "^2.0.0",
5858
"prettier": "^2.0.0",
5959
"remark-cli": "^11.0.0",
6060
"remark-preset-wooorm": "^9.0.0",

0 commit comments

Comments
 (0)