Terrace/docs/read-page/helpers.js
Joshua Bemenderfer 9d9757e868 Updates.
2025-09-08 16:24:38 -04:00

27 lines
823 B
JavaScript

export async function contentAsText(parentNode, includeCurrent = false) {
const linesAsArray = []
if (includeCurrent) linesAsArray.push(parentNode.content)
let contentDepth = includeCurrent ? parentNode.level : -1
for await (const child of parentNode.children()) {
if (contentDepth === -1) contentDepth = child.level
const indent = ''.padStart(child.level - contentDepth, ' ')
linesAsArray.push(indent + child.content.trimEnd())
}
return linesAsArray.join('\n')
}
// New helper for getting all child content as structured data
export async function getChildrenByType(parentNode) {
const result = {}
for await (const child of parentNode.children()) {
if (!child.head) continue
if (!result[child.head]) result[child.head] = []
result[child.head].push(child)
}
return result
}