Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mdast-util-to-hast

Package Overview
Dependencies
Maintainers
2
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-to-hast - npm Package Compare versions

Comparing version 10.2.0 to 11.0.0

index.d.ts

3

index.js

@@ -1,2 +0,1 @@

'use strict'
module.exports = require('./lib')
export {toHast} from './lib/index.js'

@@ -1,16 +0,28 @@

'use strict'
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('./index.js').H} H
* @typedef {import('./index.js').Content} Content
*/
module.exports = all
import {one} from './one.js'
var one = require('./one')
function all(h, parent) {
/**
* @param {H} h
* @param {Node} parent
*/
export function all(h, parent) {
/** @type {Array.<Node>} */
// @ts-ignore looks like a parent.
var nodes = parent.children || []
var length = nodes.length
var index = -1
/** @type {Array.<Content>} */
var values = []
var index = -1
/** @type {Content} */
var result
/** @type {Content} */
var head
while (++index < length) {
while (++index < nodes.length) {
// @ts-ignore looks like a parent.
result = one(h, nodes[index], parent)

@@ -20,10 +32,12 @@

if (index && nodes[index - 1].type === 'break') {
if (result.value) {
if (result.type === 'text') {
result.value = result.value.replace(/^\s+/, '')
}
head = result.children && result.children[0]
if (result.type === 'element') {
head = result.children[0]
if (head && head.value) {
head.value = head.value.replace(/^\s+/, '')
if (head && head.type === 'text') {
head.value = head.value.replace(/^\s+/, '')
}
}

@@ -30,0 +44,0 @@ }

@@ -1,21 +0,33 @@

'use strict'
/**
* @typedef {import('mdast').BlockContent} BlockContent
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
* @typedef {import('mdast').Link} Link
* @typedef {import('mdast').ListItem} ListItem
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('./index.js').H} H
*/
module.exports = generateFootnotes
import {thematicBreak} from './handlers/thematic-break.js'
import {list} from './handlers/list.js'
import {wrap} from './wrap.js'
var thematicBreak = require('./handlers/thematic-break')
var list = require('./handlers/list')
var wrap = require('./wrap')
function generateFootnotes(h) {
/**
* @param {H} h
*/
export function footer(h) {
var footnoteById = h.footnoteById
var footnoteOrder = h.footnoteOrder
var length = footnoteOrder.length
var index = -1
/** @type {Array.<ListItem>} */
var listItems = []
/** @type {FootnoteDefinition} */
var def
/** @type {Link} */
var backReference
/** @type {Array.<BlockContent>} */
var content
var tail
/** @type {string} */
var marker
while (++index < length) {
while (++index < footnoteOrder.length) {
def = footnoteById[footnoteOrder[index].toUpperCase()]

@@ -27,21 +39,26 @@

content = def.children.concat()
tail = content[content.length - 1]
marker = String(index + 1)
content = [...def.children]
backReference = {
type: 'link',
url: '#fnref-' + def.identifier,
data: {hProperties: {className: ['footnote-backref']}},
url: '#fnref' + marker,
data: {hProperties: {className: ['footnote-back'], role: 'doc-backlink'}},
children: [{type: 'text', value: '↩'}]
}
if (!tail || tail.type !== 'paragraph') {
tail = {type: 'paragraph', children: []}
content.push(tail)
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)
} else {
// @ts-ignore Indeed, link directly added in block content.
content.push(backReference)
}
tail.children.push(backReference)
listItems.push({
type: 'listItem',
data: {hProperties: {id: 'fn-' + def.identifier}},
data: {hProperties: {id: 'fn' + marker, role: 'doc-endnote'}},
children: content,

@@ -58,9 +75,9 @@ position: def.position

null,
'div',
{className: ['footnotes']},
'section',
{className: ['footnotes'], role: 'doc-endnotes'},
wrap(
[
[].concat(
thematicBreak(h),
list(h, {type: 'list', ordered: true, children: listItems})
],
),
true

@@ -67,0 +84,0 @@ )

@@ -1,10 +0,15 @@

'use strict'
/**
* @typedef {import('mdast').Blockquote} Blockquote
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = blockquote
import {wrap} from '../wrap.js'
import {all} from '../all.js'
var wrap = require('../wrap')
var all = require('../all')
function blockquote(h, node) {
/**
* @type {Handler}
* @param {Blockquote} node
*/
export function blockquote(h, node) {
return h(node, 'blockquote', wrap(all(h, node), true))
}

@@ -1,9 +0,16 @@

'use strict'
/**
* @typedef {import('unist').Node} Node
* @typedef {import('mdast').Break} Break
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = hardBreak
import {u} from 'unist-builder'
var u = require('unist-builder')
function hardBreak(h, node) {
/**
* @type {Handler}
* @param {Break} node
* @returns {Array.<Node>}
*/
export function hardBreak(h, node) {
return [h(node, 'br'), u('text', '\n')]
}

@@ -1,8 +0,15 @@

'use strict'
/**
* @typedef {import('mdast').Code} Code
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Properties} Properties
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = code
import {u} from 'unist-builder'
var u = require('unist-builder')
function code(h, node) {
/**
* @type {Handler}
* @param {Code} node
*/
export function code(h, node) {
var value = node.value ? node.value + '\n' : ''

@@ -12,3 +19,5 @@ // To do: next major, use `node.lang` w/o regex, the splitting’s been going

var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/)
/** @type {Properties} */
var props = {}
/** @type {Element} */
var code

@@ -15,0 +24,0 @@

@@ -1,9 +0,14 @@

'use strict'
/**
* @typedef {import('mdast').Delete} Delete
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = strikethrough
import {all} from '../all.js'
var all = require('../all')
function strikethrough(h, node) {
/**
* @type {Handler}
* @param {Delete} node
*/
export function strikethrough(h, node) {
return h(node, 'del', all(h, node))
}

@@ -1,9 +0,14 @@

'use strict'
/**
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = emphasis
import {all} from '../all.js'
var all = require('../all')
function emphasis(h, node) {
/**
* @type {Handler}
* @param {Emphasis} node
*/
export function emphasis(h, node) {
return h(node, 'em', all(h, node))
}

@@ -1,20 +0,29 @@

'use strict'
/**
* @typedef {import('mdast').FootnoteReference} FootnoteReference
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = footnoteReference
import {u} from 'unist-builder'
var u = require('unist-builder')
function footnoteReference(h, node) {
/**
* @type {Handler}
* @param {FootnoteReference} node
*/
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)
if (footnoteOrder.indexOf(identifier) === -1) {
footnoteOrder.push(identifier)
}
return h(node.position, 'sup', {id: 'fnref-' + identifier}, [
h(node, 'a', {href: '#fn-' + identifier, className: ['footnote-ref']}, [
u('text', node.label || identifier)
])
])
return h(
node,
'a',
{
href: '#fn' + marker,
className: ['footnote-ref'],
id: 'fnref' + marker,
role: 'doc-noteref'
},
[h(node.position, 'sup', [u('text', marker)])]
)
}

@@ -1,17 +0,22 @@

'use strict'
/**
* @typedef {import('mdast').Footnote} Footnote
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = footnote
import {footnoteReference} from './footnote-reference.js'
var footnoteReference = require('./footnote-reference')
function footnote(h, node) {
/**
* @type {Handler}
* @param {Footnote} node
*/
export function footnote(h, node) {
var footnoteById = h.footnoteById
var footnoteOrder = h.footnoteOrder
var identifier = 1
var no = 1
/** @type {string} */
var identifier
while (identifier in footnoteById) {
identifier++
}
while (no in footnoteById) no++
identifier = String(identifier)
identifier = String(no)

@@ -24,3 +29,3 @@ // No need to check if `identifier` exists in `footnoteOrder`, it’s guaranteed

type: 'footnoteDefinition',
identifier: identifier,
identifier,
children: [{type: 'paragraph', children: node.children}],

@@ -32,5 +37,5 @@ position: node.position

type: 'footnoteReference',
identifier: identifier,
identifier,
position: node.position
})
}

@@ -1,9 +0,14 @@

'use strict'
/**
* @typedef {import('mdast').Heading} Heading
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = heading
import {all} from '../all.js'
var all = require('../all')
function heading(h, node) {
/**
* @type {Handler}
* @param {Heading} node
*/
export function heading(h, node) {
return h(node, 'h' + node.depth, all(h, node))
}

@@ -1,10 +0,17 @@

'use strict'
/**
* @typedef {import('mdast').HTML} HTML
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = html
import {u} from 'unist-builder'
var u = require('unist-builder')
// Return either a `raw` node in dangerous mode, otherwise nothing.
function html(h, node) {
/**
* Return either a `raw` node in dangerous mode, otherwise nothing.
*
* @type {Handler}
* @param {HTML} node
*/
export function html(h, node) {
// @ts-ignore non-standard raw nodes.
return h.dangerous ? h.augment(node, u('raw', node.value)) : null
}

@@ -1,10 +0,17 @@

'use strict'
/**
* @typedef {import('mdast').ImageReference} ImageReference
* @typedef {import('hast').Properties} Properties
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = imageReference
import normalize from 'mdurl/encode.js'
import {revert} from '../revert.js'
var normalize = require('mdurl/encode')
var revert = require('../revert')
function imageReference(h, node) {
/**
* @type {Handler}
* @param {ImageReference} node
*/
export function imageReference(h, node) {
var def = h.definition(node.identifier)
/** @type {Properties} */
var props

@@ -11,0 +18,0 @@

@@ -1,8 +0,15 @@

'use strict'
/**
* @typedef {import('mdast').Image} Image
* @typedef {import('hast').Properties} Properties
* @typedef {import('../index.js').Handler} Handler
*/
var normalize = require('mdurl/encode')
import normalize from 'mdurl/encode.js'
module.exports = image
function image(h, node) {
/**
* @type {Handler}
* @param {Image} node
*/
export function image(h, node) {
/** @type {Properties} */
var props = {src: normalize(node.url), alt: node.alt}

@@ -9,0 +16,0 @@

@@ -1,26 +0,47 @@

'use strict'
import {blockquote} from './blockquote.js'
import {hardBreak} from './break.js'
import {code} from './code.js'
import {strikethrough} from './delete.js'
import {emphasis} from './emphasis.js'
import {footnoteReference} from './footnote-reference.js'
import {footnote} from './footnote.js'
import {heading} from './heading.js'
import {html} from './html.js'
import {imageReference} from './image-reference.js'
import {image} from './image.js'
import {inlineCode} from './inline-code.js'
import {linkReference} from './link-reference.js'
import {link} from './link.js'
import {listItem} from './list-item.js'
import {list} from './list.js'
import {paragraph} from './paragraph.js'
import {root} from './root.js'
import {strong} from './strong.js'
import {table} from './table.js'
import {text} from './text.js'
import {thematicBreak} from './thematic-break.js'
module.exports = {
blockquote: require('./blockquote'),
break: require('./break'),
code: require('./code'),
delete: require('./delete'),
emphasis: require('./emphasis'),
footnoteReference: require('./footnote-reference'),
footnote: require('./footnote'),
heading: require('./heading'),
html: require('./html'),
imageReference: require('./image-reference'),
image: require('./image'),
inlineCode: require('./inline-code'),
linkReference: require('./link-reference'),
link: require('./link'),
listItem: require('./list-item'),
list: require('./list'),
paragraph: require('./paragraph'),
root: require('./root'),
strong: require('./strong'),
table: require('./table'),
text: require('./text'),
thematicBreak: require('./thematic-break'),
export const handlers = {
blockquote,
break: hardBreak,
code,
delete: strikethrough,
emphasis,
footnoteReference,
footnote,
heading,
html,
imageReference,
image,
inlineCode,
linkReference,
link,
listItem,
list,
paragraph,
root,
strong,
table,
text,
thematicBreak,
toml: ignore,

@@ -27,0 +48,0 @@ yaml: ignore,

@@ -1,10 +0,14 @@

'use strict'
/**
* @typedef {import('mdast').InlineCode} InlineCode
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = inlineCode
import {u} from 'unist-builder'
var u = require('unist-builder')
function inlineCode(h, node) {
var value = node.value.replace(/\r?\n|\r/g, ' ')
return h(node, 'code', [u('text', value)])
/**
* @type {Handler}
* @param {InlineCode} node
*/
export function inlineCode(h, node) {
return h(node, 'code', [u('text', node.value.replace(/\r?\n|\r/g, ' '))])
}

@@ -1,11 +0,18 @@

'use strict'
/**
* @typedef {import('mdast').LinkReference} LinkReference
* @typedef {import('hast').Properties} Properties
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = linkReference
import normalize from 'mdurl/encode.js'
import {revert} from '../revert.js'
import {all} from '../all.js'
var normalize = require('mdurl/encode')
var revert = require('../revert')
var all = require('../all')
function linkReference(h, node) {
/**
* @type {Handler}
* @param {LinkReference} node
*/
export function linkReference(h, node) {
var def = h.definition(node.identifier)
/** @type {Properties} */
var props

@@ -12,0 +19,0 @@

@@ -1,9 +0,16 @@

'use strict'
/**
* @typedef {import('mdast').Link} Link
* @typedef {import('hast').Properties} Properties
* @typedef {import('../index.js').Handler} Handler
*/
var normalize = require('mdurl/encode')
var all = require('../all')
import normalize from 'mdurl/encode.js'
import {all} from '../all.js'
module.exports = link
function link(h, node) {
/**
* @type {Handler}
* @param {Link} node
*/
export function link(h, node) {
/** @type {Properties} */
var props = {href: normalize(node.url)}

@@ -10,0 +17,0 @@

@@ -1,29 +0,49 @@

'use strict'
/**
* @typedef {import('mdast').ListItem} ListItem
* @typedef {import('mdast').List} List
* @typedef {import('hast').Properties} Properties
* @typedef {import('hast').Element} Element
* @typedef {import('../index.js').Handler} Handler
* @typedef {import('../index.js').Content} Content
*/
module.exports = listItem
import {u} from 'unist-builder'
import {all} from '../all.js'
var u = require('unist-builder')
var all = require('../all')
function listItem(h, node, parent) {
/**
* @type {Handler}
* @param {ListItem} node
* @param {List} parent
*/
export function listItem(h, node, parent) {
var result = all(h, node)
var head = result[0]
var loose = parent ? listLoose(parent) : listItemLoose(node)
/** @type {Properties} */
var props = {}
/** @type {Array.<Content>} */
var wrapped = []
var length
/** @type {number} */
var index
/** @type {Element} */
var paragraph
/** @type {Content} */
var child
if (typeof node.checked === 'boolean') {
if (!head || head.tagName !== 'p') {
head = h(null, 'p', [])
result.unshift(head)
if (
result[0] &&
result[0].type === 'element' &&
result[0].tagName === 'p'
) {
paragraph = result[0]
} else {
paragraph = h(null, 'p', [])
result.unshift(paragraph)
}
if (head.children.length > 0) {
head.children.unshift(u('text', ' '))
if (paragraph.children.length > 0) {
paragraph.children.unshift(u('text', ' '))
}
head.children.unshift(
paragraph.children.unshift(
h(null, 'input', {

@@ -41,15 +61,19 @@ type: 'checkbox',

length = result.length
index = -1
while (++index < length) {
while (++index < result.length) {
child = result[index]
// Add eols before nodes, except if this is a loose, first paragraph.
if (loose || index !== 0 || child.tagName !== 'p') {
if (
loose ||
index !== 0 ||
child.type !== 'element' ||
child.tagName !== 'p'
) {
wrapped.push(u('text', '\n'))
}
if (child.tagName === 'p' && !loose) {
wrapped = wrapped.concat(child.children)
if (child.type === 'element' && child.tagName === 'p' && !loose) {
wrapped.push(...child.children)
} else {

@@ -61,3 +85,3 @@ wrapped.push(child)

// Add a final eol.
if (length && (loose || child.tagName !== 'p')) {
if (result.length > 0 && (loose || child.tagName !== 'p')) {
wrapped.push(u('text', '\n'))

@@ -69,9 +93,12 @@ }

/**
* @param {List} node
* @return {Boolean}
*/
function listLoose(node) {
var loose = node.spread
var children = node.children
var length = children.length
var index = -1
while (!loose && ++index < length) {
while (!loose && ++index < children.length) {
loose = listItemLoose(children[index])

@@ -83,2 +110,6 @@ }

/**
* @param {ListItem} node
* @return {Boolean}
*/
function listItemLoose(node) {

@@ -85,0 +116,0 @@ var spread = node.spread

@@ -1,14 +0,20 @@

'use strict'
/**
* @typedef {import('mdast').List} List
* @typedef {import('hast').Properties} Properties
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = list
import {wrap} from '../wrap.js'
import {all} from '../all.js'
var wrap = require('../wrap')
var all = require('../all')
function list(h, node) {
/**
* @type {Handler}
* @param {List} node
*/
export function list(h, node) {
/** @type {Properties} */
var props = {}
var name = node.ordered ? 'ol' : 'ul'
var items
var items = all(h, node)
var index = -1
var length

@@ -19,10 +25,11 @@ if (typeof node.start === 'number' && node.start !== 1) {

items = all(h, node)
length = items.length
// Like GitHub, add a class for custom styling.
while (++index < length) {
while (++index < items.length) {
if (
items[index].type === 'element' &&
items[index].tagName === 'li' &&
// @ts-ignore looks like properties.
items[index].properties.className &&
items[index].properties.className.indexOf('task-list-item') !== -1
// @ts-ignore looks like properties.
items[index].properties.className.includes('task-list-item')
) {

@@ -29,0 +36,0 @@ props.className = ['contains-task-list']

@@ -1,9 +0,14 @@

'use strict'
/**
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = paragraph
import {all} from '../all.js'
var all = require('../all')
function paragraph(h, node) {
/**
* @type {Handler}
* @param {Paragraph} node
*/
export function paragraph(h, node) {
return h(node, 'p', all(h, node))
}

@@ -1,11 +0,17 @@

'use strict'
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = root
import {u} from 'unist-builder'
import {all} from '../all.js'
import {wrap} from '../wrap.js'
var u = require('unist-builder')
var wrap = require('../wrap')
var all = require('../all')
function root(h, node) {
/**
* @type {Handler}
* @param {Root} node
*/
export function root(h, node) {
// @ts-ignore top-level is a root.
return h.augment(node, u('root', wrap(all(h, node))))
}

@@ -1,9 +0,14 @@

'use strict'
/**
* @typedef {import('mdast').Strong} Strong
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = strong
import {all} from '../all.js'
var all = require('../all')
function strong(h, node) {
/**
* @type {Handler}
* @param {Strong} node
*/
export function strong(h, node) {
return h(node, 'strong', all(h, node))
}

@@ -1,19 +0,32 @@

'use strict'
/**
* @typedef {import('mdast').Table} Table
* @typedef {import('mdast').TableCell} TableCell
* @typedef {import('hast').Element} Element
* @typedef {import('../index.js').Handler} Handler
* @typedef {import('../index.js').Content} Content
*/
module.exports = table
import {pointStart, pointEnd} from 'unist-util-position'
import {wrap} from '../wrap.js'
import {all} from '../all.js'
var position = require('unist-util-position')
var wrap = require('../wrap')
var all = require('../all')
function table(h, node) {
/**
* @type {Handler}
* @param {Table} node
*/
export function table(h, node) {
var rows = node.children
var index = rows.length
var align = node.align || []
var alignLength = align.length
/** @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

@@ -24,3 +37,3 @@

name = index === 0 ? 'th' : 'td'
pos = alignLength || row.length
pos = node.align ? align.length : row.length
out = []

@@ -44,4 +57,4 @@

{
start: position.start(result[1]),
end: position.end(result[result.length - 1])
start: pointStart(result[1]),
end: pointEnd(result[result.length - 1])
},

@@ -48,0 +61,0 @@ 'tbody',

@@ -1,8 +0,13 @@

'use strict'
/**
* @typedef {import('mdast').Text} Text
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = text
import {u} from 'unist-builder'
var u = require('unist-builder')
function text(h, node) {
/**
* @type {Handler}
* @param {Text} node
*/
export function text(h, node) {
return h.augment(

@@ -9,0 +14,0 @@ node,

@@ -1,7 +0,12 @@

'use strict'
/**
* @typedef {import('mdast').ThematicBreak} ThematicBreak
* @typedef {import('../index.js').Handler} Handler
*/
module.exports = thematicBreak
function thematicBreak(h, node) {
/**
* @type {Handler}
* @param {ThematicBreak} [node]
*/
export function thematicBreak(h, node) {
return h(node, 'hr')
}

@@ -1,31 +0,86 @@

'use strict'
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('mdast').Definition} Definition
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
* @typedef {import('hast').Properties} Properties
* @typedef {import('hast').Text} Text
* @typedef {import('hast').Comment} Comment
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Root} Root
* @typedef {import('unist-util-position').NodeLike} NodeLikeWithPosition
* @typedef {import('unist-util-position').PositionLike} PositionLike
* @typedef {import('unist-util-visit').Visitor<FootnoteDefinition>} FootnoteDefinitionVisitor
*
* @typedef {Element|Text|Comment} Content
*
* @typedef EmbeddedHastFields
* @property {string} [hName] Defines the tag name of an element
* @property {Properties} [hProperties] Defines the properties of an element
* @property {Array.<Content>} [hChildren] Defines the (hast) children of an element
*
* @typedef {Object.<string, unknown> & EmbeddedHastFields} Data unist data with embedded hast fields
*
* @typedef {Node & {data?: Data}} NodeWithData unist node with embedded hast data
*
* @callback Handler
* @param {H} h Handle context
* @param {Node} node mdast node to handle
* @param {Parent|null} parent Parent of `node`
* @returns {Content|Array.<Content>|null|undefined} hast node
*
* @callback HFunctionProps
* @param {Node|PositionLike|null|undefined} node mdast node or unist position
* @param {string} tagName HTML tag name
* @param {Properties} props Properties
* @param {Array.<Content>?} [children] hast content
* @returns {Element}
*
* @callback HFunctionNoProps
* @param {Node|PositionLike|null|undefined} node mdast node or unist position
* @param {string} tagName HTML tag name
* @param {Array.<Content>?} [children] hast content
* @returns {Element}
*
* @typedef HFields
* @property {boolean} dangerous Whether HTML is allowed
* @property {(identifier: string) => Definition|null} definition Definition cache
* @property {Object.<string, FootnoteDefinition>} footnoteById Footnote cache
* @property {Array.<string>} footnoteOrder Order in which footnotes occur
* @property {Handlers} handlers Applied handlers
* @property {Handler} unknownHandler Handler for any none not in `passThrough` or otherwise handled
* @property {(left: NodeWithData|PositionLike|null|undefined, right: Content) => Content} augment Like `h` but lower-level and usable on non-elements.
* @property {Array.<string>} passThrough List of node types to pass through untouched (except for their children).
*
* @typedef Options
* @property {boolean} [allowDangerousHtml=false] Whether to allow `html` nodes and inject them as `raw` HTML
* @property {Handlers} [handlers] Object mapping mdast nodes to functions handling them
* @property {Array.<string>} [passThrough] List of custom mdast node types to pass through (keep) in hast
* @property {Handler} [unknownHandler] Handler for all unknown nodes.
*
* @typedef {Record.<string, Handler>} Handlers Map of node types to handlers
* @typedef {HFunctionProps & HFunctionNoProps & HFields} H Handle context
*/
module.exports = toHast
import {u} from 'unist-builder'
import {visit} from 'unist-util-visit'
import {pointStart, pointEnd} from 'unist-util-position'
import {generated} from 'unist-util-generated'
import {definitions} from 'mdast-util-definitions'
import {one} from './one.js'
import {footer} from './footer.js'
import {handlers} from './handlers/index.js'
var u = require('unist-builder')
var visit = require('unist-util-visit')
var position = require('unist-util-position')
var generated = require('unist-util-generated')
var definitions = require('mdast-util-definitions')
var one = require('./one')
var footer = require('./footer')
var handlers = require('./handlers')
var own = {}.hasOwnProperty
var deprecationWarningIssued = false
// Factory to transform.
/**
* Factory to transform.
* @param {Node} tree mdast node
* @param {Options} [options] Configuration
* @returns {H} `h` function
*/
function factory(tree, options) {
var settings = options || {}
// Issue a warning if the deprecated tag 'allowDangerousHTML' is used
if (settings.allowDangerousHTML !== undefined && !deprecationWarningIssued) {
deprecationWarningIssued = true
console.warn(
'mdast-util-to-hast: deprecation: `allowDangerousHTML` is nonstandard, use `allowDangerousHtml` instead'
)
}
var dangerous = settings.allowDangerousHtml || settings.allowDangerousHTML
var dangerous = settings.allowDangerousHtml || false
/** @type {Object.<string, FootnoteDefinition>} */
var footnoteById = {}

@@ -36,5 +91,6 @@

h.footnoteById = footnoteById
/** @type {Array.<string>} */
h.footnoteOrder = []
h.augment = augment
h.handlers = Object.assign({}, handlers, settings.handlers)
h.handlers = {...handlers, ...settings.handlers}
h.unknownHandler = settings.unknownHandler

@@ -45,11 +101,19 @@ h.passThrough = settings.passThrough

// @ts-ignore Hush, it’s fine!
return h
// Finalise the created `right`, a hast node, from `left`, an mdast node.
/**
* Finalise the created `right`, a hast node, from `left`, an mdast node.
* @param {(NodeWithData|PositionLike)?} left
* @param {Content} right
* @returns {Content}
*/
function augment(left, right) {
/** @type {Data} */
var data
/** @type {NodeLikeWithPosition} */
var ctx
// Handle `data.hName`, `data.hProperties, `data.hChildren`.
if (left && left.data) {
if (left && 'data' in left && left.data) {
data = left.data

@@ -71,6 +135,6 @@

if (right.type === 'element' && data.hProperties) {
right.properties = Object.assign({}, right.properties, data.hProperties)
right.properties = {...right.properties, ...data.hProperties}
}
if (right.children && data.hChildren) {
if ('children' in right && right.children && data.hChildren) {
right.children = data.hChildren

@@ -80,8 +144,8 @@ }

ctx = left && left.position ? left : {position: left}
if (left) {
// @ts-ignore looks like a node vs. position.
ctx = 'type' in left ? left : {position: left}
if (!generated(ctx)) {
right.position = {
start: position.start(ctx),
end: position.end(ctx)
if (!generated(ctx)) {
right.position = {start: pointStart(ctx), end: pointEnd(ctx)}
}

@@ -93,9 +157,9 @@ }

// Create an element for `node`.
/**
* Create an element for `node`.
*
* @type {HFunctionProps}
*/
function h(node, tagName, props, children) {
if (
(children === undefined || children === null) &&
typeof props === 'object' &&
'length' in props
) {
if (Array.isArray(props)) {
children = props

@@ -105,5 +169,6 @@ props = {}

// @ts-ignore augmenting an element yields an element.
return augment(node, {
type: 'element',
tagName: tagName,
tagName,
properties: props || {},

@@ -114,2 +179,5 @@ children: children || []

/**
* @type {FootnoteDefinitionVisitor}
*/
function onfootnotedefinition(definition) {

@@ -126,13 +194,22 @@ var id = String(definition.identifier).toUpperCase()

// Transform `tree`, which is an mdast node, to a hast node.
function toHast(tree, options) {
/**
* Transform `tree` (an mdast node) to a hast node.
*
* @param {Node} tree mdast node
* @param {Options} [options] Configuration
* @returns {Node} hast node
*/
export function toHast(tree, options) {
var h = factory(tree, options)
var node = one(h, tree)
var node = one(h, tree, null)
var foot = footer(h)
if (foot) {
node.children = node.children.concat(u('text', '\n'), foot)
// @ts-ignore If there’s a footer, there were definitions, meaning block
// content.
// So assume `node` is a parent node.
node.children.push(u('text', '\n'), foot)
}
return node
return Array.isArray(node) ? {type: 'root', children: node} : node
}

@@ -1,11 +0,19 @@

'use strict'
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('mdast').Literal} Literal
* @typedef {import('mdast').Text} Text
* @typedef {import('./index.js').H} H
* @typedef {import('./index.js').Handler} Handler
*/
module.exports = one
import {u} from 'unist-builder'
import {all} from './all.js'
var u = require('unist-builder')
var all = require('./all')
var own = {}.hasOwnProperty
// Transform an unknown node.
/**
* Transform an unknown node.
* @type {Handler}
*/
function unknown(h, node) {

@@ -19,5 +27,8 @@ if (text(node)) {

// Visit a node.
function one(h, node, parent) {
/**
* @type {Handler}
*/
export function one(h, node, parent) {
var type = node && node.type
/** @type {Handler} */
var fn

@@ -32,3 +43,3 @@

fn = h.handlers[type]
} else if (h.passThrough && h.passThrough.indexOf(type) > -1) {
} else if (h.passThrough && h.passThrough.includes(type)) {
fn = returnNode

@@ -42,3 +53,7 @@ } else {

// Check if the node should be renderered as a text node.
/**
* Check if the node should be renderered as a text node.
* @param {Node} node
* @returns {node is Literal}
*/
function text(node) {

@@ -58,12 +73,8 @@ var data = node.data || {}

/**
* @type {Handler}
*/
function returnNode(h, node) {
var clone
if (node.children) {
clone = Object.assign({}, node)
clone.children = all(h, node)
return clone
}
return node
// @ts-ignore pass through any unknown node.
return node.children ? {...node, children: all(h, node)} : node
}

@@ -1,14 +0,26 @@

'use strict'
/**
* @typedef {import('mdast').LinkReference} LinkReference
* @typedef {import('mdast').ImageReference} ImageReference
* @typedef {import('./index.js').Handler} Handler
* @typedef {import('./index.js').Content} Content
*/
module.exports = revert
import {u} from 'unist-builder'
import {all} from './all.js'
var u = require('unist-builder')
var all = require('./all')
// Return the content of a reference without definition as Markdown.
function revert(h, node) {
/**
* Return the content of a reference without definition as plain text.
*
* @type {Handler}
* @param {ImageReference|LinkReference} node
* @returns {Content|Array.<Content>}
*/
export function revert(h, node) {
var subtype = node.referenceType
var suffix = ']'
/** @type {Array.<Content>} */
var contents
/** @type {Content} */
var head
/** @type {Content} */
var tail

@@ -15,0 +27,0 @@

@@ -1,13 +0,19 @@

'use strict'
/**
* @typedef {import('./index.js').Content} Content
*/
module.exports = wrap
import {u} from 'unist-builder'
var u = require('unist-builder')
// Wrap `nodes` with line feeds between each entry.
// Optionally adds line feeds at the start and end.
function wrap(nodes, loose) {
/**
* Wrap `nodes` with line feeds between each entry.
* Optionally adds line feeds at the start and end.
*
* @param {Array.<Content>} nodes
* @param {boolean} [loose=false]
* @returns {Array.<Content>}
*/
export function wrap(nodes, loose) {
/** @type {Array.<Content>} */
var result = []
var index = -1
var length = nodes.length

@@ -18,7 +24,4 @@ if (loose) {

while (++index < length) {
if (index) {
result.push(u('text', '\n'))
}
while (++index < nodes.length) {
if (index) result.push(u('text', '\n'))
result.push(nodes[index])

@@ -25,0 +28,0 @@ }

{
"name": "mdast-util-to-hast",
"version": "10.2.0",
"version": "11.0.0",
"description": "mdast utility to transform to hast",

@@ -27,45 +27,43 @@ "license": "MIT",

],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib",
"index.js",
"types/index.d.ts"
"lib/",
"index.d.ts",
"index.js"
],
"types": "types/index.d.ts",
"dependencies": {
"@types/hast": "^2.0.0",
"@types/mdast": "^3.0.0",
"@types/mdurl": "^1.0.0",
"@types/unist": "^2.0.0",
"mdast-util-definitions": "^4.0.0",
"mdast-util-definitions": "^5.0.0",
"mdurl": "^1.0.0",
"unist-builder": "^2.0.0",
"unist-util-generated": "^1.0.0",
"unist-util-position": "^3.0.0",
"unist-util-visit": "^2.0.0"
"unist-builder": "^3.0.0",
"unist-util-generated": "^2.0.0",
"unist-util-position": "^4.0.0",
"unist-util-visit": "^3.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.37.0"
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"{lib/**/**,test/**,}*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier -w . --loglevel warn && xo --fix",
"build-bundle": "browserify index.js -s mdastUtilToHast -o mdast-util-to-hast.js",
"build-mangle": "browserify index.js -s mdastUtilToHast -o mdast-util-to-hast.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run build && npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {

@@ -81,10 +79,6 @@ "tabWidth": 2,

"prettier": true,
"esnext": false,
"ignores": [
"**/*.ts",
"mdast-util-to-hast.js"
],
"rules": {
"unicorn/explicit-length-check": "off",
"unicorn/prefer-includes": "off"
"max-depth": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}

@@ -96,3 +90,8 @@ },

]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}

@@ -17,2 +17,5 @@ # mdast-util-to-hast

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
[npm][]:

@@ -35,13 +38,12 @@

```js
var inspect = require('unist-util-inspect')
var unified = require('unified')
var parse = require('remark-parse')
var vfile = require('to-vfile')
var toHast = require('mdast-util-to-hast')
import fs from 'fs'
import fromMarkdown from 'mdast-util-from-markdown'
import {toHast} from 'mdast-util-to-hast'
import toHtml from 'hast-util-to-html'
var tree = unified()
.use(parse)
.parse(vfile.readSync('example.md'))
var mdast = fromMarkdown(fs.readFileSync('example.md'))
var hast = toHast(mdast)
var html = toHtml(hast)
console.log(inspect(toHast(tree)))
console.log(html)
```

@@ -51,9 +53,4 @@

```txt
root[1] (1:1-2:1, 0-20)
└─ element[3] (1:1-1:20, 0-19) [tagName="h2"]
├─ text: "Hello " (1:4-1:10, 3-9)
├─ element[1] (1:10-1:19, 9-18) [tagName="strong"]
│ └─ text: "World" (1:12-1:17, 11-16)
└─ text: "!" (1:19-1:20, 18-19)
```html
<h2>Hello <strong>World</strong>!</h2>
```

@@ -63,2 +60,5 @@

This package exports the following identifiers: `toHast`.
There is no default export.
### `toHast(node[, options])`

@@ -123,3 +123,3 @@

`node.data.hName` sets the tag-name of an element.
`node.data.hName` sets the tag name of an element.
The following [mdast][]:

@@ -156,3 +156,3 @@

alt: 'Big red circle on a black background',
title: null
title: null,
data: {hProperties: {className: ['responsive']}}

@@ -159,0 +159,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc