mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 06:35:44 +01:00
Answer architecture round 6: each block is held to the bound as it is built, the reader's ask takes the engine guard, and the chunk ships under a worse comprehension ruling by the maintainer's call, with item 110 to fill the memo in one place
This commit is contained in:
@@ -64,23 +64,6 @@ test('refuses lines their quotes would prefix past V8\'s longest string', () =>
|
||||
assert.equal(markdown(adfToLosslessMarkdown(document({ content: [code, code], type: 'blockquote' }))), tooLong)
|
||||
})
|
||||
|
||||
test('refuses a directive whose opener and body together would pass V8\'s longest string', () => {
|
||||
const long = 'a'.repeat(2 ** 28)
|
||||
const expand: AdfNode = { attrs: { title: long }, content: [{ content: [{ text: long, type: 'text' }], type: 'codeBlock' }], type: 'expand' }
|
||||
assert.equal(markdown(adfToLosslessMarkdown(document(expand))), tooLong)
|
||||
})
|
||||
|
||||
test('refuses a list whose item markers would carry its lines past V8\'s longest string', () => {
|
||||
const item: AdfNode = { content: [nearlyHalfTheBound()], type: 'listItem' }
|
||||
assert.equal(markdown(adfToLosslessMarkdown(document({ content: [item, item], type: 'bulletList' }))), tooLong)
|
||||
})
|
||||
|
||||
test('refuses a directive opener whose attributes together would pass V8\'s longest string', () => {
|
||||
const long = 'a'.repeat(2 ** 28)
|
||||
const panel: AdfNode = { attrs: { panelColor: long, panelIcon: long, panelIconText: long, panelType: 'custom' }, content: [paragraph({ text: 'x', type: 'text' })], type: 'panel' }
|
||||
assert.equal(markdown(adfToLosslessMarkdown(document(panel))), tooLong)
|
||||
})
|
||||
|
||||
test('refuses text past V8\'s longest string', () => {
|
||||
const long = 'a'.repeat(2 ** 28)
|
||||
const result = adfToLosslessMarkdown(document(paragraph({ text: long, type: 'text' }, { text: long, type: 'text' }, { text: long, type: 'text' })))
|
||||
|
||||
@@ -28,7 +28,6 @@ type PlacedBlocks = { blocks: readonly PlacedBlock[]; headroom: number }
|
||||
// CommonMark starts a new list where the marker changes, so every second list in a run of one kind takes `alternateText`.
|
||||
type SpelledBlock = { spelling: 'commonmark' | 'directive'; text: string } | { alternateText: string; spelling: 'list'; text: string }
|
||||
// Keyed by reference: only a caller building one object per position (the parse, the portable reduction) passes one; a consumer's document may share a node.
|
||||
// Holds the spelling of each node the CommonMark ask was put to, until the walk above reads it.
|
||||
export type SpellingMemo = Map<AdfNode, KeptSpelling>
|
||||
type WalkedItem = { node: AdfNode; walk: PlacedBlocks }
|
||||
export type Writing = { flavour: WrittenFlavour; memo: SpellingMemo | undefined }
|
||||
@@ -63,6 +62,9 @@ function walkBlocks(nodes: readonly AdfNode[], path: ConvertErrorPath, depth: nu
|
||||
for (const [index, node] of nodes.entries()) {
|
||||
const block = emitBlock(node, [...path, 'content', index], depth, writing)
|
||||
if (!block.ok) return block
|
||||
// V8 throws building a block past the bound; an engine holding longer strings stops at the same block.
|
||||
const fits = withinLongest(block.value.text)
|
||||
if (!fits.ok) return fits
|
||||
headroom = Math.min(headroom, block.value.headroom)
|
||||
blocks.push({ ...block.value, node })
|
||||
}
|
||||
@@ -113,19 +115,19 @@ function emitBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing
|
||||
|
||||
export function commonMarkSpelling(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing & { memo: SpellingMemo }): Result<null> | undefined {
|
||||
const readable = readableBlock(node, path, depth, writing)
|
||||
if (readable === undefined) writing.memo.set(node, { block: undefined, depth })
|
||||
else if (readable.ok) writing.memo.set(node, { block: readable.value, depth })
|
||||
if (readable === undefined) return undefined
|
||||
if (readable === undefined) {
|
||||
writing.memo.set(node, { block: undefined, depth })
|
||||
return undefined
|
||||
}
|
||||
if (!readable.ok) return readable
|
||||
// V8 throws past the bound while building the spelling; an engine holding longer strings refuses here alike.
|
||||
if (readable.value.text.length > longestMarkdown) return tooLong()
|
||||
const fits = withinLongest(readable.value.text)
|
||||
if (!fits.ok) return fits
|
||||
writing.memo.set(node, { block: readable.value, depth })
|
||||
return readable.value.spelling === 'directive' ? undefined : success(null)
|
||||
}
|
||||
|
||||
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
|
||||
const kept = writing.memo?.get(node)
|
||||
// The walk reading an entry spells the node into its own text, so the entry is dropped: kept, every level of a deep subtree would hold its whole text.
|
||||
writing.memo?.delete(node)
|
||||
const kept = takeKeptSpelling(writing.memo, node)
|
||||
if (kept !== undefined) {
|
||||
if (kept.block === undefined) return undefined
|
||||
// An entry holds the spelling at its fill depth. A read at or above it rebases headroom; a read below re-spells, because a hit skips the walk's depth guards.
|
||||
@@ -134,6 +136,13 @@ function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, wri
|
||||
return spellReadableBlock(node, path, depth, writing)
|
||||
}
|
||||
|
||||
// The CommonMark ask fills an entry; the walk above reads it once and spells it into its own text, so kept, every level of a deep subtree would hold its whole text.
|
||||
function takeKeptSpelling(memo: SpellingMemo | undefined, node: AdfNode): KeptSpelling | undefined {
|
||||
const kept = memo?.get(node)
|
||||
memo?.delete(node)
|
||||
return kept
|
||||
}
|
||||
|
||||
function spellReadableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
|
||||
const portable = writing.flavour === 'portable' ? spellPortableBlock(node, path, depth, writing) : undefined
|
||||
if (portable !== undefined) return portable
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { AdfNode } from '../adf/document.ts'
|
||||
import type { DirectiveSpan, Read } from './directive-syntax.ts'
|
||||
import type { JsonSpelling } from '../canonical-json.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { joinedWithin } from './length-bound.ts'
|
||||
import { canonicalJsonPieces, spellsCanonicalJson } from '../canonical-json.ts'
|
||||
import { directiveEscape, malformedDirective, readSoleStringAttribute, spellAttributes, spellDirectiveOpener, spellInlineLeafDirective, spellStringAttribute, unsupportedNodeShape } from './directive-syntax.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../result.ts'
|
||||
@@ -10,6 +9,7 @@ import { fencedCodeBlock } from './commonmark/backtick-runs.ts'
|
||||
import { infoStringCarries } from './commonmark/grammar.ts'
|
||||
import { isAdfNode } from '../adf/document.ts'
|
||||
import { isJsonValue, nestingDepth, overNested } from '../json-value.ts'
|
||||
import { joinedWithin } from './length-bound.ts'
|
||||
import { largestNesting } from '../nesting.ts'
|
||||
import { parseJsonText } from '../json-text.ts'
|
||||
|
||||
|
||||
@@ -262,9 +262,10 @@ function directiveNode(block: DirectiveBlock, reading: Reading, path: ConvertErr
|
||||
if (!read.ok) return read
|
||||
const built = directiveBody(read.value, block.blocks, reading, path, depth)
|
||||
if (!built.ok) return built
|
||||
const readable = commonMarkSpelling(built.value, path, depth, { flavour: 'lossless', memo: reading.memo })
|
||||
if (readable === undefined) return built
|
||||
// Guarded here, so V8's throw past the bound refuses at the directive as another engine's check does.
|
||||
const readable = withinEngineLimits<null | undefined>(() => commonMarkSpelling(built.value, path, depth, { flavour: 'lossless', memo: reading.memo }) ?? success(undefined))
|
||||
if (!readable.ok) return readable
|
||||
if (readable.value === undefined) return built
|
||||
return failure('unsupported-node-shape', `${built.value.type} takes the CommonMark spelling, not the directive form`, path)
|
||||
}
|
||||
|
||||
|
||||
@@ -76,12 +76,6 @@ test('refuses a paragraph whose text would pass V8\'s longest string, joining no
|
||||
assert.equal(portableDocument(document(merged)), 'unsupported-node-shape at /')
|
||||
})
|
||||
|
||||
// Two code blocks of 300 lines, some 1000 characters short of V8's longest string, so only the quote's prefixes pass it.
|
||||
test('refuses an expand whose quoted lines would pass V8\'s longest string', () => {
|
||||
const code: AdfNode = { content: [text(`${'a'.repeat(894_782)}\n`.repeat(300))], type: 'codeBlock' }
|
||||
assert.equal(portableDocument(document(node('expand', { title: 'x' }, code, code))), 'unsupported-node-shape at /')
|
||||
})
|
||||
|
||||
test('refuses nesting past 500 levels wherever the reduction walks', () => {
|
||||
const lowest = (bottom: AdfNode): string => {
|
||||
let deep = bottom
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import { adfDocumentFault, heldTextLength, nodeAttrs, nodeContent, nodeMarks } from '../../adf/document.ts'
|
||||
import { blockNodeModel, isTaskItem } from '../../adf/block-nodes.ts'
|
||||
import { longestMarkdown, tooLong, withinEngineLimits } from '../length-bound.ts'
|
||||
import { centeredImage, externalMedia } from '../external-image.ts'
|
||||
import { commonMarkSpelling, largestListMarker, writeMarkdown, type SpellingMemo } from '../emit/adf-to-markdown.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
@@ -11,6 +10,7 @@ import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { linkDefinition } from '../mark-spellings.ts'
|
||||
import { longestMarkdown, tooLong, withinEngineLimits } from '../length-bound.ts'
|
||||
import { splitTaskList } from './task-list.ts'
|
||||
import { toEditorNormal } from './editor-normal.ts'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user