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

Describe each CommonMark spelling beside the emitter's spelling it names

This commit is contained in:
2026-11-14 11:06:31 +01:00
committed by Lillem4n
parent b088d4f075
commit 5d41b4be26
5 changed files with 58 additions and 41 deletions
+38 -9
View File
@@ -155,18 +155,47 @@ export function spellingMemo(): SpellingMemo {
return { ask, take }
}
type ReadableSpelling = {
// The lossless flavour's spelling in words, for the refusal of a directive that CommonMark spells.
describe: (node: AdfNode) => string
spell: (node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing) => Result<EmittedBlock> | undefined
}
const readableSpellings: ReadonlyMap<string, ReadableSpelling> = new Map<string, ReadableSpelling>([
['blockquote', {
describe: () => 'each line behind `> `',
spell: (node, path, depth, writing) => holdsOnlyAttributes(node, []) ? quotedBlocks(node, path, depth, writing, (body) => alertEscaped(body, writing)) : undefined,
}],
['bulletList', { describe: () => 'each item behind `- `, or behind `* ` where the list right before it takes `- `', spell: tryList }],
['codeBlock', {
describe: () => 'its text in a fence of more backticks than any run it holds, its language after the opening fence',
spell: (node, _path, _depth, writing) => readableText(tryCodeBlock(node, writing.flavour)),
}],
['heading', { describe: () => 'one `#` per level before its text, `## Title` at level 2', spell: (node, path, _depth, writing) => tryHeading(node, path, writing.flavour) }],
['mediaSingle', {
describe: () => '`![alt](url)`, or `[![alt](url)](href)` when linked',
spell: (node, _path, _depth, writing) => readableText(tryImage(node, writing.flavour)),
}],
['orderedList', {
describe: (node) => {
const order = String(nodeAttrs(node)['order'])
return `each item behind its number, counting up from \`${order}. \`, or from \`${order}) \` where the list right before it takes \`.\``
},
spell: tryList,
}],
['paragraph', { describe: () => 'its text alone', spell: (node, path, _depth, writing) => tryParagraph(node, path, writing.flavour) }],
['rule', { describe: () => '`---`', spell: (node) => readableText(tryRule(node)) }],
['table', { describe: () => 'a pipe table', spell: (node, path, _depth, writing) => readableText(tryPipeTable(node, path, writing.flavour)) }],
])
export function describeCommonMarkSpelling(node: AdfNode): string | undefined {
return readableSpellings.get(node.type)?.describe(node)
}
function spellReadableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
const own = writing.flavour === 'portable' ? writing.blocks.spell(node, path, depth, writing) : undefined
if (own !== undefined) return own
if (node.type === 'blockquote') return holdsOnlyAttributes(node, []) ? quotedBlocks(node, path, depth, writing, (body) => alertEscaped(body, writing)) : undefined
if (node.type === 'bulletList' || node.type === 'orderedList') return tryList(node, path, depth, writing)
if (node.type === 'codeBlock') return readableText(tryCodeBlock(node, writing.flavour))
if (node.type === 'heading') return tryHeading(node, path, writing.flavour)
if (node.type === 'mediaSingle') return readableText(tryImage(node, writing.flavour))
if (node.type === 'paragraph') return tryParagraph(node, path, writing.flavour)
if (node.type === 'rule') return readableText(tryRule(node))
if (node.type === 'table') return readableText(tryPipeTable(node, path, writing.flavour))
return undefined
return readableSpellings.get(node.type)?.spell(node, path, depth, writing)
}
function readableText(text: string | undefined): Result<EmittedBlock> | undefined {
+1 -1
View File
@@ -1,3 +1,3 @@
// The only file `parse/` imports from `emit/` (docs/decisions.md §The source parts by ADF and format).
export { commonMarkSpelling, spellingMemo, type SpellingMemo } from './adf-to-markdown.ts'
export { commonMarkSpelling, describeCommonMarkSpelling, spellingMemo, type SpellingMemo } from './adf-to-markdown.ts'
export { openingLinkTakesDirective } from './inline-line.ts'
+2 -20
View File
@@ -5,7 +5,7 @@ import type { ConvertFault } from '../../result.ts'
import type { LineContainer } from '../line-container.ts'
import type { LinkDefinitions } from '../inline-content.ts'
import { carryFenceType, readCarriedBlock } from '../opaque-carry.ts'
import { commonMarkSpelling, spellingMemo, type SpellingMemo } from '../emit/spelling-questions.ts'
import { commonMarkSpelling, describeCommonMarkSpelling, spellingMemo, type SpellingMemo } from '../emit/spelling-questions.ts'
import { documentAttribute, documentName, documentSpelling } from '../directive/block.ts'
import { emptyKeys, nodeAttrs } from '../../adf/document.ts'
import { failure, faulted, positioned, success, type ConvertErrorPath, type ParseError, type Result } from '../../result.ts'
@@ -132,28 +132,10 @@ function directiveNode(block: DirectiveBlock, reading: Reading, path: ConvertErr
const readable = withinEngineLimits(() => commonMarkSpelling(built.value, path, depth, { flavour: 'lossless', memo: reading.memo }))
if (!readable.ok) return readable
if (!readable.value) return built
const spelling = spellingToWrite(built.value)
const spelling = describeCommonMarkSpelling(built.value)
return failure('unsupported-node-shape', `${built.value.type} takes the CommonMark spelling, not the directive form${spelling === undefined ? '' : `: write ${spelling}`}`, path)
}
function spellingToWrite(node: AdfNode): string | undefined {
if (node.type !== 'orderedList') return commonMarkSpellings.get(node.type)
const order = String(nodeAttrs(node)['order'])
return `each item behind its number, counting up from \`${order}. \`, or from \`${order}) \` where the list right before it takes \`.\``
}
// One entry per node `commonMarkSpelling` spells in the lossless flavour, kept here because this refusal is its only reader; a node missing here is refused naming no spelling.
const commonMarkSpellings: ReadonlyMap<string, string> = new Map([
['blockquote', 'each line behind `> `'],
['bulletList', 'each item behind `- `, or behind `* ` where the list right before it takes `- `'],
['codeBlock', 'its text in a fence of more backticks than any run it holds, its language after the opening fence'],
['heading', 'one `#` per level before its text, `## Title` at level 2'],
['mediaSingle', '`![alt](url)`, or `[![alt](url)](href)` when linked'],
['paragraph', 'its text alone'],
['rule', '`---`'],
['table', 'a pipe table'],
])
function directiveBody(read: BlockDirectiveNode, blocks: Block[] | undefined, reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode> {
const { contentModel, node } = read
if (blocks === undefined) return success(node)