feat: complete MDX migration, stabilize routing and lead capture

Former-commit-id: ab5cc38c97e5f73d6e821465b90d199fb6e6edc9
This commit is contained in:
2026-05-03 21:06:47 +02:00
parent b13f628b55
commit f2fdea96ec
7175 changed files with 371649 additions and 1078689 deletions

View File

@@ -0,0 +1 @@
../../../@types+estree@1.0.8/node_modules/@types/estree

View File

@@ -0,0 +1 @@
../../../@types+estree-jsx@1.0.5/node_modules/@types/estree-jsx

View File

@@ -0,0 +1 @@
../../../@types+hast@3.0.4/node_modules/@types/hast

View File

@@ -0,0 +1 @@
../../comma-separated-tokens@2.0.3/node_modules/comma-separated-tokens

View File

@@ -0,0 +1 @@
../../devlop@1.1.0/node_modules/devlop

View File

@@ -0,0 +1 @@
../../estree-util-attach-comments@3.0.0/node_modules/estree-util-attach-comments

View File

@@ -0,0 +1 @@
../../estree-util-is-identifier-name@3.0.0/node_modules/estree-util-is-identifier-name

View File

@@ -0,0 +1,9 @@
export { handlers as defaultHandlers } from "./lib/handlers/index.js";
export { toEstree } from "./lib/index.js";
export type ElementAttributeNameCase = import("./lib/state.js").ElementAttributeNameCase;
export type Handle = import("./lib/state.js").Handle;
export type Options = import("./lib/state.js").Options;
export type Space = import("./lib/state.js").Space;
export type State = import("./lib/state.js").State;
export type StylePropertyNameCase = import("./lib/state.js").StylePropertyNameCase;
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":";;uCACa,OAAO,gBAAgB,EAAE,wBAAwB;qBACjD,OAAO,gBAAgB,EAAE,MAAM;sBAC/B,OAAO,gBAAgB,EAAE,OAAO;oBAChC,OAAO,gBAAgB,EAAE,KAAK;oBAC9B,OAAO,gBAAgB,EAAE,KAAK;oCAC9B,OAAO,gBAAgB,EAAE,qBAAqB"}

View File

@@ -0,0 +1,11 @@
/**
* @typedef {import('./lib/state.js').ElementAttributeNameCase} ElementAttributeNameCase
* @typedef {import('./lib/state.js').Handle} Handle
* @typedef {import('./lib/state.js').Options} Options
* @typedef {import('./lib/state.js').Space} Space
* @typedef {import('./lib/state.js').State} State
* @typedef {import('./lib/state.js').StylePropertyNameCase} StylePropertyNameCase
*/
export {handlers as defaultHandlers} from './lib/handlers/index.js'
export {toEstree} from './lib/index.js'

View File

@@ -0,0 +1,15 @@
/**
* Turn a hast comment into an estree node.
*
* @param {HastComment} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer}
* estree expression.
*/
export function comment(node: HastComment, state: State): JsxExpressionContainer;
import type { Comment as HastComment } from 'hast';
import type { State } from 'hast-util-to-estree';
import type { JSXExpressionContainer as JsxExpressionContainer } from 'estree-jsx';
//# sourceMappingURL=comment.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"comment.d.ts","sourceRoot":"","sources":["comment.js"],"names":[],"mappings":"AAaA;;;;;;;;;GASG;AACH,8BAPW,WAAW,SAEX,KAAK,GAEH,sBAAsB,CAqBlC;4CAlCwC,MAAM;2BADvB,qBAAqB;sEAFnC,YAAY"}

View File

@@ -0,0 +1,42 @@
/**
* @import {
* JSXEmptyExpression as JsxEmptyExpression,
* JSXExpressionContainer as JsxExpressionContainer,
* } from 'estree-jsx'
* @import {Comment} from 'estree'
* @import {State} from 'hast-util-to-estree'
* @import {Comment as HastComment} from 'hast'
*/
// Make VS Code show references to the above types.
''
/**
* Turn a hast comment into an estree node.
*
* @param {HastComment} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer}
* estree expression.
*/
export function comment(node, state) {
/** @type {Comment} */
const result = {type: 'Block', value: node.value}
state.inherit(node, result)
state.comments.push(result)
/** @type {JsxEmptyExpression} */
const expression = {
type: 'JSXEmptyExpression',
// @ts-expect-error: `comments` is custom.
comments: [Object.assign({}, result, {leading: false, trailing: true})]
}
state.patch(node, expression)
/** @type {JsxExpressionContainer} */
const container = {type: 'JSXExpressionContainer', expression}
state.patch(node, container)
return container
}

View File

@@ -0,0 +1,15 @@
/**
* Turn a hast element into an estree node.
*
* @param {HastElement} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxElement}
* estree expression.
*/
export function element(node: HastElement, state: State): JsxElement;
import type { Element as HastElement } from 'hast';
import type { State } from 'hast-util-to-estree';
import type { JSXElement as JsxElement } from 'estree-jsx';
//# sourceMappingURL=element.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"element.d.ts","sourceRoot":"","sources":["element.js"],"names":[],"mappings":"AAsBA;;;;;;;;;GASG;AAEH,8BARW,WAAW,SAEX,KAAK,GAEH,UAAU,CA6LtB;4CAlNwC,MAAM;2BADvB,qBAAqB;8CAFnC,YAAY"}

View File

