Skip to content

Commit

Permalink
Update @types/mdast, mdast utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 11, 2023
1 parent b5dfc57 commit 1ea759d
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 309 deletions.
14 changes: 13 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,19 @@ export interface MdxJsxTextElementHast extends HastParent {
// Add nodes to mdast content.
declare module 'mdast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface StaticPhrasingContentMap {
interface RootContentMap {
/**
* MDX JSX element node, occurring in flow (block).
*/
mdxJsxFlowElement: MdxJsxFlowElement
/**
* MDX JSX element node, occurring in text (phrasing).
*/
mdxJsxTextElement: MdxJsxTextElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface PhrasingContentMap {
/**
* MDX JSX element node, occurring in text (phrasing).
*/
Expand Down
57 changes: 29 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ import {parseEntities} from 'parse-entities'
import {stringifyPosition} from 'unist-util-stringify-position'
import {VFileMessage} from 'vfile-message'
import {stringifyEntitiesLight} from 'stringify-entities'
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
import {track} from 'mdast-util-to-markdown/lib/util/track.js'

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

Expand Down Expand Up @@ -163,8 +160,8 @@ export function mdxJsxFromMarkdown() {
start: token.start,
end: token.end
}
if (!this.getData('mdxJsxTagStack')) this.setData('mdxJsxTagStack', [])
this.setData('mdxJsxTag', tag)
if (!this.data.mdxJsxTagStack) this.data.mdxJsxTagStack = []
this.data.mdxJsxTag = tag
this.buffer()
}

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

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

if (tag.close) {
throw new VFileMessage(
Expand All @@ -205,7 +202,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function enterMdxJsxTagSelfClosingMarker(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)

if (tag.close) {
throw new VFileMessage(
Expand All @@ -221,7 +218,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagClosingMarker() {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
tag.close = true
}

Expand All @@ -230,7 +227,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagNamePrimary(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
tag.name = this.sliceSerialize(token)
}

Expand All @@ -239,7 +236,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagNameMember(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
tag.name += '.' + this.sliceSerialize(token)
}

Expand All @@ -248,7 +245,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagNameLocal(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
tag.name += ':' + this.sliceSerialize(token)
}

Expand All @@ -257,7 +254,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function enterMdxJsxTagAttribute(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
enterMdxJsxTagAnyAttribute.call(this, token)
tag.attributes.push({type: 'mdxJsxAttribute', name: '', value: null})
}
Expand All @@ -267,7 +264,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function enterMdxJsxTagExpressionAttribute(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
enterMdxJsxTagAnyAttribute.call(this, token)
tag.attributes.push({type: 'mdxJsxExpressionAttribute', value: ''})
this.buffer()
Expand All @@ -278,7 +275,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagExpressionAttribute(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
const tail = /** @type {MdxJsxExpressionAttribute} */ (
tag.attributes[tag.attributes.length - 1]
)
Expand All @@ -296,7 +293,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagAttributeNamePrimary(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
const node = /** @type {MdxJsxAttribute} */ (
tag.attributes[tag.attributes.length - 1]
)
Expand All @@ -308,7 +305,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagAttributeNameLocal(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
const node = /** @type {MdxJsxAttribute} */ (
tag.attributes[tag.attributes.length - 1]
)
Expand All @@ -320,7 +317,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagAttributeValueLiteral() {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
tag.attributes[tag.attributes.length - 1].value = parseEntities(
this.resume(),
{nonTerminated: false}
Expand All @@ -332,7 +329,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagAttributeValueExpression(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
const tail = /** @type {MdxJsxAttribute} */ (
tag.attributes[tag.attributes.length - 1]
)
Expand All @@ -352,7 +349,7 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagSelfClosingMarker() {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)

tag.selfClosing = true
}
Expand All @@ -362,8 +359,8 @@ export function mdxJsxFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTag(token) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const stack = /** @type {Array<Tag>} */ (this.getData('mdxJsxTagStack'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
const stack = /** @type {Array<Tag>} */ (this.data.mdxJsxTagStack)
const tail = stack[stack.length - 1]

if (tag.close && tail.name !== tag.name) {
Expand Down Expand Up @@ -413,7 +410,7 @@ export function mdxJsxFromMarkdown() {
* @type {OnEnterError}
*/
function onErrorRightIsTag(closing, open) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
const place = closing ? ' before the end of `' + closing.type + '`' : ''
const position = closing
? {start: closing.start, end: closing.end}
Expand All @@ -436,7 +433,7 @@ export function mdxJsxFromMarkdown() {
* @type {OnExitError}
*/
function onErrorLeftIsTag(a, b) {
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
const tag = /** @type {Tag} */ (this.data.mdxJsxTag)
throw new VFileMessage(
'Expected the closing tag `' +
serializeAbbreviatedTag(tag) +
Expand Down Expand Up @@ -523,8 +520,8 @@ export function mdxJsxToMarkdown(options) {
: false
const depth = inferDepth(context)
const currentIndent = createIndent(depth)
const trackerOneLine = track(safeOptions)
const trackerMultiLine = track(safeOptions)
const trackerOneLine = context.createTracker(safeOptions)
const trackerMultiLine = context.createTracker(safeOptions)
/** @type {Array<string>} */
const serializedAttributes = []
const prefix = (flow ? currentIndent : '') + '<' + (node.name || '')
Expand Down Expand Up @@ -633,7 +630,11 @@ export function mdxJsxToMarkdown(options) {
if (node.children && node.children.length > 0) {
if (node.type === 'mdxJsxTextElement') {
value += tracker.move(
containerPhrasing(node, context, {
// @ts-expect-error: `containerPhrasing` is typed correctly, but TS
// generates *hardcoded* types, which means that our dynamically added
// directives are not present.
// At some point, TS should fix that, and `from-markdown` should be fine.
context.containerPhrasing(node, {
...tracker.current(),
before: '>',
after: '<'
Expand Down Expand Up @@ -697,7 +698,7 @@ function containerFlow(parent, state, info) {
const serializedChild =
child.type === 'mdxJsxFlowElement'
? result
: indentLines(result, function (line, _, blank) {
: state.indentLines(result, function (line, _, blank) {
return (blank ? '' : currentIndent) + line
})

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@
],
"dependencies": {
"@types/estree-jsx": "^1.0.0",
"@types/hast": "^2.0.0",
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"@types/hast": "^3.0.0",
"@types/mdast": "^4.0.0",
"@types/unist": "^3.0.0",
"ccount": "^2.0.0",
"mdast-util-from-markdown": "^1.1.0",
"mdast-util-to-markdown": "^1.3.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-to-markdown": "^2.0.0",
"parse-entities": "^4.0.0",
"stringify-entities": "^4.0.0",
"unist-util-remove-position": "^4.0.0",
"unist-util-stringify-position": "^3.0.0",
"vfile-message": "^3.0.0"
"unist-util-remove-position": "^5.0.0",
"unist-util-stringify-position": "^4.0.0",
"vfile-message": "^4.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"acorn": "^8.0.0",
"c8": "^7.0.0",
"micromark-extension-mdx-jsx": "^1.0.0",
"micromark-extension-mdx-md": "^1.0.0",
"c8": "^8.0.0",
"micromark-extension-mdx-jsx": "^2.0.0",
"micromark-extension-mdx-md": "^2.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
Expand Down
Loading

0 comments on commit 1ea759d

Please sign in to comment.