Files
site/node_modules/mdast-util-to-markdown/lib/handle/emphasis.js
T
site-admin 3ac81d5a52
deploy / build-and-publish (push) Successful in 1m37s
the editable site (validation-lab)
2026-11-21 08:03:55 +01:00

70 lines
1.6 KiB
JavaScript

/**
* @import {AttentionInfo, Info, State} from 'mdast-util-to-markdown'
* @import {Emphasis, Parents} from 'mdast'
*/
import {checkEmphasis} from '../util/check-emphasis.js'
import {attentionItem, serialize} from '../util/container-phrasing.js'
emphasis.attention = attention
emphasis.peek = peek
/**
* Serialize an emphasis node.
*
* Attention is properly handled by `containerPhrasing`.
*
* @param {Emphasis} node
* Node to serialize.
* @param {Parents | undefined} _
* Parent node.
* @param {State} state
* Info passed around.
* @param {Info} info
* Info on surrounding context.
* @returns {string}
* Serialized markdown.
*/
export function emphasis(node, _, state, info) {
const exit = state.enter('phrasing')
const item = attentionItem(node, attention, state, state.createTracker(info))
const value = item ? serialize([item], info.before, info.after) : ''
exit()
return value
}
/**
* Serialize an emphasis node as attention.
*
* @param {Emphasis} _
* Node to serialize.
* @param {State} state
* Info passed around.
* @returns {AttentionInfo}
* Info on how to serialize the node.
*/
function attention(_, state) {
const marker = checkEmphasis(state)
return {
construct: 'emphasis',
markers: marker === '*' ? ['*', '_'] : ['_', '*'],
sizes: [1]
}
}
/**
* Peek at this node.
*
* @param {Emphasis} _
* Node to serialize.
* @param {Parents | undefined} _1
* Parent node.
* @param {State} state
* Info passed around.
* @returns {string}
* Start of the serialized markdown.
*/
function peek(_, _1, state) {
return state.options.emphasis || '*'
}