mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-20 07:05:44 +01:00
Answer architecture round 1: the reader returns its refusals and owns its stack, a decision and a test keep JSON.parse out, item 83 filed, the scoring run recorded
This commit is contained in:
@@ -3,7 +3,7 @@ import type { ConvertFault } from '../result.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { backslashEscape, holdsNullCharacter } from './commonmark/grammar.ts'
|
||||
import { backtickRun, closingBacktickRun } from './commonmark/backtick-runs.ts'
|
||||
import { isJsonValue, overNested } from '../json-value.ts'
|
||||
import { overNested } from '../json-value.ts'
|
||||
import { largestNesting } from '../nesting.ts'
|
||||
import { parseJsonText } from '../json-text.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
@@ -61,7 +61,7 @@ const prefixFault = `an unescaped ${directivePrefix} completes no directive; ${d
|
||||
export function attributeValue(text: string, kind: AttributeKind): AttributeReading {
|
||||
if (kind === 'string') return { value: { kind, value: text } }
|
||||
if (kind === 'boolean') return text === 'true' || text === 'false' ? { value: { kind, value: text === 'true' } } : { refusal: 'kind' }
|
||||
const parsed = parseJson(text)
|
||||
const parsed = parseJsonText(text).value
|
||||
if (parsed === undefined) return { refusal: 'kind' }
|
||||
if (kind === 'number') return typeof parsed === 'number' ? { value: { kind, value: parsed } } : { refusal: 'kind' }
|
||||
return overNested(parsed) ? { refusal: 'nesting' } : { value: { kind, value: parsed } }
|
||||
@@ -325,12 +325,8 @@ function readQuotedValue(text: string, index: number): Read<{ end: number; value
|
||||
if (character !== undefined) {
|
||||
return { fault: malformedDirective(`a raw ${character} inside {attrs} breaks the directive: spell it \\u${escapeDigits(character)}`) }
|
||||
}
|
||||
const parsed = parseJson(spelling)
|
||||
const parsed = parseJsonText(spelling).value
|
||||
if (typeof parsed !== 'string') return { fault: malformedDirective('the {attrs} quoted value is not a JSON string') }
|
||||
return { value: { end: cursor + 1, value: { decoded: parsed, spelling } } }
|
||||
}
|
||||
|
||||
function parseJson(raw: string): JsonValue | undefined {
|
||||
const value = parseJsonText(raw)?.value
|
||||
return value !== undefined && isJsonValue(value) ? value : undefined
|
||||
}
|
||||
|
||||
@@ -65,9 +65,10 @@ function carriedJson(node: AdfNode, spelled: object, spelling: JsonSpelling, pat
|
||||
function readCarriedJson(raw: string, spelling: JsonSpelling, levels: number, type?: string): Read<AdfNode> {
|
||||
const wayOut = type === undefined ? directiveEscape : fenceEscape(type)
|
||||
const parsed = parseJsonText(raw)
|
||||
if (parsed === undefined) return { fault: malformedDirective(`the opaque carry holds invalid JSON; ${wayOut}`) }
|
||||
if (parsed.refusal !== undefined) {
|
||||
return { fault: parsed.refusal === 'syntax' ? malformedDirective(`the opaque carry holds invalid JSON; ${wayOut}`) : unsupportedNodeShape('the opaque carry holds a number JSON cannot spell') }
|
||||
}
|
||||
const { value } = parsed
|
||||
if (!isJsonValue(value)) return { fault: unsupportedNodeShape('the opaque carry holds a number JSON cannot spell') }
|
||||
const typed = type === undefined || type === '' ? { value } : typedValue(value, type)
|
||||
if (typed.fault !== undefined) return typed
|
||||
const held = typed.value
|
||||
|
||||
@@ -405,6 +405,13 @@ test('reads the inline carry back to the node its json attribute holds', () => {
|
||||
])
|
||||
})
|
||||
|
||||
test('reads an escaped JSON key as the key it spells after reading a key holding a backslash (https://issues.chromium.org/issues/521080746)', () => {
|
||||
const extension = (parameters: string): string => `!adf:extension {extensionKey=toc extensionType=x parameters=${JSON.stringify(parameters)}}\n`
|
||||
const read = content(markdownToAdf([extension('{"\\b":1,"\\f":2}'), extension('{"\\b":3,"\\\\":4}'), extension('{"\\b":1,"\\f":2}')].join('\n')))
|
||||
const node = (parameters: AdfAttributes): AdfNode => ({ attrs: { extensionKey: 'toc', extensionType: 'x', parameters }, type: 'extension' })
|
||||
assert.deepEqual(read, [node({ '\b': 1, '\f': 2 }), node({ '\b': 3, '\\': 4 }), node({ '\b': 1, '\f': 2 })])
|
||||
})
|
||||
|
||||
test('names the invalid JSON no opaque carry holds, and the spelling that keeps it literal', () => {
|
||||
const invalid = 'malformed-directive: the opaque carry holds invalid JSON; '
|
||||
const fence = `${invalid}!adf:codeBlock {language="adf:x"} around a bare fence keeps it a code block`
|
||||
|
||||
Reference in New Issue
Block a user