@@ -0,0 +1,288 @@
/**
* @import {
* JSXAttribute as JsxAttribute,
* JSXElement as JsxElement,
* JSXSpreadAttribute as JsxSpreadAttribute,
* } from 'estree-jsx'
* @import {Property} from 'estree'
* @import {State} from 'hast-util-to-estree'
* @import {Element as HastElement} from 'hast'
*/
import {stringify as commas} from 'comma-separated-tokens'
import {name as identifierName} from 'estree-util-is-identifier-name'
import {find, hastToReact, svg} from 'property-information'
import {stringify as spaces} from 'space-separated-tokens'
import styleToJs from 'style-to-js'
const own = {}.hasOwnProperty
const cap = /[A-Z]/g
const tableCellElement = new Set(['td', 'th'])
/**
* Turn a hast element into an estree node.
*
* @param {HastElement} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxElement}
* estree expression.
*/
// eslint-disable-next-line complexity
export function element(node, state) {
const parentSchema = state.schema
let schema = parentSchema
const properties = node.properties || {}
if (parentSchema.space === 'html' && node.tagName.toLowerCase() === 'svg') {
schema = svg
state.schema = schema
}
const children = state.all(node)
/** @type {Array<JsxAttribute | JsxSpreadAttribute>} */
const attributes = []
/** @type {string} */
let property
/** @type {string | undefined} */
let alignValue
/** @type {Array<Property> | undefined} */
let styleProperties
for (property in properties) {
if (own.call(properties, property)) {
let value = properties[property]
const info = find(schema, property)
/** @type {JsxAttribute['value']} */
let attributeValue
// Ignore nullish and `NaN` values.
// Ignore `false` and falsey known booleans.
if (
value === null ||
value === undefined ||
value === false ||
(typeof value === 'number' && Number.isNaN(value)) ||
(!value && info.boolean)
) {
continue
}
property =
state.elementAttributeNameCase === 'react' && info.space
? hastToReact[info.property] || info.property
: info.attribute
if (Array.isArray(value)) {
// Accept `array`.
// Most properties are space-separated.
value = info.commaSeparated ? commas(value) : spaces(value)
}
if (property === 'style') {
let styleObject =
typeof value === 'object'
? value
: parseStyle(String(value), node.tagName)
if (state.stylePropertyNameCase === 'css') {
styleObject = transformStylesToCssCasing(styleObject)
}
/** @type {Array<Property>} */
const cssProperties = []
/** @type {string} */
let cssProperty
for (cssProperty in styleObject) {
// eslint-disable-next-line max-depth
if (own.call(styleObject, cssProperty)) {
cssProperties.push({
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: identifierName(cssProperty)
? {type: 'Identifier', name: cssProperty}
: {type: 'Literal', value: cssProperty},
value: {type: 'Literal', value: String(styleObject[cssProperty])},
kind: 'init'
})
}
}
styleProperties = cssProperties
attributeValue = {
type: 'JSXExpressionContainer',
expression: {type: 'ObjectExpression', properties: cssProperties}
}
} else if (value === true) {
attributeValue = null
} else if (
state.tableCellAlignToStyle &&
tableCellElement.has(node.tagName) &&
property === 'align'
) {
alignValue = String(value)
continue
} else {
attributeValue = {type: 'Literal', value: String(value)}
}
if (identifierName(property, {jsx: true})) {
attributes.push({
type: 'JSXAttribute',
name: {type: 'JSXIdentifier', name: property},
value: attributeValue
})
} else {
attributes.push({
type: 'JSXSpreadAttribute',
argument: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: {type: 'Literal', value: String(property)},
// @ts-expect-error No need to worry about `style` (which has a
// `JSXExpressionContainer` value) because thats a valid identifier.
value: attributeValue || {type: 'Literal', value: true},
kind: 'init'
}
]
}
})
}
}
}
if (alignValue !== undefined) {
if (!styleProperties) {
styleProperties = []
attributes.push({
type: 'JSXAttribute',
name: {type: 'JSXIdentifier', name: 'style'},
value: {
type: 'JSXExpressionContainer',
expression: {type: 'ObjectExpression', properties: styleProperties}
}
})
}
const cssProperty =
state.stylePropertyNameCase === 'css'
? transformStyleToCssCasing('textAlign')
: 'textAlign'
styleProperties.push({
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: identifierName(cssProperty)
? {type: 'Identifier', name: cssProperty}
: {type: 'Literal', value: cssProperty},
value: {type: 'Literal', value: alignValue},
kind: 'init'
})
}
// Restore parent schema.
state.schema = parentSchema
/** @type {JsxElement} */
const result = {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
attributes,
name: state.createJsxElementName(node.tagName),
selfClosing: children.length === 0
},
closingElement:
children.length > 0
? {
type: 'JSXClosingElement',
name: state.createJsxElementName(node.tagName)
}
: null,
children
}
state.inherit(node, result)
return result
}
/**
* Parse CSS rules as a declaration.
*
* @param {string} value
* CSS text.
* @param {string} tagName
* Element name.
* @returns {Record<string, string>}
* Properties.
*/
function parseStyle(value, tagName) {
try {
return styleToJs(value, {reactCompat: true})
} catch (error) {
const cause = /** @type {Error} */ (error)
const exception = new Error(
'Could not parse `style` attribute on `' + tagName + '`',
{cause}
)
throw exception
}
}
/**
* Transform a DOM casing style object to a CSS casing style object.
*
* @param {Record<string, string>} domCasing
* @returns {Record<string, string>}
*/
function transformStylesToCssCasing(domCasing) {
/** @type {Record<string, string>} */
const cssCasing = {}
/** @type {string} */
let from
for (from in domCasing) {
if (own.call(domCasing, from)) {
cssCasing[transformStyleToCssCasing(from)] = domCasing[from]
}
}
return cssCasing
}
/**
* Transform a DOM casing style property to a CSS casing style property.
*
* @param {string} from
* @returns {string}
*/
function transformStyleToCssCasing(from) {
let to = from.replace(cap, toDash)
// Handle `ms-xxx` -> `-ms-xxx`.
if (to.slice(0, 3) === 'ms-') to = '-' + to
return to
}
/**
* Make `$0` dash cased.
*
* @param {string} $0
* Capitalized ASCII leter.
* @returns {string}
* Dash and lower letter.
*/
function toDash($0) {
return '-' + $0.toLowerCase()
}

View File

@@ -0,0 +1,28 @@
export namespace handlers {
export { comment };
export { ignore as doctype };
export { element };
export { mdxExpression as mdxFlowExpression };
export { mdxJsxElement as mdxJsxFlowElement };
export { mdxJsxElement as mdxJsxTextElement };
export { mdxExpression as mdxTextExpression };
export { mdxjsEsm };
export { root };
export { text };
}
import { comment } from './comment.js';
/**
* Handle a node that is ignored.
*
* @returns {undefined}
* Nothing.
*/
declare function ignore(): undefined;
import { element } from './element.js';
import { mdxExpression } from './mdx-expression.js';
import { mdxJsxElement } from './mdx-jsx-element.js';
import { mdxjsEsm } from './mdxjs-esm.js';
import { root } from './root.js';
import { text } from './text.js';
export {};
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":";;;;;;;;;;;;wBAAsB,cAAc;AAqBpC;;;;;GAKG;AACH,2BAHa,SAAS,CAGF;wBA1BE,cAAc;8BACR,qBAAqB;8BACrB,sBAAsB;yBAC3B,gBAAgB;qBACpB,WAAW;qBACX,WAAW"}

View File

@@ -0,0 +1,28 @@
import {comment} from './comment.js'
import {element} from './element.js'
import {mdxExpression} from './mdx-expression.js'
import {mdxJsxElement} from './mdx-jsx-element.js'
import {mdxjsEsm} from './mdxjs-esm.js'
import {root} from './root.js'
import {text} from './text.js'
export const handlers = {
comment,
doctype: ignore,
element,
mdxFlowExpression: mdxExpression,
mdxJsxFlowElement: mdxJsxElement,
mdxJsxTextElement: mdxJsxElement,
mdxTextExpression: mdxExpression,
mdxjsEsm,
root,
text
}
/**
* Handle a node that is ignored.
*
* @returns {undefined}
* Nothing.
*/
function ignore() {}

View File

@@ -0,0 +1,16 @@
/**
* Turn an MDX expression node into an estree node.
*
* @param {MdxFlowExpression | MdxTextExpression} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer}
* estree expression.
*/
export function mdxExpression(node: MdxFlowExpression | MdxTextExpression, state: State): JsxExpressionContainer;
import type { MdxFlowExpressionHast as MdxFlowExpression } from 'mdast-util-mdx-expression';
import type { MdxTextExpressionHast as MdxTextExpression } from 'mdast-util-mdx-expression';
import type { State } from 'hast-util-to-estree';
import type { JSXExpressionContainer as JsxExpressionContainer } from 'estree-jsx';
//# sourceMappingURL=mdx-expression.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"mdx-expression.d.ts","sourceRoot":"","sources":["mdx-expression.js"],"names":[],"mappings":"AAeA;;;;;;;;;GASG;AACH,oCAPW,iBAAiB,GAAG,iBAAiB,SAErC,KAAK,GAEH,sBAAsB,CA4BlC;gEAzCS,2BAA2B;gEAA3B,2BAA2B;2BACb,qBAAqB;sEANnC,YAAY"}

View File

@@ -0,0 +1,51 @@
/**
* @import {
* JSXEmptyExpression as JsxEmptyExpression,
* JSXExpressionContainer as JsxExpressionContainer
* } from 'estree-jsx'
* @import {Expression} from 'estree'
* @import {
* MdxFlowExpressionHast as MdxFlowExpression,
* MdxTextExpressionHast as MdxTextExpression
* } from 'mdast-util-mdx-expression'
* @import {State} from 'hast-util-to-estree'
*/
import {attachComments} from 'estree-util-attach-comments'
/**
* Turn an MDX expression node into an estree node.
*
* @param {MdxFlowExpression | MdxTextExpression} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer}
* estree expression.
*/
export function mdxExpression(node, state) {
const estree = node.data && node.data.estree
const comments = (estree && estree.comments) || []
/** @type {Expression | JsxEmptyExpression | undefined} */
let expression
if (estree) {
state.comments.push(...comments)
attachComments(estree, estree.comments)
expression =
(estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression) ||
undefined
}
if (!expression) {
expression = {type: 'JSXEmptyExpression'}
state.patch(node, expression)
}
/** @type {JsxExpressionContainer} */
const result = {type: 'JSXExpressionContainer', expression}
state.inherit(node, result)
return result
}

View File

