site/node_modules/mdast-util-to-hast/lib/revert.js

56 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-10-14 06:09:33 +00:00
/**
* @typedef {import('hast').ElementContent} ElementContent
*
* @typedef {import('mdast').Nodes} Nodes
* @typedef {import('mdast').Reference} Reference
*
* @typedef {import('./state.js').State} State
*/
// Make VS Code show references to the above types.
''
/**
* Return the content of a reference without definition as plain text.
*
* @param {State} state
* Info passed around.
* @param {Extract<Nodes, Reference>} node
* Reference node (image, link).
* @returns {Array<ElementContent>}
* hast content.
*/
export function revert(state, node) {
const subtype = node.referenceType
let suffix = ']'
if (subtype === 'collapsed') {
suffix += '[]'
} else if (subtype === 'full') {
suffix += '[' + (node.label || node.identifier) + ']'
}
if (node.type === 'imageReference') {
return [{type: 'text', value: '![' + node.alt + suffix}]
}
const contents = state.all(node)
const head = contents[0]
if (head && head.type === 'text') {
head.value = '[' + head.value
} else {
contents.unshift({type: 'text', value: '['})
}
const tail = contents[contents.length - 1]
if (tail && tail.type === 'text') {
tail.value += suffix
} else {
contents.push({type: 'text', value: suffix})
}
return contents
}