3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-21 07:32:58 +01:00

Carry what no section or mark spelling writes, and drop the mark refusal

This commit is contained in:
2026-08-27 10:38:34 +02:00
parent f164da90f5
commit 61726d6e70
18 changed files with 459 additions and 199 deletions
+13 -16
View File
@@ -1,7 +1,6 @@
import type { AdfMark, AdfNode, AttributeVocabulary } from './adf-document.ts'
import type { JsonValue } from './json-value.ts'
import { failure, success, type ConvertErrorPath, type Result } from './result.ts'
import { attributeFailure, isBareToken, spellAttributes, spellJsonAttribute, vocabularyPairs } from './directive-attributes.ts'
import { isBareToken, spellAttributes, spellJsonAttribute, vocabularyPairs } from './directive-attributes.ts'
export type BlockDirective = {
argument?: string
@@ -80,24 +79,22 @@ export function blockDirective(type: string): BlockDirective | undefined {
return Object.hasOwn(blockDirectives, type) ? blockDirectives[type] : undefined
}
export function spellDirectiveHeader(node: AdfNode, directive: BlockDirective, path: ConvertErrorPath): Result<string> {
const argument = spellArgument(node, directive, path)
if (!argument.ok) return argument
const spelled = vocabularyPairs(node.attrs ?? {}, directive.attributes, directive.argument)
if (spelled.fault !== undefined) return attributeFailure(node.type, spelled.fault, path)
export function spellDirectiveHeader(node: AdfNode, directive: BlockDirective): string | undefined {
const argument = spellArgument(node, directive)
if (argument === undefined) return undefined
const pairs = vocabularyPairs(node.attrs ?? {}, directive.attributes, directive.argument)
if (pairs === undefined) return undefined
const marks = node.marks ?? []
if (marks.length > 0) spelled.pairs.push(['marks', spellJsonAttribute(markValues(marks))])
const attributes = spellAttributes(spelled.pairs)
return success(`${node.type}${argument.value}${attributes === '' ? '' : ` ${attributes}`}`)
if (marks.length > 0) pairs.push(['marks', spellJsonAttribute(markValues(marks))])
const attributes = spellAttributes(pairs)
return `${node.type}${argument}${attributes === '' ? '' : ` ${attributes}`}`
}
function spellArgument(node: AdfNode, directive: BlockDirective, path: ConvertErrorPath): Result<string> {
function spellArgument(node: AdfNode, directive: BlockDirective): string | undefined {
const value = directive.argument === undefined ? undefined : node.attrs?.[directive.argument]
if (value === undefined) return success('')
if (typeof value !== 'string' || !isBareToken(value)) {
return failure('unspelled-node-attribute', `the ${node.type} attribute ${directive.argument} holds no bare token the arg slot spells`, path)
}
return success(` ${value}`)
if (value === undefined) return ''
if (typeof value !== 'string' || !isBareToken(value)) return undefined
return ` ${value}`
}
function markValues(marks: readonly AdfMark[]): JsonValue {