astro-ghostcms/.pnpm-store/v3/files/60/b65f526b3688fc49beee2e6efa0...

47 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
/**
* @typedef {import('hast').Comment} Comment
* @typedef {import('hast').Parents} Parents
*
* @typedef {import('../index.js').State} State
*/
import {stringifyEntities} from 'stringify-entities'
/**
* Serialize a comment.
*
* @param {Comment} node
* Node to handle.
* @param {number | undefined} _1
* Index of `node` in `parent.
* @param {Parents | undefined} _2
* Parent of `node`.
* @param {State} state
* Info passed around about the current state.
* @returns {string}
* Serialized node.
*/
export function comment(node, _1, _2, state) {
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
return state.settings.bogusComments
? '<?' +
stringifyEntities(
node.value,
Object.assign({}, state.settings.characterReferences, {subset: ['>']})
) +
'>'
: '<!--' + node.value.replace(/^>|^->|<!--|-->|--!>|<!-$/g, encode) + '-->'
/**
* @param {string} $0
*/
function encode($0) {
return stringifyEntities(
$0,
Object.assign({}, state.settings.characterReferences, {
subset: ['<', '>']
})
)
}
}