mdast-util-mdx-jsx
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -11,3 +11,2 @@ import type {Node as MdastNode} from 'unist' | ||
import type {Program} from 'estree-jsx' | ||
import type {Tag} from './lib/index.js' | ||
@@ -14,0 +13,0 @@ |
@@ -33,5 +33,6 @@ /** | ||
export type OnExitError = import('mdast-util-from-markdown').OnExitError | ||
export type ToMarkdownHandle = import('mdast-util-to-markdown').Handle | ||
export type ToMarkdownExtension = import('mdast-util-to-markdown').Options | ||
export type ToMarkdownHandle = import('mdast-util-to-markdown').Handle | ||
export type ToMarkdownMap = import('mdast-util-to-markdown').Map | ||
export type State = import('mdast-util-to-markdown').State | ||
export type Tracker = import('mdast-util-to-markdown').Tracker | ||
export type MdxJsxAttributeValueExpression = | ||
@@ -38,0 +39,0 @@ import('../index.js').MdxJsxAttributeValueExpression |
164
lib/index.js
@@ -11,5 +11,6 @@ /** | ||
* | ||
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle | ||
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension | ||
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle | ||
* @typedef {import('mdast-util-to-markdown').Map} ToMarkdownMap | ||
* @typedef {import('mdast-util-to-markdown').State} State | ||
* @typedef {import('mdast-util-to-markdown').Tracker} Tracker | ||
* | ||
@@ -65,3 +66,2 @@ * @typedef {import('../index.js').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression | ||
import {stringifyEntitiesLight} from 'stringify-entities' | ||
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js' | ||
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js' | ||
@@ -73,2 +73,4 @@ import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js' | ||
const indent = ' ' | ||
/** | ||
@@ -527,11 +529,17 @@ * Create an extension for `mdast-util-from-markdown` to enable MDX JSX. | ||
function mdxElement(node, _, context, safeOptions) { | ||
const tracker = track(safeOptions) | ||
const selfClosing = | ||
node.name && (!node.children || node.children.length === 0) | ||
const exit = context.enter(node.type) | ||
let index = -1 | ||
const selfClosing = node.name | ||
? !node.children || node.children.length === 0 | ||
: false | ||
const depth = inferDepth(context) | ||
const currentIndent = createIndent(depth) | ||
const trackerOneLine = track(safeOptions) | ||
const trackerMultiLine = track(safeOptions) | ||
/** @type {Array<string>} */ | ||
const serializedAttributes = [] | ||
let value = tracker.move('<' + (node.name || '')) | ||
const prefix = currentIndent + '<' + (node.name || '') | ||
const exit = context.enter(node.type) | ||
trackerOneLine.move(prefix) | ||
trackerMultiLine.move(prefix) | ||
// None. | ||
@@ -543,2 +551,3 @@ if (node.attributes && node.attributes.length > 0) { | ||
let index = -1 | ||
while (++index < node.attributes.length) { | ||
@@ -593,3 +602,3 @@ const attribute = node.attributes[index] | ||
// Current position (including `<tag`). | ||
tracker.current().now.column + | ||
trackerOneLine.current().now.column + | ||
// -1 because columns, +1 for ` ` before attributes. | ||
@@ -605,5 +614,19 @@ // Attributes joined by spaces. | ||
let tracker = trackerOneLine | ||
let value = prefix | ||
if (attributesOnTheirOwnLine) { | ||
tracker = trackerMultiLine | ||
let index = -1 | ||
while (++index < serializedAttributes.length) { | ||
// Only indent first line of of attributes, we can’t indent attribute | ||
// values. | ||
serializedAttributes[index] = | ||
currentIndent + indent + serializedAttributes[index] | ||
} | ||
value += tracker.move( | ||
'\n' + indentLines(serializedAttributes.join('\n'), map) | ||
'\n' + serializedAttributes.join('\n') + '\n' + currentIndent | ||
) | ||
@@ -614,6 +637,2 @@ } else if (attributesOnOneLine) { | ||
if (attributesOnTheirOwnLine) { | ||
value += tracker.move('\n') | ||
} | ||
if (selfClosing) { | ||
@@ -628,17 +647,15 @@ value += tracker.move( | ||
if (node.children && node.children.length > 0) { | ||
if (node.type === 'mdxJsxFlowElement') { | ||
tracker.shift(2) | ||
value += tracker.move('\n') | ||
if (node.type === 'mdxJsxTextElement') { | ||
value += tracker.move( | ||
indentLines(containerFlow(node, context, tracker.current()), map) | ||
) | ||
value += tracker.move('\n') | ||
} else { | ||
value += tracker.move( | ||
containerPhrasing(node, context, { | ||
...tracker.current(), | ||
before: '<', | ||
after: '>' | ||
before: '>', | ||
after: '<' | ||
}) | ||
) | ||
} else { | ||
tracker.shift(2) | ||
value += tracker.move('\n') | ||
value += tracker.move(containerFlow(node, context, tracker.current())) | ||
value += tracker.move('\n') | ||
} | ||
@@ -648,3 +665,3 @@ } | ||
if (!selfClosing) { | ||
value += tracker.move('</' + (node.name || '') + '>') | ||
value += tracker.move(currentIndent + '</' + (node.name || '') + '>') | ||
} | ||
@@ -655,14 +672,93 @@ | ||
} | ||
} | ||
/** @type {ToMarkdownMap} */ | ||
function map(line, _, blank) { | ||
return (blank ? '' : ' ') + line | ||
// Modified copy of: | ||
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/a381cbc/lib/util/container-flow.js>. | ||
// | ||
// To do: add `indent` support to `mdast-util-to-markdown`. | ||
// As indents are only used for JSX, it’s fine for now, but perhaps better | ||
// there. | ||
/** | ||
* @param {MdxJsxFlowElement} parent | ||
* Parent of flow nodes. | ||
* @param {State} state | ||
* Info passed around about the current state. | ||
* @param {ReturnType<Tracker['current']>} info | ||
* Info on where we are in the document we are generating. | ||
* @returns {string} | ||
* Serialized children, joined by (blank) lines. | ||
*/ | ||
function containerFlow(parent, state, info) { | ||
const indexStack = state.indexStack | ||
const children = parent.children | ||
const tracker = state.createTracker(info) | ||
const currentIndent = createIndent(inferDepth(state)) | ||
/** @type {Array<string>} */ | ||
const results = [] | ||
let index = -1 | ||
indexStack.push(-1) | ||
while (++index < children.length) { | ||
const child = children[index] | ||
indexStack[indexStack.length - 1] = index | ||
const childInfo = {before: '\n', after: '\n', ...tracker.current()} | ||
const result = state.handle(child, parent, state, childInfo) | ||
const serializedChild = | ||
child.type === 'mdxJsxFlowElement' | ||
? result | ||
: indentLines(result, function (line, _, blank) { | ||
return (blank ? '' : currentIndent) + line | ||
}) | ||
results.push(tracker.move(serializedChild)) | ||
if (child.type !== 'list') { | ||
state.bulletLastUsed = undefined | ||
} | ||
if (index < children.length - 1) { | ||
results.push(tracker.move('\n\n')) | ||
} | ||
} | ||
/** | ||
* @type {ToMarkdownHandle} | ||
*/ | ||
function peekElement() { | ||
return '<' | ||
indexStack.pop() | ||
return results.join('') | ||
} | ||
/** | ||
* | ||
* @param {State} context | ||
* @returns {number} | ||
*/ | ||
function inferDepth(context) { | ||
let depth = 0 | ||
for (const x of context.stack) { | ||
if (x === 'mdxJsxFlowElement') { | ||
depth++ | ||
} | ||
} | ||
return depth | ||
} | ||
/** | ||
* @param {number} depth | ||
* @returns {string} | ||
*/ | ||
function createIndent(depth) { | ||
return indent.repeat(depth) | ||
} | ||
/** | ||
* @type {ToMarkdownHandle} | ||
*/ | ||
function peekElement() { | ||
return '<' | ||
} |
{ | ||
"name": "mdast-util-mdx-jsx", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "mdast extension to parse and serialize MDX or MDX.js JSX", | ||
@@ -53,3 +53,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^18.0.0", | ||
"@types/node": "^20.0.0", | ||
"acorn": "^8.0.0", | ||
@@ -63,4 +63,4 @@ "c8": "^7.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.53.0" | ||
"typescript": "^5.0.0", | ||
"xo": "^0.54.0" | ||
}, | ||
@@ -67,0 +67,0 @@ "scripts": { |
@@ -121,3 +121,3 @@ # mdast-util-mdx-jsx | ||
const tree = fromMarkdown(doc, { | ||
extensions: [mdxJsx({acorn: acorn, addResult: true})], | ||
extensions: [mdxJsx({acorn, addResult: true})], | ||
mdastExtensions: [mdxJsxFromMarkdown()] | ||
@@ -124,0 +124,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55388
1039