@@ -0,0 +1,17 @@
/**
* Turn an MDX JSX element node into an estree node.
*
* @param {MdxJsxFlowElement | MdxJsxTextElement} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxElement | JsxFragment}
* JSX element or fragment.
*/
export function mdxJsxElement(node: MdxJsxFlowElement | MdxJsxTextElement, state: State): JsxElement | JsxFragment;
import type { MdxJsxFlowElementHast as MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
import type { MdxJsxTextElementHast as MdxJsxTextElement } from 'mdast-util-mdx-jsx';
import type { State } from 'hast-util-to-estree';
import type { JSXElement as JsxElement } from 'estree-jsx';
import type { JSXFragment as JsxFragment } from 'estree-jsx';
//# sourceMappingURL=mdx-jsx-element.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"mdx-jsx-element.d.ts","sourceRoot":"","sources":["mdx-jsx-element.js"],"names":[],"mappings":"AAkBA;;;;;;;;;GASG;AAEH,oCARW,iBAAiB,GAAG,iBAAiB,SAErC,KAAK,GAEH,UAAU,GAAG,WAAW,CA2IpC;gEAxJS,oBAAoB;gEAApB,oBAAoB;2BAJN,qBAAqB;8CAFnC,YAAY;gDAAZ,YAAY"}

View File

@@ -0,0 +1,165 @@
/**
* @import {
* JSXAttribute as JsxAttribute,
* JSXElement as JsxElement,
* JSXFragment as JsxFragment,
* JSXSpreadAttribute as JsxSpreadAttribute
* } from 'estree-jsx'
* @import {Expression} from 'estree'
* @import {State} from 'hast-util-to-estree'
* @import {
* MdxJsxFlowElementHast as MdxJsxFlowElement,
* MdxJsxTextElementHast as MdxJsxTextElement
* } from 'mdast-util-mdx-jsx'
*/
import {attachComments} from 'estree-util-attach-comments'
import {svg} from 'property-information'
/**
* Turn an MDX JSX element node into an estree node.
*
* @param {MdxJsxFlowElement | MdxJsxTextElement} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxElement | JsxFragment}
* JSX element or fragment.
*/
// eslint-disable-next-line complexity
export function mdxJsxElement(node, state) {
const parentSchema = state.schema
let schema = parentSchema
const attributes = node.attributes || []
let index = -1
if (
node.name &&
parentSchema.space === 'html' &&
node.name.toLowerCase() === 'svg'
) {
schema = svg
state.schema = schema
}
const children = state.all(node)
/** @type {Array<JsxAttribute | JsxSpreadAttribute>} */
const jsxAttributes = []
while (++index < attributes.length) {
const attribute = attributes[index]
const value = attribute.value
/** @type {JsxAttribute['value']} */
let attributeValue
if (attribute.type === 'mdxJsxAttribute') {
if (value === null || value === undefined) {
attributeValue = null
// Empty.
}
// `MdxJsxAttributeValueExpression`.
else if (typeof value === 'object') {
const estree = value.data && value.data.estree
const comments = (estree && estree.comments) || []
/** @type {Expression | undefined} */
let expression
if (estree) {
state.comments.push(...comments)
attachComments(estree, estree.comments)
// Should exist.
/* c8 ignore next 5 */
expression =
(estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression) ||
undefined
}
attributeValue = {
type: 'JSXExpressionContainer',
expression: expression || {type: 'JSXEmptyExpression'}
}
state.inherit(value, attributeValue)
}
// Anything else.
else {
attributeValue = {type: 'Literal', value: String(value)}
}
/** @type {JsxAttribute} */
const jsxAttribute = {
type: 'JSXAttribute',
name: state.createJsxAttributeName(attribute.name),
value: attributeValue
}
state.inherit(attribute, jsxAttribute)
jsxAttributes.push(jsxAttribute)
}
// MdxJsxExpressionAttribute.
else {
const estree = attribute.data && attribute.data.estree
const comments = (estree && estree.comments) || []
/** @type {JsxSpreadAttribute['argument'] | undefined} */
let argumentValue
if (estree) {
state.comments.push(...comments)
attachComments(estree, estree.comments)
// Should exist.
/* c8 ignore next 10 */
argumentValue =
(estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression &&
estree.body[0].expression.type === 'ObjectExpression' &&
estree.body[0].expression.properties &&
estree.body[0].expression.properties[0] &&
estree.body[0].expression.properties[0].type === 'SpreadElement' &&
estree.body[0].expression.properties[0].argument) ||
undefined
}
/** @type {JsxSpreadAttribute} */
const jsxAttribute = {
type: 'JSXSpreadAttribute',
argument: argumentValue || {type: 'ObjectExpression', properties: []}
}
state.inherit(attribute, jsxAttribute)
jsxAttributes.push(jsxAttribute)
}
}
// Restore parent schema.
state.schema = parentSchema
/** @type {JsxElement | JsxFragment} */
const result = node.name
? {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
attributes: jsxAttributes,
name: state.createJsxElementName(node.name),
selfClosing: children.length === 0
},
closingElement:
children.length > 0
? {
type: 'JSXClosingElement',
name: state.createJsxElementName(node.name)
}
: null,
children
}
: {
type: 'JSXFragment',
openingFragment: {type: 'JSXOpeningFragment'},
closingFragment: {type: 'JSXClosingFragment'},
children
}
state.inherit(node, result)
return result
}

View File

@@ -0,0 +1,14 @@
/**
* Handle an MDX ESM node.
*
* @param {MdxjsEsm} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {undefined}
* Nothing.
*/
export function mdxjsEsm(node: MdxjsEsm, state: State): undefined;
import type { MdxjsEsmHast as MdxjsEsm } from 'mdast-util-mdxjs-esm';
import type { State } from 'hast-util-to-estree';
//# sourceMappingURL=mdxjs-esm.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"mdxjs-esm.d.ts","sourceRoot":"","sources":["mdxjs-esm.js"],"names":[],"mappings":"AAOA;;;;;;;;;GASG;AACH,+BAPW,QAAQ,SAER,KAAK,GAEH,SAAS,CAYrB;8CAzB0C,sBAAsB;2BACzC,qBAAqB"}

View File

@@ -0,0 +1,27 @@
/**
* @import {MdxjsEsmHast as MdxjsEsm} from 'mdast-util-mdxjs-esm'
* @import {State} from 'hast-util-to-estree'
*/
import {attachComments} from 'estree-util-attach-comments'
/**
* Handle an MDX ESM node.
*
* @param {MdxjsEsm} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {undefined}
* Nothing.
*/
export function mdxjsEsm(node, state) {
const estree = node.data && node.data.estree
const comments = (estree && estree.comments) || []
if (estree) {
state.comments.push(...comments)
attachComments(estree, comments)
state.esm.push(...estree.body)
}
}

View File

@@ -0,0 +1,15 @@
/**
* Turn a hast root node into an estree node.
*
* @param {HastRoot} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxFragment}
* estree JSX fragment.
*/
export function root(node: HastRoot, state: State): JsxFragment;
import type { Root as HastRoot } from 'hast';
import type { State } from 'hast-util-to-estree';
import type { JSXFragment as JsxFragment } from 'estree-jsx';
//# sourceMappingURL=root.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"root.d.ts","sourceRoot":"","sources":["root.js"],"names":[],"mappings":"AAcA;;;;;;;;;GASG;AACH,2BAPW,QAAQ,SAER,KAAK,GAEH,WAAW,CAqCvB;sCAjDkC,MAAM;2BADjB,qBAAqB;gDADnC,YAAY"}

View File

