Urara-Blog/node_modules/.pnpm-store/v3/files/76/5dba93bd97f4cb4f72f2202ce8a42c0aeb9bb4114432465e88110d3dec088c48538938b1b2f346411a22d7c41bed9ff1a79431a60d2307d91c3b350cd15740
2022-08-14 01:14:53 +08:00

87 lines
2.3 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @typedef {import('../types.js').Node} Node
* @typedef {import('../types.js').Parent} Parent
* @typedef {import('../types.js').SafeOptions} SafeOptions
* @typedef {import('../types.js').Context} Context
*/
import {track} from './track.js'
/**
* @param {Parent} parent
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function containerPhrasing(parent, context, safeOptions) {
const indexStack = context.indexStack
const children = parent.children || []
/** @type {Array<string>} */
const results = []
let index = -1
let before = safeOptions.before
indexStack.push(-1)
let tracker = track(safeOptions)
while (++index < children.length) {
const child = children[index]
/** @type {string} */
let after
indexStack[indexStack.length - 1] = index
if (index + 1 < children.length) {
// @ts-expect-error: hush, its actually a `zwitch`.
let handle = context.handle.handlers[children[index + 1].type]
if (handle && handle.peek) handle = handle.peek
after = handle
? handle(children[index + 1], parent, context, {
before: '',
after: '',
...tracker.current()
}).charAt(0)
: ''
} else {
after = safeOptions.after
}
// In some cases, html (text) can be found in phrasing right after an eol.
// When wed serialize that, in most cases that would be seen as html
// (flow).
// As we cant escape or so to prevent it from happening, we take a somewhat
// reasonable approach: replace that eol with a space.
// See: <https://github.com/syntax-tree/mdast-util-to-markdown/issues/15>
if (
results.length > 0 &&
(before === '\r' || before === '\n') &&
child.type === 'html'
) {
results[results.length - 1] = results[results.length - 1].replace(
/(\r?\n|\r)$/,
' '
)
before = ' '
// To do: does this work to reset tracker?
tracker = track(safeOptions)
tracker.move(results.join(''))
}
results.push(
tracker.move(
context.handle(child, parent, context, {
...tracker.current(),
before,
after
})
)
)
before = results[results.length - 1].slice(-1)
}
indexStack.pop()
return results.join('')
}