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

Answer principles round 1: 3 🔴, 8 🟡 (0 misread); attributes as data-*, carried content as paragraphs, links and notes, no character HTML cannot hold, mutants killed

This commit is contained in:
2026-10-23 19:19:14 +02:00
committed by Lillem4n
parent 1f46111fe4
commit 7a38db2563
27 changed files with 612 additions and 295 deletions
+16 -1
View File
@@ -2,8 +2,9 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import vm from 'node:vm'
import type { AdfDocument } from './document.ts'
import type { JsonValue } from '../json-value.ts'
import { adfDocumentFault, isAdfDocument } from './document.ts'
import { adfDocumentFault, isAdfDocument, writableContent } from './document.ts'
import { largestNesting } from '../nesting.ts'
function fault(value: unknown): string {
@@ -114,3 +115,17 @@ test('refuses an object holding itself, and accepts one shared', () => {
const values = { b: 1 }
assert.equal(fault({ content: [shared, shared, { attrs: { a: [values, values] }, type: 'paragraph' }], type: 'doc', version: 1 }), 'accepted')
})
test('opens every writer with the same checks: a document, version 1, and no more text than the longest output holds', () => {
const checked = (document: AdfDocument): string => {
const result = writableContent(document)
return result.ok ? JSON.stringify(result.value) ?? 'no content key' : result.error.code
}
assert.equal(checked({ type: 'doc', version: Number.NaN }), 'not-an-adf-document')
assert.equal(checked({ type: 'doc', version: 2 }), 'unsupported-document-version')
assert.equal(checked({ type: 'doc', version: 1 }), 'no content key')
assert.equal(checked({ content: [{ type: 'rule' }], type: 'doc', version: 1 }), '[{"type":"rule"}]')
const long = 'a'.repeat(2 ** 28)
const text = { content: [{ text: long, type: 'text' }, { text: long, type: 'text' }, { text: long, type: 'text' }], type: 'paragraph' }
assert.equal(checked({ content: [text], type: 'doc', version: 1 }), 'unsupported-node-shape')
})
+55 -6
View File
@@ -1,7 +1,43 @@
import type { AdfNode } from './document.ts'
import type { JsonValue } from '../json-value.ts'
import { blockNodeModel } from './block-nodes.ts'
import { nodeAttrs } from './document.ts'
export function isBlockNodeType(type: string): boolean {
return blockNodeModel(type) !== undefined || type === 'blockCard' || type === 'embedCard'
}
// Whether the text a node shows stands apart from the text beside it; media shows inline, as an image in a line does.
export function partsShownText(type: string): boolean {
return isBlockNodeType(type) && type !== 'media'
}
export function oneLine(text: string): string {
return text.replace(/[\r\u0000]/g, '').replace(/\n/g, ' ')
}
// The name of what a node references where it shows no text of its own, for a note standing in its place.
export function referenceName(node: AdfNode): string | undefined {
switch (node.type) {
case 'blockCard':
case 'embedCard':
case 'inlineCard':
return 'link card'
case 'extension':
case 'inlineExtension': {
const key = visibleText(nodeAttrs(node)['extensionKey'])
return key === undefined ? 'extension' : oneLine(key).trim()
}
case 'media':
case 'mediaInline':
return 'image'
case 'syncBlock':
return 'synced block'
default:
return undefined
}
}
// The text a node shows in place of itself, read from the attribute holding its content; `undefined` where it holds none.
export function shownText(node: AdfNode): string | undefined {
const attrs = nodeAttrs(node)
@@ -9,32 +45,45 @@ export function shownText(node: AdfNode): string | undefined {
case 'blockCard':
case 'embedCard':
case 'inlineCard':
return visibleText(attrs['url']) ?? visibleText(cardData(node)['name']) ?? cardUrl(node)
return visibleText(attrs['url']) ?? visibleText(cardData(node)['name']) ?? shownUrl(node)
case 'date':
return isoDate(attrs['timestamp'])
case 'emoji':
return visibleText(attrs['text']) ?? stringValue(attrs['shortName'])
case 'expand':
case 'nestedExpand':
case 'status':
return stringValue(node.type === 'status' ? attrs['text'] : attrs['title'])
return stringValue(attrs['title'])
case 'extension':
case 'inlineExtension':
return visibleText(attrs['text'])
case 'media':
case 'mediaInline':
return visibleText(attrs['alt']) ?? (attrs['type'] === 'external' ? visibleText(attrs['url']) : undefined)
return visibleText(attrs['alt']) ?? shownUrl(node)
case 'mention': {
const id = attrs['id']
return visibleText(attrs['text']) ?? (typeof id === 'string' && id !== '' ? `@${id}` : undefined)
}
case 'status':
return stringValue(attrs['text'])
default:
return undefined
}
}
export function cardUrl(card: AdfNode): string | undefined {
return visibleText(nodeAttrs(card)['url']) ?? visibleText(cardData(card)['url'])
// The URL a card or an external image links a reader to; `undefined` for every other node.
export function shownUrl(node: AdfNode): string | undefined {
const attrs = nodeAttrs(node)
switch (node.type) {
case 'blockCard':
case 'embedCard':
case 'inlineCard':
return visibleText(attrs['url']) ?? visibleText(cardData(node)['url'])
case 'media':
case 'mediaInline':
return attrs['type'] === 'external' ? visibleText(attrs['url']) : undefined
default:
return undefined
}
}
// `undefined` where the value is no string or holds only whitespace and null characters.