@@ -0,0 +1,59 @@
/**
* @import {
* JSXElement as JsxElement,
* JSXExpressionContainer as JsxExpressionContainer,
* JSXFragment as JsxFragment,
* JSXSpreadChild as JsxSpreadChild,
* JSXText as JsxText,
* } from 'estree-jsx'
* @import {State} from 'hast-util-to-estree'
* @import {Root as HastRoot} from 'hast'
*/
import {whitespace} from 'hast-util-whitespace'
/**
* Turn a hast root node into an estree node.
*
* @param {HastRoot} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxFragment}
* estree JSX fragment.
*/
export function root(node, state) {
const children = state.all(node)
/** @type {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>} */
const cleanChildren = []
let index = -1
/** @type {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText> | undefined} */
let queue
// Remove surrounding whitespace nodes from the fragment.
while (++index < children.length) {
const child = children[index]
if (
child.type === 'JSXExpressionContainer' &&
child.expression.type === 'Literal' &&
whitespace(String(child.expression.value))
) {
if (queue) queue.push(child)
} else {
if (queue) cleanChildren.push(...queue)
cleanChildren.push(child)
queue = []
}
}
/** @type {JsxFragment} */
const result = {
type: 'JSXFragment',
openingFragment: {type: 'JSXOpeningFragment'},
closingFragment: {type: 'JSXClosingFragment'},
children: cleanChildren
}
state.inherit(node, result)
return result
}

View File

@@ -0,0 +1,15 @@
/**
* Turn a hast text node into an estree node.
*
* @param {HastText} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer | undefined}
* JSX expression.
*/
export function text(node: HastText, state: State): JsxExpressionContainer | undefined;
import type { Text as HastText } from 'hast';
import type { State } from 'hast-util-to-estree';
import type { JSXExpressionContainer as JsxExpressionContainer } from 'estree-jsx';
//# sourceMappingURL=text.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["text.js"],"names":[],"mappings":"AAUA;;;;;;;;;GASG;AACH,2BAPW,QAAQ,SAER,KAAK,GAEH,sBAAsB,GAAG,SAAS,CAe9C;sCA5BkC,MAAM;2BADjB,qBAAqB;sEAFsB,YAAY"}

View File

@@ -0,0 +1,33 @@
/**
* @import {JSXExpressionContainer as JsxExpressionContainer} from 'estree-jsx'
* @import {Literal} from 'estree'
* @import {State} from 'hast-util-to-estree'
* @import {Text as HastText} from 'hast'
*/
// Make VS Code show references to the above types.
''
/**
* Turn a hast text node into an estree node.
*
* @param {HastText} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer | undefined}
* JSX expression.
*/
export function text(node, state) {
const value = String(node.value || '')
if (value) {
/** @type {Literal} */
const result = {type: 'Literal', value}
state.inherit(node, result)
/** @type {JsxExpressionContainer} */
const container = {type: 'JSXExpressionContainer', expression: result}
state.patch(node, container)
return container
}
}

View File

@@ -0,0 +1,55 @@
/**
* Transform a hast tree (with embedded MDX nodes) into an estree.
*
* ##### Notes
*
* ###### Comments
*
* Comments are attached to the tree in their neighbouring nodes (`recast`,
* `babel` style) and also added as a `comments` array on the program node
* (`espree` style).
* You may have to do `program.comments = undefined` for certain compilers.
*
* ###### Frameworks
*
* There are differences between what JSX frameworks accept, such as whether they
* accept `class` or `className`, or `background-color` or `backgroundColor`.
*
* For JSX components written in MDX, the author has to be aware of this
* difference and write code accordingly.
* For hast elements transformed by this project, this will be handled through
* options.
*
* | Framework | `elementAttributeNameCase` | `stylePropertyNameCase` |
* | --------- | -------------------------- | ----------------------- |
* | Preact | `'html'` | `'dom'` |
* | React | `'react'` | `'dom'` |
* | Solid | `'html'` | `'css'` |
* | Vue | `'html'` | `'dom'` |
*
* @param {HastNodes} tree
* hast tree.
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Program}
* estree program node.
*
* The programs last child in `body` is most likely an `ExpressionStatement`,
* whose expression is a `JSXFragment` or a `JSXElement`.
*
* Typically, there is only one node in `body`, however, this utility also
* supports embedded MDX nodes in the HTML (when `mdast-util-mdx` is used
* with mdast to parse markdown before passing its nodes through to hast).
* When MDX ESM import/exports are used, those nodes are added before the
* fragment or element in body.
*
* There arent many great estree serializers out there that support JSX.
* To do that, you can use `estree-util-to-js`.
* Or, use `estree-util-build-jsx` to turn JSX into function calls, and then
* serialize with whatever (`astring`, `escodegen`).
*/
export function toEstree(tree: HastNodes, options?: Options | null | undefined): Program;
import type { Nodes as HastNodes } from 'hast';
import type { Options } from 'hast-util-to-estree';
import type { Program } from 'estree';
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,+BArBW,SAAS,YAET,OAAO,GAAG,IAAI,GAAG,SAAS,GAExB,OAAO,CAgDnB;wCAtFoC,MAAM;6BADjB,qBAAqB;6BADA,QAAQ"}

View File

@@ -0,0 +1,93 @@
/**
* @import {} from 'mdast-util-mdx-expression'
* @import {} from 'mdast-util-mdx-jsx'
* @import {} from 'mdast-util-mdxjs-esm'
* @import {ExpressionStatement, Program} from 'estree'
* @import {Options} from 'hast-util-to-estree'
* @import {Nodes as HastNodes} from 'hast'
*/
import {createState} from './state.js'
/**
* Transform a hast tree (with embedded MDX nodes) into an estree.
*
* ##### Notes
*
* ###### Comments
*
* Comments are attached to the tree in their neighbouring nodes (`recast`,
* `babel` style) and also added as a `comments` array on the program node
* (`espree` style).
* You may have to do `program.comments = undefined` for certain compilers.
*
* ###### Frameworks
*
* There are differences between what JSX frameworks accept, such as whether they
* accept `class` or `className`, or `background-color` or `backgroundColor`.
*
* For JSX components written in MDX, the author has to be aware of this
* difference and write code accordingly.
* For hast elements transformed by this project, this will be handled through
* options.
*
* | Framework | `elementAttributeNameCase` | `stylePropertyNameCase` |
* | --------- | -------------------------- | ----------------------- |
* | Preact | `'html'` | `'dom'` |
* | React | `'react'` | `'dom'` |
* | Solid | `'html'` | `'css'` |
* | Vue | `'html'` | `'dom'` |
*
* @param {HastNodes} tree
* hast tree.
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Program}
* estree program node.
*
* The programs last child in `body` is most likely an `ExpressionStatement`,
* whose expression is a `JSXFragment` or a `JSXElement`.
*
* Typically, there is only one node in `body`, however, this utility also
* supports embedded MDX nodes in the HTML (when `mdast-util-mdx` is used
* with mdast to parse markdown before passing its nodes through to hast).
* When MDX ESM import/exports are used, those nodes are added before the
* fragment or element in body.
*
* There arent many great estree serializers out there that support JSX.
* To do that, you can use `estree-util-to-js`.
* Or, use `estree-util-build-jsx` to turn JSX into function calls, and then
* serialize with whatever (`astring`, `escodegen`).
*/
export function toEstree(tree, options) {
const state = createState(options || {})
let result = state.handle(tree)
const body = state.esm
if (result) {
if (result.type !== 'JSXFragment' && result.type !== 'JSXElement') {
result = {
type: 'JSXFragment',
openingFragment: {type: 'JSXOpeningFragment'},
closingFragment: {type: 'JSXClosingFragment'},
children: [result]
}
state.patch(tree, result)
}
/** @type {ExpressionStatement} */
const statement = {type: 'ExpressionStatement', expression: result}
state.patch(tree, statement)
body.push(statement)
}
/** @type {Program} */
const program = {
type: 'Program',
body,
sourceType: 'module',
comments: state.comments
}
state.patch(tree, program)
return program
}

View File

