mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 22:55:44 +01:00
Split adfToPlainText's step and line-feed helpers out of its writer
This commit is contained in:
@@ -42,8 +42,7 @@ function writeText(content: readonly AdfNode[]): Result<string> {
|
|||||||
let literal = 0
|
let literal = 0
|
||||||
|
|
||||||
function emit(text: string): void {
|
function emit(text: string): void {
|
||||||
let end = text.length
|
const end = endBeforeLineFeeds(text)
|
||||||
while (end > 0 && text[end - 1] === '\n') end -= 1
|
|
||||||
if (end > 0) {
|
if (end > 0) {
|
||||||
const owed = Math.max(gap, literal)
|
const owed = Math.max(gap, literal)
|
||||||
const head = length === 0 || owed > 0 ? `${length === 0 ? '' : '\n'.repeat(owed)}${lead ?? indent}` : ''
|
const head = length === 0 || owed > 0 ? `${length === 0 ? '' : '\n'.repeat(owed)}${lead ?? indent}` : ''
|
||||||
@@ -52,9 +51,8 @@ function writeText(content: readonly AdfNode[]): Result<string> {
|
|||||||
length += head.length + body.length
|
length += head.length + body.length
|
||||||
gap = 0
|
gap = 0
|
||||||
lead = undefined
|
lead = undefined
|
||||||
literal = 0
|
|
||||||
}
|
}
|
||||||
literal += text.length - end
|
literal = (end > 0 ? 0 : literal) + text.length - end
|
||||||
}
|
}
|
||||||
|
|
||||||
const pending: Step[] = childSteps(content, { level: 1, lines: 2 }, 0, undefined, () => undefined).reverse()
|
const pending: Step[] = childSteps(content, { level: 1, lines: 2 }, 0, undefined, () => undefined).reverse()
|
||||||
@@ -88,6 +86,17 @@ function writeText(content: readonly AdfNode[]): Result<string> {
|
|||||||
return success(pieces.join(''))
|
return success(pieces.join(''))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function endBeforeLineFeeds(text: string): number {
|
||||||
|
let end = text.length
|
||||||
|
while (end > 0 && text[end - 1] === '\n') end -= 1
|
||||||
|
return end
|
||||||
|
}
|
||||||
|
|
||||||
|
function stepsWithin(node: AdfNode, depth: number, place: Place | undefined): Step[] {
|
||||||
|
const layout = layoutOf(node)
|
||||||
|
return childSteps(nodeContent(node), layout, depth + layout.level, place, markerFor(node))
|
||||||
|
}
|
||||||
|
|
||||||
// The steps for `nodes` in document order, `layout.lines` line feeds apart.
|
// The steps for `nodes` in document order, `layout.lines` line feeds apart.
|
||||||
function childSteps(nodes: readonly AdfNode[], layout: Layout, depth: number, parent: Place | undefined, markerFor: (index: number, node: AdfNode) => string | undefined): Step[] {
|
function childSteps(nodes: readonly AdfNode[], layout: Layout, depth: number, parent: Place | undefined, markerFor: (index: number, node: AdfNode) => string | undefined): Step[] {
|
||||||
const steps: Step[] = []
|
const steps: Step[] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user