27 lines
823 B
JavaScript
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
|
|
}
|