@@ -0,0 +1,140 @@
/**
* Create a state from options.
*
* @param {Options} options
* Configuration.
* @returns {State}
* Info passed around about the current state.
*/
export function createState(options: Options): State;
/**
* Specify casing to use for attribute names.
*
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
* React casing is for example `className`, `strokeLinecap`, `xmlLang`.
*/
export type ElementAttributeNameCase = "html" | "react";
/**
* Turn a hast node into an estree node.
*/
export type Handle = (node: any, state: State) => JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined;
/**
* Configuration.
*/
export type Options = {
/**
* Specify casing to use for attribute names (default: `'react'`).
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
*/
elementAttributeNameCase?: ElementAttributeNameCase | null | undefined;
/**
* Custom handlers (optional).
*/
handlers?: Record<string, Handle | null | undefined> | null | undefined;
/**
* Which space the document is in (default: `'html'`).
*
* When an `<svg>` element is found in the HTML space, this package already
* automatically switches to and from the SVG space when entering and exiting
* it.
*/
space?: Space | null | undefined;
/**
* Specify casing to use for property names in `style` objects (default: `'dom'`).
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
*/
stylePropertyNameCase?: StylePropertyNameCase | null | undefined;
/**
* Turn obsolete `align` props on `td` and `th` into CSS `style` props
* (default: `true`).
*/
tableCellAlignToStyle?: boolean | null | undefined;
};
/**
* Namespace.
*/
export type Space = "html" | "svg";
/**
* Casing to use for property names in `style` objects.
*
* CSS casing is for example `background-color` and `-webkit-line-clamp`.
* DOM casing is for example `backgroundColor` and `WebkitLineClamp`.
*/
export type StylePropertyNameCase = "css" | "dom";
/**
* Info passed around about the current state.
*/
export type State = {
/**
* Transform children of a hast parent to estree.
*/
all: (parent: HastParents) => Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>;
/**
* List of estree comments.
*/
comments: Array<Comment>;
/**
* Create a JSX attribute name.
*/
createJsxAttributeName: (name: string) => JsxIdentifier | JsxNamespacedName;
/**
* Create a JSX element name.
*/
createJsxElementName: (name: string) => JsxIdentifier | JsxMemberExpression | JsxNamespacedName;
/**
* Casing to use for attribute names.
*/
elementAttributeNameCase: ElementAttributeNameCase;
/**
* List of top-level estree nodes.
*/
esm: Array<Directive | ModuleDeclaration | Statement>;
/**
* Transform a hast node to estree.
*/
handle: (node: any) => JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined;
/**
* Take positional info and data from `from` (use `patch` if you dont want data).
*/
inherit: (from: HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute, to: Comment | EstreeNode) => undefined;
/**
* Take positional info from `from` (use `inherit` if you also want data).
*/
patch: (from: HastNodes, to: Comment | EstreeNode) => undefined;
/**
* Current schema.
*/
schema: Schema;
/**
* Casing to use for property names in `style` objects.
*/
stylePropertyNameCase: StylePropertyNameCase;
/**
* Turn obsolete `align` props on `td` and `th` into CSS `style` props.
*/
tableCellAlignToStyle: boolean;
};
import type { JSXElement as JsxElement } from 'estree-jsx';
import type { JSXExpressionContainer as JsxExpressionContainer } from 'estree-jsx';
import type { JSXFragment as JsxFragment } from 'estree-jsx';
import type { JSXSpreadChild as JsxSpreadChild } from 'estree-jsx';
import type { JSXText as JsxText } from 'estree-jsx';
import type { Parents as HastParents } from 'hast';
import type { Comment } from 'estree';
import type { JSXIdentifier as JsxIdentifier } from 'estree-jsx';
import type { JSXNamespacedName as JsxNamespacedName } from 'estree-jsx';
import type { JSXMemberExpression as JsxMemberExpression } from 'estree-jsx';
import type { Directive } from 'estree';
import type { ModuleDeclaration } from 'estree';
import type { Statement } from 'estree';
import type { Nodes as HastNodes } from 'hast';
import type { MdxJsxAttribute } from 'mdast-util-mdx-jsx';
import type { MdxJsxAttributeValueExpression } from 'mdast-util-mdx-jsx';
import type { MdxJsxExpressionAttribute } from 'mdast-util-mdx-jsx';
import type { Node as EstreeNode } from 'estree';
import type { Schema } from 'property-information';
//# sourceMappingURL=state.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["state.js"],"names":[],"mappings":"AAoHA;;;;;;;GAOG;AAEH,qCANW,OAAO,GAEL,KAAK,CAsCjB;;;;;;;uCA7IY,MAAM,GAAG,OAAO;;;;4BAQlB,GAAG,SAEH,KAAK,KAEH,UAAU,GAAG,sBAAsB,GAAG,WAAW,GAAG,cAAc,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;;+BAK9F,wBAAwB,GAAG,IAAI,GAAG,SAAS;;;;eAK3C,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,IAAI,GAAG,SAAS;;;;;;;;YAE5D,KAAK,GAAG,IAAI,GAAG,SAAS;;;;;;;4BAMxB,qBAAqB,GAAG,IAAI,GAAG,SAAS;;;;;4BAKxC,OAAO,GAAG,IAAI,GAAG,SAAS;;;;;oBAI3B,MAAM,GAAG,KAAK;;;;;;;oCAGd,KAAK,GAAG,KAAK;;;;;;;;SAQZ,CAAC,MAAM,EAAE,WAAW,KAAK,KAAK,CAAC,UAAU,GAAG,sBAAsB,GAAG,WAAW,GAAG,cAAc,GAAG,OAAO,CAAC;;;;cAE5G,KAAK,CAAC,OAAO,CAAC;;;;4BAEd,CAAC,IAAI,EAAE,MAAM,KAAK,aAAa,GAAG,iBAAiB;;;;0BAEnD,CAAC,IAAI,EAAE,MAAM,KAAK,aAAa,GAAG,mBAAmB,GAAG,iBAAiB;;;;8BAEzE,wBAAwB;;;;SAExB,KAAK,CAAC,SAAS,GAAG,iBAAiB,GAAG,SAAS,CAAC;;;;YAEhD,CAAC,IAAI,EAAE,GAAG,KAAK,UAAU,GAAG,sBAAsB,GAAG,WAAW,GAAG,cAAc,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS;;;;aAE9G,CAAC,IAAI,EAAE,SAAS,GAAG,eAAe,GAAG,8BAA8B,GAAG,yBAAyB,EAAE,EAAE,EAAE,OAAO,GAAG,UAAU,KAAK,SAAS;;;;WAEvI,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,OAAO,GAAG,UAAU,KAAK,SAAS;;;;YAExD,MAAM;;;;2BAEN,qBAAqB;;;;2BAErB,OAAO;;8CAhFX,YAAY;sEAAZ,YAAY;gDAAZ,YAAY;sDAAZ,YAAY;wCAAZ,YAAY;4CAGuC,MAAM;6BAFoB,QAAQ;oDADrF,YAAY;4DAAZ,YAAY;gEAAZ,YAAY;+BACiE,QAAQ;uCAAR,QAAQ;+BAAR,QAAQ;wCAElC,MAAM;qCAD0B,oBAAoB;oDAApB,oBAAoB;+CAApB,oBAAoB;wCAD1B,QAAQ;4BAGtE,sBAAsB"}

View File

