mdast-util-to-hast
Advanced tools
Comparing version 11.1.1 to 11.2.0
/** | ||
* @param {H} h | ||
*/ | ||
export function footer(h: H): import('hast').Element | ||
export function footer(h: H): import('hast').Element | null | ||
export type BlockContent = import('mdast').BlockContent | ||
@@ -6,0 +6,0 @@ export type FootnoteDefinition = import('mdast').FootnoteDefinition |
@@ -18,18 +18,10 @@ /** | ||
export function footer(h) { | ||
var footnoteById = h.footnoteById | ||
var footnoteOrder = h.footnoteOrder | ||
var index = -1 | ||
const footnoteById = h.footnoteById | ||
const footnoteOrder = h.footnoteOrder | ||
let index = -1 | ||
/** @type {Array.<ListItem>} */ | ||
var listItems = [] | ||
/** @type {FootnoteDefinition} */ | ||
var def | ||
/** @type {Link} */ | ||
var backReference | ||
/** @type {Array.<BlockContent>} */ | ||
var content | ||
/** @type {string} */ | ||
var marker | ||
const listItems = [] | ||
while (++index < footnoteOrder.length) { | ||
def = footnoteById[footnoteOrder[index].toUpperCase()] | ||
const def = footnoteById[footnoteOrder[index].toUpperCase()] | ||
@@ -40,6 +32,6 @@ if (!def) { | ||
marker = String(index + 1) | ||
content = [...def.children] | ||
backReference = { | ||
const marker = String(index + 1) | ||
const content = [...def.children] | ||
/** @type {Link} */ | ||
const backReference = { | ||
type: 'link', | ||
@@ -50,11 +42,10 @@ url: '#fnref' + marker, | ||
} | ||
const tail = content[content.length - 1] | ||
if ( | ||
content[content.length - 1] && | ||
content[content.length - 1].type === 'paragraph' | ||
) { | ||
// @ts-ignore it’s a paragraph, TypeScript… | ||
content[content.length - 1].children.push(backReference) | ||
if (tail && tail.type === 'paragraph') { | ||
tail.children.push(backReference) | ||
} else { | ||
// @ts-ignore Indeed, link directly added in block content. | ||
// @ts-expect-error Indeed, link directly added in block content. | ||
// Which we do because that way at least the handlers will be called | ||
// for the other HTML we’re generating (as markdown). | ||
content.push(backReference) | ||
@@ -80,6 +71,6 @@ } | ||
wrap( | ||
[].concat( | ||
[ | ||
thematicBreak(h), | ||
list(h, {type: 'list', ordered: true, children: listItems}) | ||
), | ||
], | ||
true | ||
@@ -86,0 +77,0 @@ ) |
@@ -8,4 +8,8 @@ /** | ||
node: Blockquote | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Blockquote = import('mdast').Blockquote | ||
export type Handler = import('../index.js').Handler |
/** | ||
* @type {Handler} | ||
* @param {Break} node | ||
* @returns {Array.<Node>} | ||
* @returns {Array<Element|Text>} | ||
*/ | ||
export function hardBreak(h: import('../index.js').H, node: Break): Array<Node> | ||
export type Node = import('unist').Node | ||
export function hardBreak( | ||
h: import('../index.js').H, | ||
node: Break | ||
): Array<Element | Text> | ||
export type Element = import('hast').Element | ||
export type Text = import('hast').Text | ||
export type Break = import('mdast').Break | ||
export type Handler = import('../index.js').Handler |
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('hast').Text} Text | ||
* @typedef {import('mdast').Break} Break | ||
@@ -12,3 +13,3 @@ * @typedef {import('../index.js').Handler} Handler | ||
* @param {Break} node | ||
* @returns {Array.<Node>} | ||
* @returns {Array<Element|Text>} | ||
*/ | ||
@@ -15,0 +16,0 @@ export function hardBreak(h, node) { |
@@ -8,3 +8,7 @@ /** | ||
node: Code | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Code = import('mdast').Code | ||
@@ -11,0 +15,0 @@ export type Element = import('hast').Element |
@@ -15,10 +15,8 @@ /** | ||
export function code(h, node) { | ||
var value = node.value ? node.value + '\n' : '' | ||
const value = node.value ? node.value + '\n' : '' | ||
// To do: next major, use `node.lang` w/o regex, the splitting’s been going | ||
// on for years in remark now. | ||
var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/) | ||
const lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/) | ||
/** @type {Properties} */ | ||
var props = {} | ||
/** @type {Element} */ | ||
var code | ||
const props = {} | ||
@@ -29,3 +27,3 @@ if (lang) { | ||
code = h(node, 'code', props, [u('text', value)]) | ||
const code = h(node, 'code', props, [u('text', value)]) | ||
@@ -32,0 +30,0 @@ if (node.meta) { |
@@ -8,4 +8,8 @@ /** | ||
node: Delete | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Delete = import('mdast').Delete | ||
export type Handler = import('../index.js').Handler |
@@ -8,4 +8,8 @@ /** | ||
node: Emphasis | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Emphasis = import('mdast').Emphasis | ||
export type Handler = import('../index.js').Handler |
@@ -8,4 +8,8 @@ /** | ||
node: FootnoteReference | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type FootnoteReference = import('mdast').FootnoteReference | ||
export type Handler = import('../index.js').Handler |
@@ -13,6 +13,8 @@ /** | ||
export function footnoteReference(h, node) { | ||
var footnoteOrder = h.footnoteOrder | ||
var identifier = String(node.identifier) | ||
var index = footnoteOrder.indexOf(identifier) | ||
var marker = String(index === -1 ? footnoteOrder.push(identifier) : index + 1) | ||
const footnoteOrder = h.footnoteOrder | ||
const identifier = String(node.identifier) | ||
const index = footnoteOrder.indexOf(identifier) | ||
const marker = String( | ||
index === -1 ? footnoteOrder.push(identifier) : index + 1 | ||
) | ||
@@ -19,0 +21,0 @@ return h( |
@@ -8,4 +8,8 @@ /** | ||
node: Footnote | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Footnote = import('mdast').Footnote | ||
export type Handler = import('../index.js').Handler |
@@ -13,11 +13,9 @@ /** | ||
export function footnote(h, node) { | ||
var footnoteById = h.footnoteById | ||
var footnoteOrder = h.footnoteOrder | ||
var no = 1 | ||
/** @type {string} */ | ||
var identifier | ||
const footnoteById = h.footnoteById | ||
const footnoteOrder = h.footnoteOrder | ||
let no = 1 | ||
while (no in footnoteById) no++ | ||
identifier = String(no) | ||
const identifier = String(no) | ||
@@ -24,0 +22,0 @@ // No need to check if `identifier` exists in `footnoteOrder`, it’s guaranteed |
@@ -8,4 +8,8 @@ /** | ||
node: Heading | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Heading = import('mdast').Heading | ||
export type Handler = import('../index.js').Handler |
@@ -10,4 +10,8 @@ /** | ||
node: HTML | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type HTML = import('mdast').HTML | ||
export type Handler = import('../index.js').Handler |
@@ -15,4 +15,4 @@ /** | ||
export function html(h, node) { | ||
// @ts-ignore non-standard raw nodes. | ||
// @ts-expect-error non-standard raw nodes. | ||
return h.dangerous ? h.augment(node, u('raw', node.value)) : null | ||
} |
@@ -8,5 +8,9 @@ /** | ||
node: ImageReference | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type ImageReference = import('mdast').ImageReference | ||
export type Properties = import('hast').Properties | ||
export type Handler = import('../index.js').Handler |
@@ -15,5 +15,3 @@ /** | ||
export function imageReference(h, node) { | ||
var def = h.definition(node.identifier) | ||
/** @type {Properties} */ | ||
var props | ||
const def = h.definition(node.identifier) | ||
@@ -24,3 +22,4 @@ if (!def) { | ||
props = {src: normalize(def.url || ''), alt: node.alt} | ||
/** @type {Properties} */ | ||
const props = {src: normalize(def.url || ''), alt: node.alt} | ||
@@ -27,0 +26,0 @@ if (def.title !== null && def.title !== undefined) { |
@@ -8,5 +8,9 @@ /** | ||
node: Image | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Image = import('mdast').Image | ||
export type Properties = import('hast').Properties | ||
export type Handler = import('../index.js').Handler |
@@ -15,3 +15,3 @@ /** | ||
/** @type {Properties} */ | ||
var props = {src: normalize(node.url), alt: node.alt} | ||
const props = {src: normalize(node.url), alt: node.alt} | ||
@@ -18,0 +18,0 @@ if (node.title !== null && node.title !== undefined) { |
@@ -51,3 +51,3 @@ export namespace handlers { | ||
import {thematicBreak} from './thematic-break.js' | ||
declare function ignore(): any | ||
declare function ignore(): null | ||
export {} |
@@ -8,4 +8,8 @@ /** | ||
node: InlineCode | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type InlineCode = import('mdast').InlineCode | ||
export type Handler = import('../index.js').Handler |
@@ -8,5 +8,9 @@ /** | ||
node: LinkReference | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type LinkReference = import('mdast').LinkReference | ||
export type Properties = import('hast').Properties | ||
export type Handler = import('../index.js').Handler |
@@ -16,5 +16,3 @@ /** | ||
export function linkReference(h, node) { | ||
var def = h.definition(node.identifier) | ||
/** @type {Properties} */ | ||
var props | ||
const def = h.definition(node.identifier) | ||
@@ -25,3 +23,4 @@ if (!def) { | ||
props = {href: normalize(def.url || '')} | ||
/** @type {Properties} */ | ||
const props = {href: normalize(def.url || '')} | ||
@@ -28,0 +27,0 @@ if (def.title !== null && def.title !== undefined) { |
@@ -8,5 +8,9 @@ /** | ||
node: Link | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Link = import('mdast').Link | ||
export type Properties = import('hast').Properties | ||
export type Handler = import('../index.js').Handler |
@@ -16,3 +16,3 @@ /** | ||
/** @type {Properties} */ | ||
var props = {href: normalize(node.url)} | ||
const props = {href: normalize(node.url)} | ||
@@ -19,0 +19,0 @@ if (node.title !== null && node.title !== undefined) { |
@@ -10,3 +10,7 @@ /** | ||
parent: List | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type ListItem = import('mdast').ListItem | ||
@@ -13,0 +17,0 @@ export type List = import('mdast').List |
@@ -19,16 +19,13 @@ /** | ||
export function listItem(h, node, parent) { | ||
var result = all(h, node) | ||
var loose = parent ? listLoose(parent) : listItemLoose(node) | ||
const result = all(h, node) | ||
const loose = parent ? listLoose(parent) : listItemLoose(node) | ||
/** @type {Properties} */ | ||
var props = {} | ||
const props = {} | ||
/** @type {Array.<Content>} */ | ||
var wrapped = [] | ||
/** @type {number} */ | ||
var index | ||
/** @type {Element} */ | ||
var paragraph | ||
/** @type {Content} */ | ||
var child | ||
const wrapped = [] | ||
if (typeof node.checked === 'boolean') { | ||
/** @type {Element} */ | ||
let paragraph | ||
if ( | ||
@@ -62,6 +59,6 @@ result[0] && | ||
index = -1 | ||
let index = -1 | ||
while (++index < result.length) { | ||
child = result[index] | ||
const child = result[index] | ||
@@ -85,4 +82,6 @@ // Add eols before nodes, except if this is a loose, first paragraph. | ||
const tail = result[result.length - 1] | ||
// Add a final eol. | ||
if (result.length > 0 && (loose || child.tagName !== 'p')) { | ||
if (tail && (loose || !('tagName' in tail) || tail.tagName !== 'p')) { | ||
wrapped.push(u('text', '\n')) | ||
@@ -99,5 +98,5 @@ } | ||
function listLoose(node) { | ||
var loose = node.spread | ||
var children = node.children | ||
var index = -1 | ||
let loose = node.spread | ||
const children = node.children | ||
let index = -1 | ||
@@ -108,3 +107,3 @@ while (!loose && ++index < children.length) { | ||
return loose | ||
return Boolean(loose) | ||
} | ||
@@ -117,3 +116,3 @@ | ||
function listItemLoose(node) { | ||
var spread = node.spread | ||
const spread = node.spread | ||
@@ -120,0 +119,0 @@ return spread === undefined || spread === null |
/** | ||
* @type {Handler} | ||
* @param {List} node | ||
* @returns {Element} | ||
*/ | ||
export function list( | ||
h: import('../index.js').H, | ||
node: List | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
export function list(h: import('../index.js').H, node: List): Element | ||
export type List = import('mdast').List | ||
export type Element = import('hast').Element | ||
export type Properties = import('hast').Properties | ||
export type Handler = import('../index.js').Handler |
/** | ||
* @typedef {import('mdast').List} List | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('hast').Properties} Properties | ||
@@ -13,9 +14,10 @@ * @typedef {import('../index.js').Handler} Handler | ||
* @param {List} node | ||
* @returns {Element} | ||
*/ | ||
export function list(h, node) { | ||
/** @type {Properties} */ | ||
var props = {} | ||
var name = node.ordered ? 'ol' : 'ul' | ||
var items = all(h, node) | ||
var index = -1 | ||
const props = {} | ||
const name = node.ordered ? 'ol' : 'ul' | ||
const items = all(h, node) | ||
let index = -1 | ||
@@ -28,9 +30,10 @@ if (typeof node.start === 'number' && node.start !== 1) { | ||
while (++index < items.length) { | ||
const item = items[index] | ||
if ( | ||
items[index].type === 'element' && | ||
items[index].tagName === 'li' && | ||
// @ts-ignore looks like properties. | ||
items[index].properties.className && | ||
// @ts-ignore looks like properties. | ||
items[index].properties.className.includes('task-list-item') | ||
item.type === 'element' && | ||
item.tagName === 'li' && | ||
item.properties && | ||
Array.isArray(item.properties.className) && | ||
item.properties.className.includes('task-list-item') | ||
) { | ||
@@ -37,0 +40,0 @@ props.className = ['contains-task-list'] |
@@ -8,4 +8,8 @@ /** | ||
node: Paragraph | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Paragraph = import('mdast').Paragraph | ||
export type Handler = import('../index.js').Handler |
@@ -8,4 +8,8 @@ /** | ||
node: Root | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Root = import('mdast').Root | ||
export type Handler = import('../index.js').Handler |
@@ -15,4 +15,4 @@ /** | ||
export function root(h, node) { | ||
// @ts-ignore top-level is a root. | ||
// @ts-expect-error `root`s are also fine. | ||
return h.augment(node, u('root', wrap(all(h, node)))) | ||
} |
@@ -8,4 +8,8 @@ /** | ||
node: Strong | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Strong = import('mdast').Strong | ||
export type Handler = import('../index.js').Handler |
@@ -8,3 +8,7 @@ /** | ||
node: Table | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Table = import('mdast').Table | ||
@@ -11,0 +15,0 @@ export type TableCell = import('mdast').TableCell |
@@ -18,26 +18,17 @@ /** | ||
export function table(h, node) { | ||
var rows = node.children | ||
var index = rows.length | ||
var align = node.align || [] | ||
const rows = node.children | ||
let index = rows.length | ||
const align = node.align || [] | ||
/** @type {Array.<Element>} */ | ||
var result = [] | ||
/** @type {number} */ | ||
var pos | ||
/** @type {Array.<TableCell>} */ | ||
var row | ||
/** @type {Array.<Content>} */ | ||
var out | ||
/** @type {string} */ | ||
var name | ||
/** @type {TableCell} */ | ||
var cell | ||
const result = [] | ||
while (index--) { | ||
row = rows[index].children | ||
name = index === 0 ? 'th' : 'td' | ||
pos = node.align ? align.length : row.length | ||
out = [] | ||
const row = rows[index].children | ||
const name = index === 0 ? 'th' : 'td' | ||
let pos = node.align ? align.length : row.length | ||
/** @type {Array.<Content>} */ | ||
const out = [] | ||
while (pos--) { | ||
cell = row[pos] | ||
const cell = row[pos] | ||
out[pos] = h(cell, name, {align: align[pos]}, cell ? all(h, cell) : []) | ||
@@ -44,0 +35,0 @@ } |
@@ -8,4 +8,8 @@ /** | ||
node: Text | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
): | ||
| import('../index.js').Content | ||
| import('../index.js').Content[] | ||
| null | ||
| undefined | ||
export type Text = import('mdast').Text | ||
export type Handler = import('../index.js').Handler |
/** | ||
* @typedef {import('mdast').ThematicBreak} ThematicBreak | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('../index.js').Handler} Handler | ||
@@ -8,8 +9,10 @@ */ | ||
* @param {ThematicBreak} [node] | ||
* @returns {Element} | ||
*/ | ||
export function thematicBreak( | ||
h: import('../index.js').H, | ||
node?: ThematicBreak | ||
): import('../index.js').Content | import('../index.js').Content[] | ||
node?: import('mdast').ThematicBreak | undefined | ||
): Element | ||
export type ThematicBreak = import('mdast').ThematicBreak | ||
export type Element = import('hast').Element | ||
export type Handler = import('../index.js').Handler |
/** | ||
* @typedef {import('mdast').ThematicBreak} ThematicBreak | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('../index.js').Handler} Handler | ||
@@ -9,2 +10,3 @@ */ | ||
* @param {ThematicBreak} [node] | ||
* @returns {Element} | ||
*/ | ||
@@ -11,0 +13,0 @@ export function thematicBreak(h, node) { |
/** | ||
* Transform `tree` (an mdast node) to a hast node. | ||
* | ||
* @param {Node} tree mdast node | ||
* @param {MdastNode} tree mdast node | ||
* @param {Options} [options] Configuration | ||
* @returns {Node} hast node | ||
* @returns {HastNode|null|undefined} hast node | ||
*/ | ||
export function toHast(tree: Node, options?: Options): Node | ||
export type Node = import('unist').Node | ||
export type Parent = import('unist').Parent | ||
export function toHast( | ||
tree: MdastNode, | ||
options?: Options | undefined | ||
): HastNode | null | undefined | ||
export type MdastNode = | ||
| import('mdast').Root | ||
| import('mdast').Parent['children'][number] | ||
export type HastNode = | ||
| import('hast').Root | ||
| import('hast').Parent['children'][number] | ||
export type Parent = import('mdast').Parent | ||
export type Definition = import('mdast').Definition | ||
@@ -18,3 +26,2 @@ export type FootnoteDefinition = import('mdast').FootnoteDefinition | ||
export type Root = import('hast').Root | ||
export type NodeLikeWithPosition = import('unist-util-position').NodeLike | ||
export type PositionLike = import('unist-util-position').PositionLike | ||
@@ -28,11 +35,11 @@ export type FootnoteDefinitionVisitor = | ||
*/ | ||
hName?: string | ||
hName?: string | undefined | ||
/** | ||
* Defines the properties of an element | ||
*/ | ||
hProperties?: Properties | ||
hProperties?: import('hast').Properties | undefined | ||
/** | ||
* Defines the (hast) children of an element | ||
*/ | ||
hChildren?: Array<Content> | ||
hChildren?: Content[] | undefined | ||
} | ||
@@ -48,3 +55,3 @@ /** | ||
*/ | ||
export type NodeWithData = Node & { | ||
export type NodeWithData = MdastNode & { | ||
data?: Data | ||
@@ -54,15 +61,15 @@ } | ||
h: H, | ||
node: Node, | ||
node: any, | ||
parent: Parent | null | ||
) => Content | Array<Content> | null | undefined | ||
export type HFunctionProps = ( | ||
node: Node | PositionLike | null | undefined, | ||
node: MdastNode | PositionLike | null | undefined, | ||
tagName: string, | ||
props: Properties, | ||
children?: Array<Content> | null | ||
children?: Content[] | null | undefined | ||
) => Element | ||
export type HFunctionNoProps = ( | ||
node: Node | PositionLike | null | undefined, | ||
node: MdastNode | PositionLike | null | undefined, | ||
tagName: string, | ||
children?: Array<Content> | null | ||
children?: Content[] | null | undefined | ||
) => Element | ||
@@ -112,15 +119,15 @@ export type HFields = { | ||
*/ | ||
allowDangerousHtml?: boolean | ||
allowDangerousHtml?: boolean | undefined | ||
/** | ||
* Object mapping mdast nodes to functions handling them | ||
*/ | ||
handlers?: Handlers | ||
handlers?: Handlers | undefined | ||
/** | ||
* List of custom mdast node types to pass through (keep) in hast | ||
*/ | ||
passThrough?: Array<string> | ||
passThrough?: string[] | undefined | ||
/** | ||
* Handler for all unknown nodes. | ||
*/ | ||
unknownHandler?: Handler | ||
unknownHandler?: Handler | undefined | ||
} | ||
@@ -127,0 +134,0 @@ /** |
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('mdast').Root|import('mdast').Parent['children'][number]} MdastNode | ||
* @typedef {import('hast').Root|import('hast').Parent['children'][number]} HastNode | ||
* @typedef {import('mdast').Parent} Parent | ||
* @typedef {import('mdast').Definition} Definition | ||
@@ -11,3 +12,2 @@ * @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition | ||
* @typedef {import('hast').Root} Root | ||
* @typedef {import('unist-util-position').NodeLike} NodeLikeWithPosition | ||
* @typedef {import('unist-util-position').PositionLike} PositionLike | ||
@@ -25,7 +25,7 @@ * @typedef {import('unist-util-visit').Visitor<FootnoteDefinition>} FootnoteDefinitionVisitor | ||
* | ||
* @typedef {Node & {data?: Data}} NodeWithData unist node with embedded hast data | ||
* @typedef {MdastNode & {data?: Data}} NodeWithData unist node with embedded hast data | ||
* | ||
* @callback Handler | ||
* @param {H} h Handle context | ||
* @param {Node} node mdast node to handle | ||
* @param {any} node mdast node to handle | ||
* @param {Parent|null} parent Parent of `node` | ||
@@ -35,3 +35,3 @@ * @returns {Content|Array.<Content>|null|undefined} hast node | ||
* @callback HFunctionProps | ||
* @param {Node|PositionLike|null|undefined} node mdast node or unist position | ||
* @param {MdastNode|PositionLike|null|undefined} node mdast node or unist position | ||
* @param {string} tagName HTML tag name | ||
@@ -43,3 +43,3 @@ * @param {Properties} props Properties | ||
* @callback HFunctionNoProps | ||
* @param {Node|PositionLike|null|undefined} node mdast node or unist position | ||
* @param {MdastNode|PositionLike|null|undefined} node mdast node or unist position | ||
* @param {string} tagName HTML tag name | ||
@@ -78,7 +78,7 @@ * @param {Array.<Content>?} [children] hast content | ||
var own = {}.hasOwnProperty | ||
const own = {}.hasOwnProperty | ||
/** | ||
* Factory to transform. | ||
* @param {Node} tree mdast node | ||
* @param {MdastNode} tree mdast node | ||
* @param {Options} [options] Configuration | ||
@@ -88,6 +88,6 @@ * @returns {H} `h` function | ||
function factory(tree, options) { | ||
var settings = options || {} | ||
var dangerous = settings.allowDangerousHtml || false | ||
const settings = options || {} | ||
const dangerous = settings.allowDangerousHtml || false | ||
/** @type {Object.<string, FootnoteDefinition>} */ | ||
var footnoteById = {} | ||
const footnoteById = {} | ||
@@ -106,3 +106,3 @@ h.dangerous = dangerous | ||
// @ts-ignore Hush, it’s fine! | ||
// @ts-expect-error Hush, it’s fine! | ||
return h | ||
@@ -117,10 +117,6 @@ | ||
function augment(left, right) { | ||
/** @type {Data} */ | ||
var data | ||
/** @type {NodeLikeWithPosition} */ | ||
var ctx | ||
// Handle `data.hName`, `data.hProperties, `data.hChildren`. | ||
if (left && 'data' in left && left.data) { | ||
data = left.data | ||
/** @type {Data} */ | ||
const data = left.data | ||
@@ -150,4 +146,3 @@ if (data.hName) { | ||
if (left) { | ||
// @ts-ignore looks like a node vs. position. | ||
ctx = 'type' in left ? left : {position: left} | ||
const ctx = 'type' in left ? left : {position: left} | ||
@@ -173,3 +168,3 @@ if (!generated(ctx)) { | ||
// @ts-ignore augmenting an element yields an element. | ||
// @ts-expect-error augmenting an element yields an element. | ||
return augment(node, { | ||
@@ -187,3 +182,3 @@ type: 'element', | ||
function onfootnotedefinition(definition) { | ||
var id = String(definition.identifier).toUpperCase() | ||
const id = String(definition.identifier).toUpperCase() | ||
@@ -201,13 +196,13 @@ // Mimick CM behavior of link definitions. | ||
* | ||
* @param {Node} tree mdast node | ||
* @param {MdastNode} tree mdast node | ||
* @param {Options} [options] Configuration | ||
* @returns {Node} hast node | ||
* @returns {HastNode|null|undefined} hast node | ||
*/ | ||
export function toHast(tree, options) { | ||
var h = factory(tree, options) | ||
var node = one(h, tree, null) | ||
var foot = footer(h) | ||
const h = factory(tree, options) | ||
const node = one(h, tree, null) | ||
const foot = footer(h) | ||
if (foot) { | ||
// @ts-ignore If there’s a footer, there were definitions, meaning block | ||
// @ts-expect-error If there’s a footer, there were definitions, meaning block | ||
// content. | ||
@@ -214,0 +209,0 @@ // So assume `node` is a parent node. |
@@ -19,10 +19,4 @@ /** | ||
export function revert(h, node) { | ||
var subtype = node.referenceType | ||
var suffix = ']' | ||
/** @type {Array.<Content>} */ | ||
var contents | ||
/** @type {Content} */ | ||
var head | ||
/** @type {Content} */ | ||
var tail | ||
const subtype = node.referenceType | ||
let suffix = ']' | ||
@@ -39,4 +33,4 @@ if (subtype === 'collapsed') { | ||
contents = all(h, node) | ||
head = contents[0] | ||
const contents = all(h, node) | ||
const head = contents[0] | ||
@@ -49,3 +43,3 @@ if (head && head.type === 'text') { | ||
tail = contents[contents.length - 1] | ||
const tail = contents[contents.length - 1] | ||
@@ -52,0 +46,0 @@ if (tail && tail.type === 'text') { |
/** | ||
* @type {Handler} | ||
* @param {MdastNode} node | ||
*/ | ||
export function one( | ||
h: import('./index.js').H, | ||
node: import('unist').Node, | ||
parent: import('unist').Parent | ||
): import('./index.js').Content | import('./index.js').Content[] | ||
node: MdastNode, | ||
parent: import('mdast').Parent | null | ||
): | ||
| import('./index.js').Content | ||
| import('./index.js').Content[] | ||
| null | ||
| undefined | ||
/** | ||
* @param {H} h | ||
* @param {Node} parent | ||
* @param {MdastNode} parent | ||
*/ | ||
export function all(h: H, parent: Node): import('./index.js').Content[] | ||
export type Node = import('unist').Node | ||
export type Parent = import('unist').Parent | ||
export type Literal = import('mdast').Literal | ||
export type Text = import('mdast').Text | ||
export function all(h: H, parent: MdastNode): import('./index.js').Content[] | ||
export type MdastNode = | ||
| import('mdast').Root | ||
| import('mdast').Parent['children'][number] | ||
export type H = import('./index.js').H | ||
export type Handler = import('./index.js').Handler | ||
export type Content = import('./index.js').Content |
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('mdast').Literal} Literal | ||
* @typedef {import('mdast').Text} Text | ||
* @typedef {import('mdast').Root|import('mdast').Parent['children'][number]} MdastNode | ||
* @typedef {import('./index.js').H} H | ||
@@ -13,3 +10,3 @@ * @typedef {import('./index.js').Handler} Handler | ||
var own = {}.hasOwnProperty | ||
const own = {}.hasOwnProperty | ||
@@ -19,5 +16,15 @@ /** | ||
* @type {Handler} | ||
* @param {MdastNode} node | ||
*/ | ||
function unknown(h, node) { | ||
if (text(node)) { | ||
const data = node.data || {} | ||
if ( | ||
'value' in node && | ||
!( | ||
own.call(data, 'hName') || | ||
own.call(data, 'hProperties') || | ||
own.call(data, 'hChildren') | ||
) | ||
) { | ||
return h.augment(node, u('text', node.value)) | ||
@@ -31,7 +38,8 @@ } | ||
* @type {Handler} | ||
* @param {MdastNode} node | ||
*/ | ||
export function one(h, node, parent) { | ||
var type = node && node.type | ||
const type = node && node.type | ||
/** @type {Handler} */ | ||
var fn | ||
let fn | ||
@@ -55,26 +63,8 @@ // Fail on non-nodes. | ||
/** | ||
* Check if the node should be renderered as a text node. | ||
* @param {Node} node | ||
* @returns {node is Literal} | ||
*/ | ||
function text(node) { | ||
var data = node.data || {} | ||
if ( | ||
own.call(data, 'hName') || | ||
own.call(data, 'hProperties') || | ||
own.call(data, 'hChildren') | ||
) { | ||
return false | ||
} | ||
return 'value' in node | ||
} | ||
/** | ||
* @type {Handler} | ||
* @param {MdastNode} node | ||
*/ | ||
function returnNode(h, node) { | ||
// @ts-ignore pass through any unknown node. | ||
return node.children ? {...node, children: all(h, node)} : node | ||
// @ts-expect-error: Pass through custom node. | ||
return 'children' in node ? {...node, children: all(h, node)} : node | ||
} | ||
@@ -84,36 +74,36 @@ | ||
* @param {H} h | ||
* @param {Node} parent | ||
* @param {MdastNode} parent | ||
*/ | ||
export function all(h, parent) { | ||
/** @type {Array.<Node>} */ | ||
// @ts-ignore looks like a parent. | ||
var nodes = parent.children || [] | ||
var index = -1 | ||
/** @type {Array.<Content>} */ | ||
var values = [] | ||
/** @type {Content} */ | ||
var result | ||
/** @type {Content} */ | ||
var head | ||
const values = [] | ||
while (++index < nodes.length) { | ||
// @ts-ignore looks like a parent. | ||
result = one(h, nodes[index], parent) | ||
if ('children' in parent) { | ||
const nodes = parent.children | ||
let index = -1 | ||
if (result) { | ||
if (index && nodes[index - 1].type === 'break') { | ||
if (result.type === 'text') { | ||
result.value = result.value.replace(/^\s+/, '') | ||
} | ||
while (++index < nodes.length) { | ||
const result = one(h, nodes[index], parent) | ||
if (result.type === 'element') { | ||
head = result.children[0] | ||
if (result) { | ||
if (index && nodes[index - 1].type === 'break') { | ||
if (!Array.isArray(result) && result.type === 'text') { | ||
result.value = result.value.replace(/^\s+/, '') | ||
} | ||
if (head && head.type === 'text') { | ||
head.value = head.value.replace(/^\s+/, '') | ||
if (!Array.isArray(result) && result.type === 'element') { | ||
const head = result.children[0] | ||
if (head && head.type === 'text') { | ||
head.value = head.value.replace(/^\s+/, '') | ||
} | ||
} | ||
} | ||
if (Array.isArray(result)) { | ||
values.push(...result) | ||
} else { | ||
values.push(result) | ||
} | ||
} | ||
values = values.concat(result) | ||
} | ||
@@ -120,0 +110,0 @@ } |
@@ -9,3 +9,6 @@ /** | ||
*/ | ||
export function wrap(nodes: Array<Content>, loose?: boolean): Array<Content> | ||
export function wrap( | ||
nodes: Array<Content>, | ||
loose?: boolean | undefined | ||
): Array<Content> | ||
export type Content = import('./index.js').Content |
@@ -17,4 +17,4 @@ /** | ||
/** @type {Array.<Content>} */ | ||
var result = [] | ||
var index = -1 | ||
const result = [] | ||
let index = -1 | ||
@@ -21,0 +21,0 @@ if (loose) { |
{ | ||
"name": "mdast-util-to-hast", | ||
"version": "11.1.1", | ||
"version": "11.2.0", | ||
"description": "mdast utility to transform to hast", | ||
@@ -40,3 +40,2 @@ "license": "MIT", | ||
"@types/mdurl": "^1.0.0", | ||
"@types/unist": "^2.0.0", | ||
"mdast-util-definitions": "^5.0.0", | ||
@@ -59,3 +58,3 @@ "mdurl": "^1.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.40.0" | ||
"xo": "^0.42.0" | ||
}, | ||
@@ -82,4 +81,2 @@ "scripts": { | ||
"max-depth": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off", | ||
"import/no-cycle": "error" | ||
@@ -96,4 +93,8 @@ } | ||
"detail": true, | ||
"strict": true | ||
"strict": true, | ||
"#": "needed `any`s", | ||
"ignoreFiles": [ | ||
"lib/index.d.ts" | ||
] | ||
} | ||
} |
@@ -37,10 +37,10 @@ # mdast-util-to-hast | ||
```js | ||
import fs from 'fs' | ||
import fromMarkdown from 'mdast-util-from-markdown' | ||
import fs from 'node:fs' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
import {toHast} from 'mdast-util-to-hast' | ||
import toHtml from 'hast-util-to-html' | ||
import {toHtml} from 'hast-util-to-html' | ||
var mdast = fromMarkdown(fs.readFileSync('example.md')) | ||
var hast = toHast(mdast) | ||
var html = toHtml(hast) | ||
const mdast = fromMarkdown(fs.readFileSync('example.md')) | ||
const hast = toHast(mdast) | ||
const html = toHtml(hast) | ||
@@ -47,0 +47,0 @@ console.log(html) |
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
57370
9
1561
- Removed@types/unist@^2.0.0