3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45: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
parent 25aa975c91
commit 53201f65eb
27 changed files with 612 additions and 295 deletions
+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.