@@ -0,0 +1,354 @@
/**
* @import {
* JSXElement as JsxElement,
* JSXExpressionContainer as JsxExpressionContainer,
* JSXFragment as JsxFragment,
* JSXIdentifier as JsxIdentifier,
* JSXMemberExpression as JsxMemberExpression,
* JSXNamespacedName as JsxNamespacedName,
* JSXSpreadChild as JsxSpreadChild,
* JSXText as JsxText,
* } from 'estree-jsx'
* @import {Comment, Directive, ModuleDeclaration, Node as EstreeNode, Statement} from 'estree'
* @import {MdxJsxAttribute, MdxJsxAttributeValueExpression, MdxJsxExpressionAttribute} from 'mdast-util-mdx-jsx'
* @import {Nodes as HastNodes, Parents as HastParents} from 'hast'
* @import {Schema} from 'property-information'
*/
/**
* @typedef {'html' | 'react'} ElementAttributeNameCase
* Specify casing to use for attribute names.
*
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
* React casing is for example `className`, `strokeLinecap`, `xmlLang`.
*
* @callback Handle
* Turn a hast node into an estree node.
* @param {any} node
* Expected hast node.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined}
* estree node.
*
* @typedef Options
* Configuration.
* @property {ElementAttributeNameCase | null | undefined} [elementAttributeNameCase='react']
* Specify casing to use for attribute names (default: `'react'`).
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers={}]
* Custom handlers (optional).
* @property {Space | null | undefined} [space='html']
* Which space the document is in (default: `'html'`).
*
* When an `<svg>` element is found in the HTML space, this package already
* automatically switches to and from the SVG space when entering and exiting
* it.
* @property {StylePropertyNameCase | null | undefined} [stylePropertyNameCase='dom']
* Specify casing to use for property names in `style` objects (default: `'dom'`).
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
* @property {boolean | null | undefined} [tableCellAlignToStyle=true]
* Turn obsolete `align` props on `td` and `th` into CSS `style` props
* (default: `true`).
*
* @typedef {'html' | 'svg'} Space
* Namespace.
*
* @typedef {'css' | 'dom'} StylePropertyNameCase
* Casing to use for property names in `style` objects.
*
* CSS casing is for example `background-color` and `-webkit-line-clamp`.
* DOM casing is for example `backgroundColor` and `WebkitLineClamp`.
*
* @typedef State
* Info passed around about the current state.
* @property {(parent: HastParents) => Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>} all
* Transform children of a hast parent to estree.
* @property {Array<Comment>} comments
* List of estree comments.
* @property {(name: string) => JsxIdentifier | JsxNamespacedName} createJsxAttributeName
* Create a JSX attribute name.
* @property {(name: string) => JsxIdentifier | JsxMemberExpression | JsxNamespacedName} createJsxElementName
* Create a JSX element name.
* @property {ElementAttributeNameCase} elementAttributeNameCase
* Casing to use for attribute names.
* @property {Array<Directive | ModuleDeclaration | Statement>} esm
* List of top-level estree nodes.
* @property {(node: any) => JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined} handle
* Transform a hast node to estree.
* @property {(from: HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute, to: Comment | EstreeNode) => undefined} inherit
* Take positional info and data from `from` (use `patch` if you dont want data).
* @property {(from: HastNodes, to: Comment | EstreeNode) => undefined} patch
* Take positional info from `from` (use `inherit` if you also want data).
* @property {Schema} schema
* Current schema.
* @property {StylePropertyNameCase} stylePropertyNameCase
* Casing to use for property names in `style` objects.
* @property {boolean} tableCellAlignToStyle
* Turn obsolete `align` props on `td` and `th` into CSS `style` props.
*/
import {ok as assert} from 'devlop'
import {html, svg} from 'property-information'
import {position} from 'unist-util-position'
import {zwitch} from 'zwitch'
import {handlers} from './handlers/index.js'
const own = {}.hasOwnProperty
// `react-dom` triggers a warning for *any* white space in tables.
// To follow GFM, `mdast-util-to-hast` injects line endings between elements.
// Other tools might do so too, but they dont do here, so we remove all of
// that.
//
// See: <https://github.com/facebook/react/pull/7081>.
// See: <https://github.com/facebook/react/pull/7515>.
// See: <https://github.com/remarkjs/remark-react/issues/64>.
// See: <https://github.com/rehypejs/rehype-react/pull/29>.
// See: <https://github.com/rehypejs/rehype-react/pull/32>.
// See: <https://github.com/rehypejs/rehype-react/pull/45>.
// See: <https://github.com/mdx-js/mdx/issues/2000>
const tableElements = new Set(['table', 'tbody', 'thead', 'tfoot', 'tr'])
/**
* Create a state from options.
*
* @param {Options} options
* Configuration.
* @returns {State}
* Info passed around about the current state.
*/
export function createState(options) {
/** @type {Handle} */
const one = zwitch('type', {
invalid,
unknown,
handlers: {...handlers, ...options.handlers}
})
return {
// Current space.
elementAttributeNameCase: options.elementAttributeNameCase || 'react',
schema: options.space === 'svg' ? svg : html,
stylePropertyNameCase: options.stylePropertyNameCase || 'dom',
tableCellAlignToStyle: options.tableCellAlignToStyle !== false,
// Results.
comments: [],
esm: [],
// Useful functions.
all,
createJsxAttributeName,
createJsxElementName,
handle,
inherit,
patch
}
/**
* @this {State}
* @param {any} node
* @returns {JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined}
*/
function handle(node) {
return one(node, this)
}
}
/**
* Crash on an invalid value.
*
* @param {unknown} value
* Non-node.
* @returns {never}
* Nothing (crashes).
*/
function invalid(value) {
throw new Error('Cannot handle value `' + value + '`, expected node')
}
/**
* Crash on an unknown node.
*
* @param {unknown} node
* Unknown node.
* @returns {never}
* Nothing (crashes).
*/
function unknown(node) {
assert(node && typeof node === 'object')
assert('type' in node)
throw new Error('Cannot handle unknown node `' + node.type + '`')
}
/**
* @this {State} state
* Info passed around about the current state.
* @param {HastParents} parent
* hast node whose children to transform.
* @returns {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>}
* estree nodes.
*/
function all(parent) {
const children = parent.children || []
let index = -1
/** @type {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>} */
const results = []
const ignoreLineBreak =
this.schema.space === 'html' &&
parent.type === 'element' &&
tableElements.has(parent.tagName.toLowerCase())
while (++index < children.length) {
const child = children[index]
if (ignoreLineBreak && child.type === 'text' && child.value === '\n') {
continue
}
const result = this.handle(child)
if (Array.isArray(result)) {
results.push(...result)
} else if (result) {
results.push(result)
}
}
return results
}
/**
* Take positional info and data from `hast`.
*
* Use `patch` if you dont want data.
*
* @param {HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute} from
* hast node to take positional info and data from.
* @param {Comment | EstreeNode} to
* estree node to add positional info and data to.
* @returns {undefined}
* Nothing.
*/
function inherit(from, to) {
const left = /** @type {Record<string, unknown> | undefined} */ (from.data)
/** @type {Record<string, unknown> | undefined} */
let right
/** @type {string} */
let key
patch(from, to)
if (left) {
for (key in left) {
if (own.call(left, key) && key !== 'estree') {
if (!right) right = {}
right[key] = left[key]
}
}
if (right) {
// @ts-expect-error `esast` extension.
to.data = right
}
}
}
/**
* Take positional info from `from`.
*
* Use `inherit` if you also want data.
*
* @param {HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute} from
* hast node to take positional info from.
* @param {Comment | EstreeNode} to
* estree node to add positional info to.
* @returns {undefined}
* Nothing.
*/
function patch(from, to) {
const p = position(from)
if (p && p.start.offset !== undefined && p.end.offset !== undefined) {
// @ts-expect-error acorn-style.
to.start = p.start.offset
// @ts-expect-error acorn-style.
to.end = p.end.offset
to.loc = {
start: {line: p.start.line, column: p.start.column - 1},
end: {line: p.end.line, column: p.end.column - 1}
}
to.range = [p.start.offset, p.end.offset]
}
}
/**
* Create a JSX attribute name.
*
* @param {string} name
* @returns {JsxIdentifier | JsxNamespacedName}
*/
function createJsxAttributeName(name) {
const node = createJsxNameFromString(name)
// MDX never generates this.
/* c8 ignore next 3 */
if (node.type === 'JSXMemberExpression') {
throw new Error('Member expressions in attribute names are not supported')
}
return node
}
/**
* Create a JSX element name.
*
* @param {string} name
* @returns {JsxIdentifier | JsxMemberExpression | JsxNamespacedName}
*/
function createJsxElementName(name) {
return createJsxNameFromString(name)
}
/**
* Create a JSX name from a string.
*
* @param {string} name
* Name.
* @returns {JsxIdentifier | JsxMemberExpression | JsxNamespacedName}
* Node.
*/
function createJsxNameFromString(name) {
if (name.includes('.')) {
const names = name.split('.')
let part = names.shift()
assert(part, 'Expected `part` to be defined')
/** @type {JsxIdentifier | JsxMemberExpression} */
let node = {type: 'JSXIdentifier', name: part}
while ((part = names.shift())) {
node = {
type: 'JSXMemberExpression',
object: node,
property: {type: 'JSXIdentifier', name: part}
}
}
return node
}
if (name.includes(':')) {
const parts = name.split(':')
return {
type: 'JSXNamespacedName',
namespace: {type: 'JSXIdentifier', name: parts[0]},
name: {type: 'JSXIdentifier', name: parts[1]}
}
}
return {type: 'JSXIdentifier', name}
}

