squash-text-nodes.ts
ink/squash-text-nodes.ts
93
Lines
2293
Bytes
2
Exports
2
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with ui-flow. It contains 93 lines, 2 detected imports, and 2 detected exports.
Important relationships
Detected exports
StyledSegmentsquashTextNodesToSegments
Keywords
childnodetextnodenamestylesnodetextstylesinheritedhyperlinkmergedstyleselsesquashtextnodes
Detected imports
./dom.js./styles.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import type { DOMElement } from './dom.js'
import type { TextStyles } from './styles.js'
/**
* A segment of text with its associated styles.
* Used for structured rendering without ANSI string transforms.
*/
export type StyledSegment = {
text: string
styles: TextStyles
hyperlink?: string
}
/**
* Squash text nodes into styled segments, propagating styles down through the tree.
* This allows structured styling without relying on ANSI string transforms.
*/
export function squashTextNodesToSegments(
node: DOMElement,
inheritedStyles: TextStyles = {},
inheritedHyperlink?: string,
out: StyledSegment[] = [],
): StyledSegment[] {
const mergedStyles = node.textStyles
? { ...inheritedStyles, ...node.textStyles }
: inheritedStyles
for (const childNode of node.childNodes) {
if (childNode === undefined) {
continue
}
if (childNode.nodeName === '#text') {
if (childNode.nodeValue.length > 0) {
out.push({
text: childNode.nodeValue,
styles: mergedStyles,
hyperlink: inheritedHyperlink,
})
}
} else if (
childNode.nodeName === 'ink-text' ||
childNode.nodeName === 'ink-virtual-text'
) {
squashTextNodesToSegments(
childNode,
mergedStyles,
inheritedHyperlink,
out,
)
} else if (childNode.nodeName === 'ink-link') {
const href = childNode.attributes['href'] as string | undefined
squashTextNodesToSegments(
childNode,
mergedStyles,
href || inheritedHyperlink,
out,
)
}
}
return out
}
/**
* Squash text nodes into a plain string (without styles).
* Used for text measurement in layout calculations.
*/
function squashTextNodes(node: DOMElement): string {
let text = ''
for (const childNode of node.childNodes) {
if (childNode === undefined) {
continue
}
if (childNode.nodeName === '#text') {
text += childNode.nodeValue
} else if (
childNode.nodeName === 'ink-text' ||
childNode.nodeName === 'ink-virtual-text'
) {
text += squashTextNodes(childNode)
} else if (childNode.nodeName === 'ink-link') {
text += squashTextNodes(childNode)
}
}
return text
}
export default squashTextNodes