From 00c89dd63b3de19624632b2b5ea6c0bd696adaca Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 3 Oct 2026 16:22:19 +0200 Subject: [PATCH] Give the unmarked bare text check and the text-holding-content refusal one owner each --- src/adf/document.ts | 4 ++++ src/markdown/emit/adf-to-markdown.ts | 4 ++-- src/markdown/emit/inline-line.ts | 10 ++++++++-- src/markdown/parse/directive-nodes.ts | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/adf/document.ts b/src/adf/document.ts index b84f6ff..cd5d0e6 100644 --- a/src/adf/document.ts +++ b/src/adf/document.ts @@ -69,6 +69,10 @@ export function isBareText(node: AdfNode): boolean { return node.type === 'text' && node.attrs === undefined && node.content === undefined && node.marks?.length !== 0 } +export function isUnmarkedBareText(node: AdfNode): boolean { + return isBareText(node) && node.marks === undefined +} + export function identicalMark(left: AdfMark, right: AdfMark): boolean { return marksKey([left]) === marksKey([right]) } diff --git a/src/markdown/emit/adf-to-markdown.ts b/src/markdown/emit/adf-to-markdown.ts index 936a646..5c17534 100644 --- a/src/markdown/emit/adf-to-markdown.ts +++ b/src/markdown/emit/adf-to-markdown.ts @@ -1,7 +1,7 @@ import type { AdfDocument, AdfNode } from '../../adf/document.ts' import type { BlockNodeModel } from '../../adf/block-nodes.ts' import type { Flavour } from '../plain/conventions.ts' -import { adfDocumentFault, holdsOnlyAttributes, isBareText, nodeAttrs, nodeContent } from '../../adf/document.ts' +import { adfDocumentFault, holdsOnlyAttributes, isUnmarkedBareText, nodeAttrs, nodeContent } from '../../adf/document.ts' import { alertMarker, foldedAlertMarker, leadingMarker, readAlertMarker, readTaskMarker, taskMarker } from '../plain/conventions.ts' import { blockDirectiveForm, documentSpelling, listBreakSpelling } from '../block-directive.ts' import { blockNodeModel, blockNodes } from '../../adf/block-nodes.ts' @@ -278,7 +278,7 @@ function emitCodeDirective(node: AdfNode, model: BlockNodeModel, path: ConvertEr // The text each fence holds, or `undefined` where a child is no plain text node, which the carry holds instead. function fencedTexts(node: AdfNode, path: ConvertErrorPath): Result { if (node.content === undefined) return success(['']) - if (node.content.some((child) => !isBareText(child) || child.marks !== undefined)) return success(undefined) + if (node.content.some((child) => !isUnmarkedBareText(child))) return success(undefined) const texts: string[] = [] for (const [index, child] of node.content.entries()) { const childPath = [...path, 'content', index] diff --git a/src/markdown/emit/inline-line.ts b/src/markdown/emit/inline-line.ts index 77c4afe..636b120 100644 --- a/src/markdown/emit/inline-line.ts +++ b/src/markdown/emit/inline-line.ts @@ -267,8 +267,13 @@ function emitInlineDirective(node: AdfNode, model: InlineNodeModel, index: numbe return success({ segments: [syntax(spellInlineDirectiveOpener(node.type)), ...content, syntax(`]${attributes}`)] }) } +function textHoldingContent(node: AdfNode, path: ConvertErrorPath): Result | undefined { + return node.type === 'text' && nodeContent(node).length > 0 ? failure('unsupported-node-shape', 'a text node holds no content: this one holds some', path) : undefined +} + function emitText(node: AdfNode, context: InlineContext, index: number, path: ConvertErrorPath): Result { - if (nodeContent(node).length > 0) return failure('unsupported-node-shape', 'a text node holds no content: this one holds some', path) + const holding = textHoldingContent(node, path) + if (holding !== undefined) return holding if (!isBareText(node)) return success({ carry: { first: index, last: index } }) if (typeof node.text !== 'string' || node.text === '') return failure('unsupported-node-shape', 'a text node holds text: this one has none', path) if (/\r/.test(node.text)) return failure('unspellable-character', 'a text node holds a carriage return CommonMark rewrites', path) @@ -323,7 +328,8 @@ function emitHighlight(nodes: readonly AdfNode[], depth: number, range: NodeRang function emitCodeSpan(nodes: readonly AdfNode[], depth: number, range: NodeRange, path: ConvertErrorPath): Result { const spans: string[] = [] for (const node of nodes) { - if (node.type === 'text' && nodeContent(node).length > 0) return failure('unsupported-node-shape', 'a text node holds no content: this one holds some', path) + const holding = textHoldingContent(node, path) + if (holding !== undefined) return holding if (!isBareText(node) || nodeMarks(node).length !== depth + 1) return success({ carry: range }) const { text } = node if (typeof text !== 'string' || text === '') return failure('unsupported-node-shape', 'a text node holds text: this one has none', path) diff --git a/src/markdown/parse/directive-nodes.ts b/src/markdown/parse/directive-nodes.ts index 3af6098..19afd52 100644 --- a/src/markdown/parse/directive-nodes.ts +++ b/src/markdown/parse/directive-nodes.ts @@ -3,7 +3,7 @@ import type { BlockNodeModel } from '../../adf/block-nodes.ts' import type { ConvertFault } from '../../result.ts' import type { DirectiveAttributes, DirectiveValue } from '../directive-syntax.ts' import type { Elsewhere } from './directive-attributes.ts' -import { attributeNestingMessage, isBareText } from '../../adf/document.ts' +import { attributeNestingMessage, isUnmarkedBareText } from '../../adf/document.ts' import { attributeValue, directivePrefix, spellAttributeValue, unknownDirectiveFault } from '../directive-syntax.ts' import { blockArgument, blockDirectiveForm, marksAttribute, readMarkValues } from '../block-directive.ts' import { blockNodeModel } from '../../adf/block-nodes.ts' @@ -92,7 +92,7 @@ function blockSpellingFault(name: string): ConvertFault | undefined { function slotText(content: readonly AdfNode[]): string | undefined { if (content.length === 0) return '' const only = content.length === 1 ? content[0] : undefined - if (only === undefined || !isBareText(only) || only.marks !== undefined || typeof only.text !== 'string') return undefined + if (only === undefined || !isUnmarkedBareText(only) || typeof only.text !== 'string') return undefined return only.text }