import { RstNode, type RstNodeJson, type RstNodeObject, type RstNodeSource } from '../RstNode.ts' import { removeEscapeChar } from '../../utils/removeEscapeChar.ts' import type { RstNodeRegistrar } from '../../Parser/RstNodeRegistrar.ts' import type { RstNodeType } from '../RstNodeType.ts' // ---------------------------------------------------------------------------- // MARK: Node // ---------------------------------------------------------------------------- export type RstTextData = { rawText: string } export class RstText extends RstNode { protected readonly rawText: string constructor( registrar: RstNodeRegistrar, source: RstNodeSource, rawText: string, ) { super(registrar, source) this.rawText = rawText } override toObject(): RstNodeObject { const root = super.toObject() root.text = this.textContent return root } override toJSON(): RstNodeJson { const root = super.toJSON() as RstNodeJson root.data = { rawText: this.rawText, } return root } static reviveRstNodeFromJson(registrar: RstNodeRegistrar, json: RstNodeJson): RstText { return new RstText(registrar, structuredClone(json.source), json.data.rawText) } override clone(registrar: RstNodeRegistrar): RstText { return new RstText(registrar, structuredClone(this.source), this.rawText) } override get nodeType(): RstNodeType { return 'Text' } override get isInlineNode(): boolean { return true } override get shouldExportPlainText(): boolean { // Only this parent class is plaintext (e.g. Text that subclasses this should not be considered plaintext) return this.nodeType === 'Text' } override get textContent(): string { return removeEscapeChar(this.rawText) } override get rawTextContent(): string { return this.rawText } override toShortString(): string { return `${super.toShortString()} len:${this.textContent.length}` } override toString(depth = 0): string { const selfTab = ' '.repeat(depth) const childTab = ' '.repeat(depth + 1) let str = selfTab + `[${this.toShortString()}] (${this.lineNums})\n` for (const line of this.textContent.split('\n')) { str += childTab + `"${line}"\n` } return str } } export type ContinuousText = Array