3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 22:55:44 +01:00

Answer architecture round 1: 0 🔴, 2 🟡; join empty-keys to the directive grammar and name the readers by direction

This commit is contained in:
2026-10-21 23:49:15 +02:00
committed by Lillem4n
parent b080410852
commit 3f7b3a386e
11 changed files with 46 additions and 28 deletions
+47
View File
@@ -0,0 +1,47 @@
import type { AdfMark } from '../../adf/document.ts'
import type { AttributeVocabulary } from '../../adf/attribute-vocabulary.ts'
import type { ConvertFault } from '../../result.ts'
import type { DirectiveAttributes } from './syntax.ts'
import type { MarkSpelling } from '../mark-spellings.ts'
import { directivePrefix } from './syntax.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { markSpelling } from '../mark-spellings.ts'
import { readEmptyKeys } from './empty-keys.ts'
import { readVocabulary } from './attribute-reading.ts'
export function readDirectiveMark(name: string, attributes: DirectiveAttributes, path: ConvertErrorPath): Result<AdfMark> | undefined {
const spelling = markSpelling(name)
if (spelling === undefined) return undefined
const markdown = markdownForm(spelling)
if (markdown !== undefined) return failure('unsupported-node-shape', `${name} is spelled ${markdown}, never as a directive`, path)
return readMarkAttributes(name, attributes, spelling.attributes, path)
}
export function readMarkAttributes(type: string, attributes: DirectiveAttributes, vocabulary: AttributeVocabulary, path: ConvertErrorPath): Result<AdfMark> {
const empty = readEmptyKeys(type, attributes, ['attrs'], false)
if (empty.fault !== undefined) return faulted(empty.fault, path)
const attrs = readVocabulary(type, empty.value.rest, vocabulary, undefined, path)
if (!attrs.ok) return attrs
if (empty.value.empty.has('attrs')) return success({ attrs: {}, type })
return success(Object.keys(attrs.value).length === 0 ? { type } : { attrs: attrs.value, type })
}
export function inlineMarkSpellingFault(name: string): ConvertFault | undefined {
const spelling = markSpelling(name)
if (spelling === undefined) return undefined
const directive = `${directivePrefix}${name}[…]`
const forms = spelling.kind === 'link' ? `[x](url) or ${directive}` : (markdownForm(spelling) ?? directive)
return { code: 'unsupported-node-shape', message: `${name} is spelled ${forms}, never as a block directive` }
}
function markdownForm(spelling: MarkSpelling): string | undefined {
switch (spelling.kind) {
case 'code':
return '`x`'
case 'directive':
case 'link':
return undefined
case 'emphasis':
return `${spelling.spelling}x${spelling.spelling}`
}
}