View File

@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) Titus Wormer <tituswormer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,116 @@
{
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"bugs": "https://github.com/syntax-tree/hast-util-to-estree/issues",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"dependencies": {
"@types/estree": "^1.0.0",
"@types/estree-jsx": "^1.0.0",
"@types/hast": "^3.0.0",
"comma-separated-tokens": "^2.0.0",
"devlop": "^1.0.0",
"estree-util-attach-comments": "^3.0.0",
"estree-util-is-identifier-name": "^3.0.0",
"hast-util-whitespace": "^3.0.0",
"mdast-util-mdx-expression": "^2.0.0",
"mdast-util-mdx-jsx": "^3.0.0",
"mdast-util-mdxjs-esm": "^2.0.0",
"property-information": "^7.0.0",
"space-separated-tokens": "^2.0.0",
"style-to-js": "^1.0.0",
"unist-util-position": "^5.0.0",
"zwitch": "^2.0.0"
},
"description": "hast utility to transform to estree (JavaScript AST) JSX",
"devDependencies": {
"@types/node": "^22.0.0",
"acorn-jsx": "^5.0.0",
"c8": "^10.0.0",
"esast-util-from-js": "^2.0.0",
"estree-util-to-js": "^2.0.0",
"estree-walker": "^3.0.0",
"hastscript": "^9.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-mdx": "^3.0.0",
"mdast-util-to-hast": "^13.0.0",
"micromark-extension-mdxjs": "^3.0.0",
"prettier": "^3.0.0",
"remark-cli": "^12.0.0",
"remark-preset-wooorm": "^11.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unist-util-visit": "^5.0.0",
"xo": "^0.60.0"
},
"exports": "./index.js",
"files": [
"index.d.ts.map",
"index.d.ts",
"index.js",
"lib/"
],
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"keywords": [
"change",
"ecmascript",
"estree",
"hast-util",
"hast",
"html",
"javascript",
"jsx",
"mdx",
"rehype",
"transform",
"unist",
"utility",
"util"
],
"license": "MIT",
"name": "hast-util-to-estree",
"prettier": {
"bracketSpacing": false,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
},
"remarkConfig": {
"plugins": [
"remark-preset-wooorm"
]
},
"repository": "syntax-tree/hast-util-to-estree",
"scripts": {
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark --frail --output --quiet -- . && prettier --log-level warn --write -- . && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --100 --reporter lcov -- npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"sideEffects": false,
"typeCoverage": {
"atLeast": 100,
"ignoreFiles#": "needed `any`",
"ignoreFiles": [
"lib/state.d.ts",
"lib/state.js"
],
"strict": true
},
"type": "module",
"version": "3.1.3",
"xo": {
"prettier": true,
"rules": {
"logical-assignment-operators": "off",
"unicorn/prefer-code-point": "off",
"unicorn/prefer-string-replace-all": "off"
}
}
}

View File

@@ -0,0 +1,452 @@
# hast-util-to-estree
[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[hast][github-hast] utility to transform to [estree][github-estree] (JSX).
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`toEstree(tree[, options])`](#toestreetree-options)
* [`defaultHandlers`](#defaulthandlers)
* [`ElementAttributeNameCase`](#elementattributenamecase)
* [`Handle`](#handle)
* [`Options`](#options)
* [`Space`](#space)
* [`State`](#state)
* [`StylePropertyNameCase`](#stylepropertynamecase)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package is a utility that takes a
[hast][github-hast] (HTML)
syntax tree as input and turns it into an
[estree][github-estree] (JavaScript)
syntax tree (with a JSX extension).
This package also supports embedded MDX nodes.
## When should I use this?
This project is useful when you want to embed HTML as JSX inside JS while
working with syntax trees.
This is used in [MDX][mdxjs].
## Install
This package is [ESM only][github-gist-esm].
In Node.js (version 16+), install with [npm][npmjs-install]:
```sh
npm install hast-util-to-estree
```
In Deno with [`esm.sh`][esmsh]:
```js
import {toEstree} from 'https://esm.sh/hast-util-to-estree@3'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import {toEstree} from 'https://esm.sh/hast-util-to-estree@3?bundle'
</script>
```
## Use
Say our module `example.html` contains:
```html
<!doctype html>
<html lang=en>
<title>Hi!</title>
<link rel=stylesheet href=index.css>
<h1>Hello, world!</h1>
<a download style="width:1;height:10px"></a>
<!--commentz-->
<svg xmlns="http://www.w3.org/2000/svg">
<title>SVG `&lt;ellipse&gt;` element</title>
<ellipse
cx="120"
cy="70"
rx="100"
ry="50"
/>
</svg>
<script src="index.js"></script>
```
…and our module `example.js` looks as follows:
```js
import fs from 'node:fs/promises'
import {jsx, toJs} from 'estree-util-to-js'
import {fromHtml} from 'hast-util-from-html'
import {toEstree} from 'hast-util-to-estree'
const hast = fromHtml(await fs.readFile('example.html'))
const estree = toEstree(hast)
console.log(toJs(estree, {handlers: jsx}).value)
```
…now running `node example.js` (and prettier) yields:
```js
/* Commentz */
;<>
<html lang="en">
<head>
<title>{'Hi!'}</title>
{'\n'}
<link rel="stylesheet" href="index.css" />
{'\n'}
</head>
<body>
<h1>{'Hello, world!'}</h1>
{'\n'}
<a
download
style={{
width: '1',
height: '10px'
}}
/>
{'\n'}
{}
{'\n'}
<svg xmlns="http://www.w3.org/2000/svg">
{'\n '}
<title>{'SVG `<ellipse>` element'}</title>
{'\n '}
<ellipse cx="120" cy="70" rx="100" ry="50" />
{'\n'}
</svg>
{'\n'}
<script src="index.js" />
{'\n'}
</body>
</html>
</>
```
## API
This package exports the identifiers [`defaultHandlers`][api-default-handlers]
and [`toEstree`][api-to-estree].
There is no default export.
### `toEstree(tree[, options])`
Transform a hast tree (with embedded MDX nodes) into an estree (with JSX
nodes).
##### Notes
###### Comments
Comments are attached to the tree in their neighbouring nodes (`recast`,
`babel` style) and also added as a `comments` array on the program node
(`espree` style).
You may have to do `program.comments = undefined` for certain compilers.
###### Frameworks
There are differences between what JSX frameworks accept, such as whether they
accept `class` or `className`, or `background-color` or `backgroundColor`.
For JSX components written in MDX, the author has to be aware of this
difference and write code accordingly.
For hast elements transformed by this project, this will be handled through
options.
| Framework | `elementAttributeNameCase` | `stylePropertyNameCase` |
| --------- | -------------------------- | ----------------------- |
| Preact | `'html'` | `'dom'` |
| React | `'react'` | `'dom'` |
| Solid | `'html'` | `'css'` |
| Vue | `'html'` | `'dom'` |
###### Parameters
* `tree` ([`HastNode`][github-hast-nodes])
— hast tree
* `options` ([`Options`][api-options], optional)
— configuration
###### Returns
estree program node ([`Program`][github-estree-program]).
The programs last child in `body` is most likely an `ExpressionStatement`,
whose expression is a `JSXFragment` or a `JSXElement`.
Typically, there is only one node in `body`, however, this utility also supports
embedded MDX nodes in the HTML
(when [`mdast-util-mdx`][github-mdast-util-mdx] is used with mdast to parse
markdown before passing its nodes through to hast).
When MDX ESM import/exports are used, those nodes are added before the fragment
or element in body.
There arent many great estree serializers out there that support JSX.
To do that, you can use [`estree-util-to-js`][github-estree-util-to-js].
Or, use [`estree-util-build-jsx`][github-estree-util-build-jsx]
to turn JSX into function calls,
and then serialize with whatever (`astring`, `escodegen`).
### `defaultHandlers`
Default handlers for elements (`Record<string, Handle>`).
Each key is a node type, each value is a [`Handle`][api-handle].
### `ElementAttributeNameCase`
Specify casing to use for attribute names (TypeScript type).
HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
React casing is for example `className`, `strokeLinecap`, `xmlLang`.
###### Type
```ts
type ElementAttributeNameCase = 'html' | 'react'
```
### `Handle`
Turn a hast node into an estree node (TypeScript type).
###### Parameters
* `node` ([`HastNode`][github-hast-nodes])
— expected hast node
* `state` ([`State`][api-state])
— info passed around about the current state
###### Returns
JSX child (`JsxChild`, optional).
You can also add more results to `state.esm` and `state.comments`.
### `Options`
Configuration (TypeScript type).
###### Fields
* `elementAttributeNameCase`
([`ElementAttributeNameCase`][api-element-attribute-name-case], default:
`'react'`)
— specify casing to use for attribute names; this casing is used for hast
elements, not for embedded MDX JSX nodes (components that someone authored
manually)
* `handlers` (`Record<string, Handle>`, optional)
— custom handlers
* `space` ([`Space`][api-space], default: `'html'`)
— which space the document is in; when an `<svg>` element is found in the
HTML space, this package already automatically switches to and from the SVG
space when entering and exiting it
* `stylePropertyNameCase`
([`StylePropertyNameCase`][api-style-property-name-case],
default: `'dom'`)
— specify casing to use for property names in `style` objects; this casing
is used for hast elements, not for embedded MDX JSX nodes (components that
someone authored manually)
* `tableCellAlignToStyle` (`boolean`, default: `true`)
— turn obsolete `align` props on `td` and `th` into CSS `style` props
### `Space`
Namespace (TypeScript type).
###### Type
```ts
type Space = 'html' | 'svg'
```
### `State`
Info passed around about the current state (TypeScript type).
###### Fields
* `all` (`(node: HastParent) => EstreeJsxChild | undefined`)
— transform children of a hast parent to estree
* `comments` (`Array<EstreeComment>`)
— list of estree comments
* `createJsxAttributeName` (`(name: string) => EstreeJsxAttributeName`)
— create a JSX attribute name
* `createJsxElementName` (`(name: string) => EstreeJsxElementName`)
— create a JSX attribute name
* `elementAttributeNameCase`
([`ElementAttributeNameCase`][api-element-attribute-name-case])
— casing to use for attribute names
* `esm` (`Array<EstreeNode>`)
— list of top-level estree nodes
* `handle` (`(node: HastNode) => EstreeJsxChild | undefined`)
— transform a hast node to estree
* `inherit` (`(from: HastNode, to: EstreeNode) => undefined`)
— take positional info and data from `from` (use `patch` if you dont want
data)
* `patch` (`(from: HastNode, to: EstreeNode) => undefined`)
— take positional info from `from` (use `inherit` if you also want data)
* `schema` ([`Schema`][github-property-information-schema])
— current schema
* `stylePropertyNameCase`
([`StylePropertyNameCase`][api-style-property-name-case])
— casing for property names in `style` objects
* `tableCellAlignToStyle` (`boolean`)
— turn obsolete `align` props on `td` and `th` into CSS `style` props
### `StylePropertyNameCase`
Casing to use for property names in `style` objects (TypeScript type).
CSS casing is for example `background-color` and `-webkit-line-clamp`.
DOM casing is for example `backgroundColor` and `WebkitLineClamp`.
###### Type
```ts
type StylePropertyNameCase = 'css' | 'dom'
```
## Types
This package is fully typed with [TypeScript][].
It exports the additional types
[`ElementAttributeNameCase`][api-element-attribute-name-case],
[`Handle`][api-handle], [`Options`][api-options],
[`Space`][api-space], [`State`][api-state], and
[`StylePropertyNameCase`][api-style-property-name-case].
## Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, `hast-util-to-estree@^3`,
compatible with Node.js 16.
## Security
Youre working with JavaScript.
Its not safe.
## Related
* [`estree-util-build-jsx`][github-estree-util-build-jsx]
— transform JSX to function calls
* [`hastscript`][github-hastscript]
— hyperscript compatible interface for creating nodes
* [`hast-util-from-dom`](https://github.com/syntax-tree/hast-util-from-dom)
— transform a DOM tree to hast
## Contribute
See [`contributing.md`][health-contributing] in [`syntax-tree/.github`][health]
for ways to get started.
See [`support.md`][health-support] for ways to get help.
This project has a [code of conduct][health-coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
## License
[MIT][file-license] © [Titus Wormer][wooorm]
<!-- Definitions -->
[api-default-handlers]: #defaulthandlers
[api-element-attribute-name-case]: #elementattributenamecase
[api-handle]: #handle
[api-options]: #options
[api-space]: #space
[api-state]: #state
[api-style-property-name-case]: #stylepropertynamecase
[api-to-estree]: #toestreetree-options
[badge-build-image]: https://github.com/syntax-tree/hast-util-to-estree/workflows/main/badge.svg
[badge-build-url]: https://github.com/syntax-tree/hast-util-to-estree/actions
[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-to-estree.svg
[badge-coverage-url]: https://codecov.io/github/syntax-tree/hast-util-to-estree
[badge-downloads-image]: https://img.shields.io/npm/dm/hast-util-to-estree.svg
[badge-downloads-url]: https://www.npmjs.com/package/hast-util-to-estree
[badge-size-image]: https://img.shields.io/bundlejs/size/hast-util-to-estree
[badge-size-url]: https://bundlejs.com/?q=hast-util-to-estree
[esmsh]: https://esm.sh
[file-license]: license
[github-estree]: https://github.com/estree/estree
[github-estree-program]: https://github.com/estree/estree/blob/master/es5.md#programs
[github-estree-util-build-jsx]: https://github.com/wooorm/estree-util-build-jsx
[github-estree-util-to-js]: https://github.com/syntax-tree/estree-util-to-js
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[github-hast]: https://github.com/syntax-tree/hast
[github-hast-nodes]: https://github.com/syntax-tree/hast#nodes
[github-hastscript]: https://github.com/syntax-tree/hastscript
[github-mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx
[github-property-information-schema]: https://github.com/wooorm/property-information#api
[health]: https://github.com/syntax-tree/.github
[health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[health-contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[health-support]: https://github.com/syntax-tree/.github/blob/main/support.md
[mdxjs]: https://mdxjs.com
[npmjs-install]: https://docs.npmjs.com/cli/install
[typescript]: https://www.typescriptlang.org
[wooorm]: https://wooorm.com

View File

@@ -0,0 +1 @@
../../hast-util-whitespace@3.0.0/node_modules/hast-util-whitespace

View File

@@ -0,0 +1 @@
../../mdast-util-mdx-expression@2.0.1/node_modules/mdast-util-mdx-expression

View File

@@ -0,0 +1 @@
../../mdast-util-mdx-jsx@3.2.0/node_modules/mdast-util-mdx-jsx

View File

@@ -0,0 +1 @@
../../mdast-util-mdxjs-esm@2.0.1/node_modules/mdast-util-mdxjs-esm

View File

@@ -0,0 +1 @@
../../property-information@7.1.0/node_modules/property-information

View File

@@ -0,0 +1 @@
../../space-separated-tokens@2.0.2/node_modules/space-separated-tokens

View File

@@ -0,0 +1 @@
../../style-to-js@1.1.21/node_modules/style-to-js

View File

@@ -0,0 +1 @@
../../unist-util-position@5.0.0/node_modules/unist-util-position

View File

@@ -0,0 +1 @@
../../zwitch@2.0.4/node_modules/zwitch