site/node_modules/mdast-util-to-hast/lib/handlers/link.js

38 lines
843 B
JavaScript
Raw Permalink Normal View History

2024-10-14 06:09:33 +00:00
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Properties} Properties
* @typedef {import('mdast').Link} Link
* @typedef {import('../state.js').State} State
*/
import {normalizeUri} from 'micromark-util-sanitize-uri'
/**
* Turn an mdast `link` node into hast.
*
* @param {State} state
* Info passed around.
* @param {Link} node
* mdast node.
* @returns {Element}
* hast node.
*/
export function link(state, node) {
/** @type {Properties} */
const properties = {href: normalizeUri(node.url)}
if (node.title !== null && node.title !== undefined) {
properties.title = node.title
}
/** @type {Element} */
const result = {
type: 'element',
tagName: 'a',
properties,
children: state.all(node)
}
state.patch(node, result)
return state.applyData(node, result)
}