mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 14:15:44 +01:00
Give the directive grammar, flavours and reader failures one place each
This commit is contained in:
@@ -6,7 +6,7 @@ import { readFileSync } from 'node:fs'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { AttributeKind, AttributeVocabulary } from '../adf/attribute-vocabulary.ts'
|
||||
import { blockArgument } from '../markdown/block-directive.ts'
|
||||
import { blockArgument } from '../markdown/directive/block.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { codeTextMarkTypes, inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { markVocabularies } from '../adf/mark-attributes.ts'
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { AttributeKind, AttributeVocabulary } from '../adf/attribute-vocabu
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { blockMarkAttributes, markAttributes, markVocabularies } from '../adf/mark-attributes.ts'
|
||||
import { textDirectiveName } from '../markdown/text-directive.ts'
|
||||
import { textDirectiveName } from '../markdown/directive/text.ts'
|
||||
|
||||
type Declared = { attributes: AttributeVocabulary }
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import type { Result } from '../result.ts'
|
||||
import { adfDocument, attributes, blockTaskItemChildren, jsonKey, jsonValue, markdownPieces, propertyRuns, propertyTimeout, textOf } from './property-harness.ts'
|
||||
import { adfToLosslessMarkdown } from '../markdown/emit/adf-to-markdown.ts'
|
||||
import { adfToPortableMarkdown } from '../markdown/portable/adf-to-portable-markdown.ts'
|
||||
import { blockArgument, marksAttribute } from '../markdown/block-directive.ts'
|
||||
import { blockArgument, marksAttribute } from '../markdown/directive/block.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { carryName } from '../markdown/opaque-carry.ts'
|
||||
import { commonMarkToAdf, losslessMarkdownToAdf, portableMarkdownToAdf } from '../markdown/parse/markdown-to-adf.ts'
|
||||
@@ -24,14 +24,14 @@ import {
|
||||
spellJsonAttribute,
|
||||
spellStringAttribute,
|
||||
spellVocabulary,
|
||||
} from '../markdown/directive-syntax.ts'
|
||||
} from '../markdown/directive/syntax.ts'
|
||||
import { fencedCodeBlock } from '../markdown/commonmark/backtick-runs.ts'
|
||||
import { inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { markAttributes } from '../adf/mark-attributes.ts'
|
||||
import { markSpelling } from '../markdown/mark-spellings.ts'
|
||||
import { nodeContent, nodeMarks } from '../adf/document.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
import { textDirectiveName } from '../markdown/text-directive.ts'
|
||||
import { textDirectiveName } from '../markdown/directive/text.ts'
|
||||
import { vocabularyPairs } from '../adf/attribute-vocabulary.ts'
|
||||
|
||||
type Choice = { arbitrary: Arbitrary<string>; hostile?: true; weight: number }
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { readdirSync, readFileSync } from 'node:fs'
|
||||
import test from 'node:test'
|
||||
|
||||
const markdown = join(dirname(fileURLToPath(import.meta.url)), '..', 'markdown')
|
||||
const importSpecifier = /\bfrom '([^']+)'/g
|
||||
|
||||
function importsOf(directory: string): { file: string; specifier: string }[] {
|
||||
const imports: { file: string; specifier: string }[] = []
|
||||
for (const file of readdirSync(join(markdown, directory)).filter((name) => name.endsWith('.ts') && !name.endsWith('.test.ts'))) {
|
||||
for (const match of readFileSync(join(markdown, directory, file), 'utf8').matchAll(importSpecifier)) {
|
||||
imports.push({ file: `${directory}/${file}`, specifier: match[1] ?? '' })
|
||||
}
|
||||
}
|
||||
return imports
|
||||
}
|
||||
|
||||
// docs/decisions.md §The source parts by ADF and format
|
||||
test('parse/ asks emit/ only through the module that names its questions', () => {
|
||||
const reached = importsOf('parse').filter(({ specifier }) => specifier.startsWith('../emit/'))
|
||||
assert.ok(reached.length > 0)
|
||||
assert.deepEqual(
|
||||
reached.filter(({ specifier }) => specifier !== '../emit/spelling-questions.ts'),
|
||||
[],
|
||||
)
|
||||
})
|
||||
@@ -7,11 +7,11 @@ import type { Arbitrary } from 'fast-check'
|
||||
import type { AttributeKind, AttributeVocabulary } from '../adf/attribute-vocabulary.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import type { LinkDefinition } from '../markdown/commonmark/link-syntax.ts'
|
||||
import { blockArgument } from '../markdown/block-directive.ts'
|
||||
import { blockArgument } from '../markdown/directive/block.ts'
|
||||
import { blockMarkAttributes, markAttributes } from '../adf/mark-attributes.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { centeredImage, externalMedia } from '../markdown/external-image.ts'
|
||||
import { directivePrefix } from '../markdown/directive-syntax.ts'
|
||||
import { directivePrefix } from '../markdown/directive/syntax.ts'
|
||||
import { emptyKeys, mergeAdjacentText, nodeContent } from '../adf/document.ts'
|
||||
import { inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { joinsWhenRead } from '../markdown/adjacent-text.ts'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { AdfNode } from '../adf/document.ts'
|
||||
import { identicalMarks, isSpellableText, nodeMarks } from '../adf/document.ts'
|
||||
import { spellInlineLeafDirective } from './directive-syntax.ts'
|
||||
import { spellInlineLeafDirective } from './directive/syntax.ts'
|
||||
|
||||
export const textBreakName = 'textBreak'
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Claims } from './portable/conventions.ts'
|
||||
import type { Claims } from './flavour.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { carryFenceType } from './opaque-carry.ts'
|
||||
import { infoStringCarries } from './commonmark/grammar.ts'
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { AdfAttributes } from '../../adf/document.ts'
|
||||
import type { AttributeVocabulary } from '../../adf/attribute-vocabulary.ts'
|
||||
import type { DirectiveAttributes } from '../directive-syntax.ts'
|
||||
import type { DirectiveAttributes } from './syntax.ts'
|
||||
import { attributeNestingMessage } from '../../adf/document.ts'
|
||||
import { attributeValue, spellAttributeValue } from '../directive-syntax.ts'
|
||||
import { attributeValue, spellAttributeValue } from './syntax.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
|
||||
export type Elsewhere = { key: string; slot: 'argument' | 'content' }
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { BlockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import { attributeNestingMessage, nodeAttrs, nodeMarks } from '../../adf/document.ts'
|
||||
import { blockArgument, marksAttribute, spellBlockMarks } from '../block-directive.ts'
|
||||
import { blockArgument, marksAttribute, spellBlockMarks } from './block.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { isBareToken, spellAttributes, spellDirectiveOpener, spellVocabulary } from '../directive-syntax.ts'
|
||||
import { isBareToken, spellAttributes, spellDirectiveOpener, spellVocabulary } from './syntax.ts'
|
||||
import { spellEmptyKeys } from '../empty-keys.ts'
|
||||
import { vocabularyPairs } from '../../adf/attribute-vocabulary.ts'
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { AdfMark } from '../adf/document.ts'
|
||||
import type { BlockType } from '../adf/block-nodes.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { blockNodeModel } from '../adf/block-nodes.ts'
|
||||
import { isAdfMark } from '../adf/document.ts'
|
||||
import { markVocabularies } from '../adf/mark-attributes.ts'
|
||||
import { overNested } from '../json-value.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
import { spellAttributes, spellDirectiveOpener, spellJsonAttribute, spellStringAttribute } from './directive-syntax.ts'
|
||||
import { spellMarkVocabulary } from './mark-spellings.ts'
|
||||
import type { AdfMark } from '../../adf/document.ts'
|
||||
import type { BlockType } from '../../adf/block-nodes.ts'
|
||||
import type { JsonValue } from '../../json-value.ts'
|
||||
import { blockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import { isAdfMark } from '../../adf/document.ts'
|
||||
import { markVocabularies } from '../../adf/mark-attributes.ts'
|
||||
import { overNested } from '../../json-value.ts'
|
||||
import { serializeCanonicalJson } from '../../canonical-json.ts'
|
||||
import { spellAttributes, spellDirectiveOpener, spellJsonAttribute, spellStringAttribute } from './syntax.ts'
|
||||
import { spellMarkVocabulary } from '../mark-spellings.ts'
|
||||
|
||||
const argumentByType = new Map(
|
||||
Object.entries({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { InlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import { nodeAttrs } from '../../adf/document.ts'
|
||||
import { spellAttributes, spellVocabulary } from '../directive-syntax.ts'
|
||||
import { spellAttributes, spellVocabulary } from './syntax.ts'
|
||||
import { spellEmptyKeys } from '../empty-keys.ts'
|
||||
import { vocabularyPairs } from '../../adf/attribute-vocabulary.ts'
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { AdfMark } from '../../adf/document.ts'
|
||||
import type { AttributeVocabulary } from '../../adf/attribute-vocabulary.ts'
|
||||
import type { ConvertFault } from '../../result.ts'
|
||||
import type { DirectiveAttributes } from '../directive-syntax.ts'
|
||||
import type { DirectiveAttributes } from './syntax.ts'
|
||||
import type { MarkSpelling } from '../mark-spellings.ts'
|
||||
import { directivePrefix } from '../directive-syntax.ts'
|
||||
import { directivePrefix } from './syntax.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { markSpelling } from '../mark-spellings.ts'
|
||||
import { readEmptyKeys } from '../empty-keys.ts'
|
||||
import { readVocabulary } from './directive-attributes.ts'
|
||||
import { readVocabulary } from './attributes.ts'
|
||||
|
||||
export function readDirectiveMark(name: string, attributes: DirectiveAttributes, path: ConvertErrorPath): Result<AdfMark> | undefined {
|
||||
const spelling = markSpelling(name)
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import type { AdfAttributes, AdfMark, AdfNode, EmptyKey } from '../../adf/document.ts'
|
||||
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 type { DirectiveAttributes, DirectiveValue } from './syntax.ts'
|
||||
import type { Elsewhere } from './attributes.ts'
|
||||
import { attributeNestingMessage, isUnmarkedSpellableText } from '../../adf/document.ts'
|
||||
import { attributePairCause, attributeValue, directivePrefix, readAttributes, readDirectiveName, unknownDirectiveFault } from '../directive-syntax.ts'
|
||||
import { blockArgument, blockDirectiveForm, marksAttribute, readMarkValues, spellBlockMarks } from '../block-directive.ts'
|
||||
import { attributePairCause, attributeValue, directivePrefix, readAttributes, readDirectiveName, unknownDirectiveFault } from './syntax.ts'
|
||||
import { blockArgument, blockDirectiveForm, marksAttribute, readMarkValues, spellBlockMarks } from './block.ts'
|
||||
import { blockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import { carryFencePrefix, carryName } from '../opaque-carry.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { inlineMarkSpellingFault, readMarkAttributes } from './directive-marks.ts'
|
||||
import { inlineMarkSpellingFault, readMarkAttributes } from './marks.ts'
|
||||
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import { markVocabularies } from '../../adf/mark-attributes.ts'
|
||||
import { readEmptyKeys, spellsEmpty } from '../empty-keys.ts'
|
||||
import { readVocabulary } from './directive-attributes.ts'
|
||||
import { slotFault } from '../directive-syntax.ts'
|
||||
import { readVocabulary } from './attributes.ts'
|
||||
import { slotFault } from './syntax.ts'
|
||||
import { textBreakName } from '../adjacent-text.ts'
|
||||
import { textDirectiveName } from '../text-directive.ts'
|
||||
import { textDirectiveName } from './text.ts'
|
||||
|
||||
export type BlockDirectiveNode = { contentModel: BlockNodeModel['contentModel']; node: AdfNode }
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { DirectiveAttributes, DirectiveLine } from './directive-syntax.ts'
|
||||
import { claimDirectivePrefix, readDirectiveLine, readInlineDirective } from './directive-syntax.ts'
|
||||
import { inlineSource } from './inline-tokens.ts'
|
||||
import { largestNesting } from '../nesting.ts'
|
||||
import type { DirectiveAttributes, DirectiveLine } from './syntax.ts'
|
||||
import { claimDirectivePrefix, readDirectiveLine, readInlineDirective } from './syntax.ts'
|
||||
import { inlineSource } from '../inline-tokens.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
|
||||
// A pair the input spells bare decodes to itself; a quoted one names its spelling beside the decoding.
|
||||
type Pair = [string, string, string?]
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import type { AttributeKind, VocabularyPair, VocabularyValue } from '../adf/attribute-vocabulary.ts'
|
||||
import type { CodeSpanCloser } from './commonmark/backtick-runs.ts'
|
||||
import type { ConvertFault } from '../result.ts'
|
||||
import type { InlineSource } from './inline-tokens.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { backslashEscape, holdsNullCharacter } from './commonmark/grammar.ts'
|
||||
import { backtickRun } from './commonmark/backtick-runs.ts'
|
||||
import { isNumberList } from '../adf/attribute-vocabulary.ts'
|
||||
import { overNested } from '../json-value.ts'
|
||||
import { largestNesting } from '../nesting.ts'
|
||||
import { parseJsonText } from '../json-text.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
import type { AttributeKind, VocabularyPair, VocabularyValue } from '../../adf/attribute-vocabulary.ts'
|
||||
import type { CodeSpanCloser } from '../commonmark/backtick-runs.ts'
|
||||
import type { ConvertFault, Read } from '../../result.ts'
|
||||
import type { InlineSource } from '../inline-tokens.ts'
|
||||
import type { JsonValue } from '../../json-value.ts'
|
||||
import { backslashEscape, holdsNullCharacter } from '../commonmark/grammar.ts'
|
||||
import { backtickRun } from '../commonmark/backtick-runs.ts'
|
||||
import { isNumberList } from '../../adf/attribute-vocabulary.ts'
|
||||
import { overNested } from '../../json-value.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { parseJsonText } from '../../json-text.ts'
|
||||
import { serializeCanonicalJson } from '../../canonical-json.ts'
|
||||
|
||||
export type AttributeReading = { refusal: 'kind' | 'nesting'; value?: undefined } | { refusal?: undefined; value: VocabularyValue }
|
||||
|
||||
@@ -31,8 +31,6 @@ export type DirectiveSpan = { attributes: DirectiveAttributes; content: string |
|
||||
|
||||
export type NestedSpans = ReadonlyMap<number, DirectiveSpan>
|
||||
|
||||
export type Read<T> = { fault: ConvertFault; value?: undefined } | { fault?: undefined; value: T }
|
||||
|
||||
type AttributePair = { end: number; key: string; value: DirectiveValue }
|
||||
|
||||
type Content = { content: string | undefined; end: number; spans: NestedSpans }
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { DirectiveSpan, Read } from './directive-syntax.ts'
|
||||
import { readSoleStringAttribute, spellAttributes, spellInlineLeafDirective, spellStringAttribute, unsupportedNodeShape } from './directive-syntax.ts'
|
||||
import type { DirectiveSpan } from './syntax.ts'
|
||||
import type { Read } from '../../result.ts'
|
||||
import { readSoleStringAttribute, spellAttributes, spellInlineLeafDirective, spellStringAttribute, unsupportedNodeShape } from './syntax.ts'
|
||||
|
||||
const name = 'text'
|
||||
const spelledRun = /^(?:[ \t]+|\n+|\0+)$/
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import type { BlockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import type { WrittenFlavour } from '../portable/conventions.ts'
|
||||
import type { WrittenFlavour } from '../flavour.ts'
|
||||
import { adfDocumentFault, heldTextLength, holdsOnlyAttributes, isUnmarkedSpellableText, nodeAttrs, nodeContent } from '../../adf/document.ts'
|
||||
import { alertMarker, flavourClaims, foldedAlertMarker, leadingMarker, readAlertMarker, readTaskMarker, taskMarker } from '../portable/conventions.ts'
|
||||
import { blockDirectiveForm, documentSpelling } from '../block-directive.ts'
|
||||
import { alertMarker, foldedAlertMarker, leadingMarker, readAlertMarker, readTaskMarker, taskMarker } from '../portable/conventions.ts'
|
||||
import { blockDirectiveForm, documentSpelling } from '../directive/block.ts'
|
||||
import { blockNodeModel, blockNodes, isTaskItem } from '../../adf/block-nodes.ts'
|
||||
import { carriedBlock } from '../opaque-carry.ts'
|
||||
import { emitInlineLine } from './inline-line.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { fencedCodeBlock } from '../commonmark/backtick-runs.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { holdsNullCharacter, isBlankLine, isThematicBreak, markerInterruptsParagraph } from '../commonmark/grammar.ts'
|
||||
import { languageSlot, type LanguageSlot } from '../code-language.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { longestMarkdown, tooLong, withinEngineLimits, withinLongest } from '../length-bound.ts'
|
||||
import { spellBlockDirectiveOpener } from './block-directive-spelling.ts'
|
||||
import { spellDirectiveCloser, spellDirectiveOpener } from '../directive-syntax.ts'
|
||||
import { spellBlockDirectiveOpener } from '../directive/block-spelling.ts'
|
||||
import { spellDirectiveCloser, spellDirectiveOpener } from '../directive/syntax.ts'
|
||||
import { tryImage } from './image.ts'
|
||||
import { tryPipeTable } from './pipe-table.ts'
|
||||
|
||||
|
||||
@@ -2,22 +2,23 @@ import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { InlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
|
||||
import type { WrittenFlavour } from '../portable/conventions.ts'
|
||||
import type { WrittenFlavour } from '../flavour.ts'
|
||||
import { assembleInlineLine, isSyntax, type InlineEscaping, type InlineSegment, type MarkRun, type NodeRange } from './line-escaping.ts'
|
||||
import { carriedInline } from '../opaque-carry.ts'
|
||||
import { commonMarkLink, linkHref, markSpelling, spellMarkAttributes } from '../mark-spellings.ts'
|
||||
import { escapeUnbalanced, spellDestination, spellLinkTarget } from '../commonmark/link-syntax.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { flavourClaims, highlightDelimiter } from '../portable/conventions.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { highlightDelimiter } from '../portable/conventions.ts'
|
||||
import { holdsNullCharacter, trimTrailingSpace } from '../commonmark/grammar.ts'
|
||||
import { identicalMark, isSpellableText, nodeAttrs, nodeContent, nodeMarks } from '../../adf/document.ts'
|
||||
import { inlineNodeModel, takenMarkTypes } from '../../adf/inline-nodes.ts'
|
||||
import { joinsWhenRead, textBreakSpelling } from '../adjacent-text.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { longestBacktickRun } from '../commonmark/backtick-runs.ts'
|
||||
import { slotFault, spellInlineDirectiveOpener, spellInlineLeafDirective } from '../directive-syntax.ts'
|
||||
import { spellInlineNodeAttributes } from './inline-directive-spelling.ts'
|
||||
import { spellTextDirective } from '../text-directive.ts'
|
||||
import { slotFault, spellInlineDirectiveOpener, spellInlineLeafDirective } from '../directive/syntax.ts'
|
||||
import { spellInlineNodeAttributes } from '../directive/inline-spelling.ts'
|
||||
import { spellTextDirective } from '../directive/text.ts'
|
||||
|
||||
type EmittedLine = { line: string; openingLinkAsDirective: boolean; segments: InlineSegment[] }
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import type { Claims, WrittenFlavour } from '../portable/conventions.ts'
|
||||
import type { Claims, WrittenFlavour } from '../flavour.ts'
|
||||
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import { delimiterFlags, isWordCharacter, matchEmphasis, runLength } from '../commonmark/emphasis-matching.ts'
|
||||
import { delimiterRunToken, inlineSource, readInlineToken } from '../inline-tokens.ts'
|
||||
import { escapesLineClaim, type LinePosition } from '../commonmark/grammar.ts'
|
||||
import { flavourClaims, highlightFlanking } from '../portable/conventions.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { highlightFlanking } from '../portable/conventions.ts'
|
||||
import { isBareDelimiterRow } from '../pipe-table-syntax.ts'
|
||||
import { opensLinkDefinition } from '../commonmark/link-reference-definitions.ts'
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { ConvertErrorPath } from '../../result.ts'
|
||||
import type { WrittenFlavour } from '../portable/conventions.ts'
|
||||
import type { WrittenFlavour } from '../flavour.ts'
|
||||
import { holdsOnlyAttributes, nodeContent } from '../../adf/document.ts'
|
||||
import { spellPipeDelimiter, spellPipeRow } from '../pipe-table-syntax.ts'
|
||||
import { tryPipeCell } from './inline-line.ts'
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// The only file `parse/` imports from `emit/` (docs/decisions.md §The source parts by ADF and format).
|
||||
export { commonMarkSpelling, type SpellingMemo } from './adf-to-markdown.ts'
|
||||
export { openingLinkTakesDirective } from './inline-line.ts'
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { DirectiveAttributes, DirectiveValue, Read } from './directive-syntax.ts'
|
||||
import type { DirectiveAttributes, DirectiveValue } from './directive/syntax.ts'
|
||||
import type { EmptyKey } from '../adf/document.ts'
|
||||
import type { Read } from '../result.ts'
|
||||
import { emptyKeys } from '../adf/document.ts'
|
||||
import { unsupportedNodeShape } from './directive-syntax.ts'
|
||||
import { unsupportedNodeShape } from './directive/syntax.ts'
|
||||
|
||||
type EmptyKeysRead = { empty: ReadonlySet<EmptyKey>; rest: Map<string, DirectiveValue> }
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
export type Flavour = 'commonmark' | 'lossless' | 'portable'
|
||||
|
||||
export type WrittenFlavour = Exclude<Flavour, 'commonmark'>
|
||||
|
||||
// What a reader reads past CommonMark, and so what its writer escapes (docs/decisions.md §Each call names its flavour).
|
||||
export type Claims = {
|
||||
alerts: boolean
|
||||
carryFence: boolean
|
||||
directives: boolean
|
||||
highlights: boolean
|
||||
// `gfm`: a row may drop its leading `|`, and an alignment colon is read and dropped; `flavour`: every row opens with `|`, the delimiter row `-` runs only.
|
||||
pipeTables: 'flavour' | 'gfm' | false
|
||||
strikethrough: boolean
|
||||
taskMarkers: boolean
|
||||
}
|
||||
|
||||
type WrittenClaims = Claims & { pipeTables: 'flavour' | 'gfm'; strikethrough: true }
|
||||
|
||||
// Directives exclude alerts and highlights: an expand directive leaves `inExpand` unset and a directive's slot reads `==`.
|
||||
type ReadClaims = Claims & ({ directives: false } | { alerts: false; directives: true; highlights: false })
|
||||
|
||||
export const flavourClaims: Readonly<{ commonmark: ReadClaims } & Record<WrittenFlavour, ReadClaims & WrittenClaims>> = {
|
||||
commonmark: { alerts: false, carryFence: false, directives: false, highlights: false, pipeTables: false, strikethrough: false, taskMarkers: false },
|
||||
lossless: { alerts: false, carryFence: true, directives: true, highlights: false, pipeTables: 'flavour', strikethrough: true, taskMarkers: false },
|
||||
portable: { alerts: true, carryFence: false, directives: false, highlights: true, pipeTables: 'gfm', strikethrough: true, taskMarkers: true },
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { adfToLosslessMarkdown } from './emit/adf-to-markdown.ts'
|
||||
import { flavourClaims } from './portable/conventions.ts'
|
||||
import { flavourClaims } from './flavour.ts'
|
||||
import { inlineSource, readInlineToken } from './inline-tokens.ts'
|
||||
import { losslessMarkdownToAdf } from './parse/markdown-to-adf.ts'
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { ClaimedPrefix } from './directive-syntax.ts'
|
||||
import type { Claims } from './portable/conventions.ts'
|
||||
import type { ClaimedPrefix } from './directive/syntax.ts'
|
||||
import type { Claims } from './flavour.ts'
|
||||
import type { CodeSpanCloser } from './commonmark/backtick-runs.ts'
|
||||
import { backslashEscape, inlineHtmlConstruct, readBracketedAutolink, readEmailAutolink } from './commonmark/grammar.ts'
|
||||
import { backtickCloser, backtickRun } from './commonmark/backtick-runs.ts'
|
||||
import { claimDirectivePrefix } from './directive-syntax.ts'
|
||||
import { claimDirectivePrefix } from './directive/syntax.ts'
|
||||
import { delimiterFlags, runLength } from './commonmark/emphasis-matching.ts'
|
||||
import { highlightDelimiter, highlightFlanking } from './portable/conventions.ts'
|
||||
import { readEntityReference } from './commonmark/entity-references.ts'
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { LinkDefinition } from './commonmark/link-syntax.ts'
|
||||
import type { MarkType } from '../adf/mark-attributes.ts'
|
||||
import { emptyKeys, nodeAttrs, nodeMarks } from '../adf/document.ts'
|
||||
import { escapeUnbalanced, spellLinkTarget } from './commonmark/link-syntax.ts'
|
||||
import { holdsDirectivePrefix, spellAttributes, spellVocabulary } from './directive-syntax.ts'
|
||||
import { holdsDirectivePrefix, spellAttributes, spellVocabulary } from './directive/syntax.ts'
|
||||
import { holdsEntityReference } from './commonmark/entity-references.ts'
|
||||
import { isAutolink } from './commonmark/grammar.ts'
|
||||
import { isMarkType, markAttributes } from '../adf/mark-attributes.ts'
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { AdfNode } from '../adf/document.ts'
|
||||
import type { DirectiveSpan, Read } from './directive-syntax.ts'
|
||||
import type { DirectiveSpan } from './directive/syntax.ts'
|
||||
import type { JsonSpelling } from '../canonical-json.ts'
|
||||
import type { JsonValue } from '../json-value.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'
|
||||
import { directiveEscape, malformedDirective, readSoleStringAttribute, spellAttributes, spellDirectiveOpener, spellInlineLeafDirective, spellStringAttribute, unsupportedNodeShape } from './directive/syntax.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Read, type Result } from '../result.ts'
|
||||
import { fencedCodeBlock } from './commonmark/backtick-runs.ts'
|
||||
import { infoStringCarries } from './commonmark/grammar.ts'
|
||||
import { isAdfNode } from '../adf/document.ts'
|
||||
|
||||
@@ -3,15 +3,14 @@ import test from 'node:test'
|
||||
|
||||
import type { Block } from './blocks.ts'
|
||||
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
|
||||
import { flavourClaims } from '../portable/conventions.ts'
|
||||
import { parseBlocks } from './blocks.ts'
|
||||
|
||||
function definitions(markdown: string): [string, LinkDefinition][] {
|
||||
return [...parseBlocks(markdown, flavourClaims.lossless).definitions]
|
||||
return [...parseBlocks(markdown, 'lossless').definitions]
|
||||
}
|
||||
|
||||
function kinds(markdown: string): string[] {
|
||||
return parseBlocks(markdown, flavourClaims.lossless).blocks.map((block) => block.kind)
|
||||
return parseBlocks(markdown, 'lossless').blocks.map((block) => block.kind)
|
||||
}
|
||||
|
||||
function faults(markdown: string): string[] {
|
||||
@@ -24,7 +23,7 @@ function faults(markdown: string): string[] {
|
||||
if (block.kind === 'bulletList' || block.kind === 'orderedList') for (const item of block.items) walk(item)
|
||||
}
|
||||
}
|
||||
walk(parseBlocks(markdown, flavourClaims.lossless).blocks)
|
||||
walk(parseBlocks(markdown, 'lossless').blocks)
|
||||
return messages
|
||||
}
|
||||
|
||||
@@ -90,7 +89,7 @@ test('opens a container where the content model takes content, and holds it open
|
||||
assert.deepEqual(faults('!adf:panel info\n> Part.\n!adf:/panel\n'), [])
|
||||
assert.deepEqual(faults('!adf:panel info\n- !adf:panel warning\n Part.\n !adf:/panel\n!adf:/panel\n'), [])
|
||||
assert.deepEqual(faults('!adf:panel info\n```\n!adf:/panel\n```\n!adf:/panel\n'), [])
|
||||
assert.deepEqual(parseBlocks('!adf:panel info {panelColor="#ff0000"}\nPart.\n!adf:/panel\n', flavourClaims.lossless).blocks, [
|
||||
assert.deepEqual(parseBlocks('!adf:panel info {panelColor="#ff0000"}\nPart.\n!adf:/panel\n', 'lossless').blocks, [
|
||||
{
|
||||
argument: 'info',
|
||||
attributes: new Map([['panelColor', { decoded: '#ff0000', spelling: '"#ff0000"' }]]),
|
||||
@@ -100,7 +99,7 @@ test('opens a container where the content model takes content, and holds it open
|
||||
position: { line: 1, offset: 0 },
|
||||
},
|
||||
])
|
||||
assert.deepEqual(parseBlocks('!adf:rule\n', flavourClaims.lossless).blocks, [
|
||||
assert.deepEqual(parseBlocks('!adf:rule\n', 'lossless').blocks, [
|
||||
{ argument: undefined, attributes: new Map(), blocks: undefined, kind: 'directive', name: 'rule', position: { line: 1, offset: 0 } },
|
||||
])
|
||||
})
|
||||
@@ -125,7 +124,7 @@ test("releases an indented code block's held blank lines as a walk rather than a
|
||||
|
||||
// A level is one list item.
|
||||
function deepestItem(markdown: string): { depth: number; kinds: string[] } {
|
||||
let blocks = parseBlocks(markdown, flavourClaims.lossless).blocks
|
||||
let blocks = parseBlocks(markdown, 'lossless').blocks
|
||||
let depth = 0
|
||||
for (let list = blocks[0]; list?.kind === 'bulletList'; list = blocks[0]) {
|
||||
blocks = list.items.at(-1) ?? []
|
||||
@@ -148,7 +147,7 @@ test('stops a blank line at the first blockquote', () => {
|
||||
|
||||
test('stops a blank line at an empty item, and gives a code block in an item the blank line without its spaces', () => {
|
||||
assert.deepEqual(deepestItem('- - \n\n a\n'), { depth: 2, kinds: [] })
|
||||
assert.deepEqual(parseBlocks('- ```\n \n x\n ```\n', flavourClaims.lossless).blocks.map((block) => block.kind === 'bulletList' && block.items[0]?.[0]), [
|
||||
assert.deepEqual(parseBlocks('- ```\n \n x\n ```\n', 'lossless').blocks.map((block) => block.kind === 'bulletList' && block.items[0]?.[0]), [
|
||||
{ kind: 'code', language: '', position: { line: 1, offset: 0 }, text: '\nx' },
|
||||
])
|
||||
})
|
||||
@@ -166,7 +165,7 @@ function outline(blocks: readonly Block[]): string {
|
||||
}
|
||||
|
||||
test('reads a marker a tab indents four or more columns into its container as text', () => {
|
||||
const read = (markdown: string): string => outline(parseBlocks(markdown, flavourClaims.lossless).blocks)
|
||||
const read = (markdown: string): string => outline(parseBlocks(markdown, 'lossless').blocks)
|
||||
assert.equal(read('> a\n \t> b\n'), 'quote(paragraph(a|> b))')
|
||||
assert.equal(read('> > a\n>\t\t> b\n'), 'quote(quote(paragraph(a|> b)))')
|
||||
assert.equal(read('- > a\n\t\t> b\n'), 'list(quote(paragraph(a|> b)))')
|
||||
@@ -174,7 +173,7 @@ test('reads a marker a tab indents four or more columns into its container as te
|
||||
})
|
||||
|
||||
test("keeps as spaces the columns a tab holds past an item's indentation", () => {
|
||||
const read = (markdown: string): string => outline(parseBlocks(markdown, flavourClaims.lossless).blocks)
|
||||
const read = (markdown: string): string => outline(parseBlocks(markdown, 'lossless').blocks)
|
||||
assert.equal(read('- ```\n\tx\n ```\n'), 'list(code(" x"))')
|
||||
assert.equal(read('- a\n \t- b\n'), 'list(paragraph(a) list(paragraph(b)))')
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Claims } from '../portable/conventions.ts'
|
||||
import type { Claims, Flavour } from '../flavour.ts'
|
||||
import type { ConvertFault, SourcePosition } from '../../result.ts'
|
||||
import type { DirectiveAttributes, DirectiveLine } from '../directive-syntax.ts'
|
||||
import type { DirectiveAttributes, DirectiveLine } from '../directive/syntax.ts'
|
||||
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
|
||||
import {
|
||||
atxHeading,
|
||||
@@ -20,8 +20,9 @@ import {
|
||||
type ThematicBreakTail,
|
||||
} from '../commonmark/grammar.ts'
|
||||
import { barePipeCells, isDelimiterRow, isPipeAlignment, malformedPipeTable, pipeCells } from '../pipe-table-syntax.ts'
|
||||
import { blockDirectiveForm } from '../block-directive.ts'
|
||||
import { directiveEscape, malformedDirective, readDirectiveLine, spellDirectiveCloser } from '../directive-syntax.ts'
|
||||
import { blockDirectiveForm } from '../directive/block.ts'
|
||||
import { directiveEscape, malformedDirective, readDirectiveLine, spellDirectiveCloser } from '../directive/syntax.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { readLinkDefinitions } from '../commonmark/link-reference-definitions.ts'
|
||||
|
||||
export type Block = { position: SourcePosition } & (
|
||||
@@ -90,10 +91,10 @@ const indentedCodeColumns = 4
|
||||
const largestOpenerIndentation = 3
|
||||
const tabStop = 4
|
||||
|
||||
export function parseBlocks(markdown: string, claims: Claims): ParsedBlocks {
|
||||
export function parseBlocks(markdown: string, flavour: Flavour): ParsedBlocks {
|
||||
const walk: Walk = {
|
||||
blocks: [],
|
||||
claims,
|
||||
claims: flavourClaims[flavour],
|
||||
definitions: new Map(),
|
||||
endedLists: new Set(),
|
||||
leaf: undefined,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { ClaimedPrefix, DirectiveSpan, NestedSpans } from '../directive-syntax.ts'
|
||||
import type { Claims } from '../portable/conventions.ts'
|
||||
import type { ClaimedPrefix, DirectiveSpan, NestedSpans } from '../directive/syntax.ts'
|
||||
import type { Claims, Flavour } from '../flavour.ts'
|
||||
import type { EmphasisPairing } from '../commonmark/emphasis-matching.ts'
|
||||
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
@@ -9,19 +9,20 @@ import { centeredImage, externalMedia } from '../external-image.ts'
|
||||
import { commonMarkLink, linkHref, linkMark } from '../mark-spellings.ts'
|
||||
import { decodeTextEscapes, trimTrailingSpace } from '../commonmark/grammar.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { highlightDelimiter } from '../portable/conventions.ts'
|
||||
import { identicalMarks, mergeAdjacentText, nodeAttrs, nodeMarks } from '../../adf/document.ts'
|
||||
import { inlineNodeModel, takesMark } from '../../adf/inline-nodes.ts'
|
||||
import { joinsWhenRead, textBreakName, textBreakSpelling } from '../adjacent-text.ts'
|
||||
import { matchEmphasis } from '../commonmark/emphasis-matching.ts'
|
||||
import { normalizeLabel, readInlineTarget, readLabel } from '../commonmark/link-syntax.ts'
|
||||
import { noSpans, readInlineDirective } from '../directive-syntax.ts'
|
||||
import { openingLinkTakesDirective } from '../emit/inline-line.ts'
|
||||
import { noSpans, readInlineDirective } from '../directive/syntax.ts'
|
||||
import { openingLinkTakesDirective } from '../emit/spelling-questions.ts'
|
||||
import { readCarriedInline } from '../opaque-carry.ts'
|
||||
import { readDirectiveMark } from './directive-marks.ts'
|
||||
import { readInlineDirectiveNode } from './directive-nodes.ts'
|
||||
import { readDirectiveMark } from '../directive/marks.ts'
|
||||
import { readInlineDirectiveNode } from '../directive/nodes.ts'
|
||||
import { inlineSource, readInlineToken } from '../inline-tokens.ts'
|
||||
import { readTextDirective } from '../text-directive.ts'
|
||||
import { readTextDirective } from '../directive/text.ts'
|
||||
|
||||
// Only a `paragraph` block takes `image`, the `mediaSingle` a lone image builds; every other container takes `nodes`, where the image is its linked alt text.
|
||||
export type InlineContent = { image?: AdfNode; nodes: AdfNode[] }
|
||||
@@ -89,8 +90,8 @@ const spellableLink = 'link takes the directive form only where CommonMark canno
|
||||
const textBreak: TextBreak = { kind: 'textBreak' }
|
||||
|
||||
// The outermost content: only here does a text break see both its neighbours.
|
||||
export function parseInlineContent(source: string, definitions: LinkDefinitions, path: ConvertErrorPath, container: LineContainer, claims: Claims): Result<InlineContent> {
|
||||
const scan = freshScan(source, { definitions, path }, { claims, container, inLink: false, spans: noSpans })
|
||||
export function parseInlineContent(source: string, definitions: LinkDefinitions, path: ConvertErrorPath, container: LineContainer, flavour: Flavour): Result<InlineContent> {
|
||||
const scan = freshScan(source, { definitions, path }, { claims: flavourClaims[flavour], container, inLink: false, spans: noSpans })
|
||||
const scanned = scanInline(scan)
|
||||
if (!scanned.ok) return scanned
|
||||
const nodes = partText(scanned.value.nodes, scan)
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import type { Block, DirectiveBlock } from './blocks.ts'
|
||||
import type { BlockDirectiveNode } from './directive-nodes.ts'
|
||||
import type { Claims } from '../portable/conventions.ts'
|
||||
import type { BlockDirectiveNode } from '../directive/nodes.ts'
|
||||
import type { ConvertFault } from '../../result.ts'
|
||||
import type { Flavour } from '../flavour.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { LinkDefinitions } from './inline-content.ts'
|
||||
import { carryFenceType, readCarriedBlock } from '../opaque-carry.ts'
|
||||
import { commonMarkSpelling, type SpellingMemo } from '../emit/adf-to-markdown.ts'
|
||||
import { documentAttribute, documentName, documentSpelling } from '../block-directive.ts'
|
||||
import { commonMarkSpelling, type SpellingMemo } from '../emit/spelling-questions.ts'
|
||||
import { documentAttribute, documentName, documentSpelling } from '../directive/block.ts'
|
||||
import { emptyKeys, nodeAttrs, nodeContent } from '../../adf/document.ts'
|
||||
import { failure, faulted, positioned, success, type ConvertErrorPath, type ParseError, type Result, type SourcePosition } from '../../result.ts'
|
||||
import { flavourClaims, leadingMarker, readAlertMarker, readTaskMarker } from '../portable/conventions.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { leadingMarker, readAlertMarker, readTaskMarker } from '../portable/conventions.ts'
|
||||
import { mintTaskIds } from '../portable/task-ids.ts'
|
||||
import { parseBlocks } from './blocks.ts'
|
||||
import { parseInlineContent } from './inline-content.ts'
|
||||
import { readBlockDirectiveNode } from './directive-nodes.ts'
|
||||
import { spellStringAttribute, unsupportedNodeShape } from '../directive-syntax.ts'
|
||||
import { readBlockDirectiveNode } from '../directive/nodes.ts'
|
||||
import { spellStringAttribute, unsupportedNodeShape } from '../directive/syntax.ts'
|
||||
import { spellsEmpty } from '../empty-keys.ts'
|
||||
import { splitPlace, splitTaskList } from '../portable/task-list.ts'
|
||||
import { withinEngineLimits } from '../length-bound.ts'
|
||||
@@ -30,40 +31,40 @@ type TaskParagraph = { kind: 'taskParagraph'; position: SourcePosition; text: st
|
||||
type ReadBlock = Block | TaskParagraph
|
||||
|
||||
// `inExpand` is whether an expand holds the blocks, which makes a folded callout a nestedExpand.
|
||||
type Reading = { claims: Claims; definitions: LinkDefinitions; inExpand: boolean; memo: SpellingMemo }
|
||||
type Reading = { definitions: LinkDefinitions; flavour: Flavour; inExpand: boolean; memo: SpellingMemo }
|
||||
|
||||
export function commonMarkToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, flavourClaims.commonmark)
|
||||
return readDocument(markdown, 'commonmark')
|
||||
}
|
||||
|
||||
export function losslessMarkdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, flavourClaims.lossless)
|
||||
return readDocument(markdown, 'lossless')
|
||||
}
|
||||
|
||||
export function portableMarkdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, flavourClaims.portable)
|
||||
return readDocument(markdown, 'portable')
|
||||
}
|
||||
|
||||
function readDocument(markdown: string, claims: Claims): Result<AdfDocument, ParseError> {
|
||||
function readDocument(markdown: string, flavour: Flavour): Result<AdfDocument, ParseError> {
|
||||
return positioned(
|
||||
withinEngineLimits(() => readMarkdown(markdown, claims)),
|
||||
withinEngineLimits(() => readMarkdown(markdown, flavour)),
|
||||
{ line: 1, offset: 0 },
|
||||
)
|
||||
}
|
||||
|
||||
function readMarkdown(markdown: string, claims: Claims): Result<AdfDocument> {
|
||||
function readMarkdown(markdown: string, flavour: Flavour): Result<AdfDocument> {
|
||||
if (typeof markdown !== 'string') return failure('not-a-string', `markdown is a string: found ${typeName(markdown)}`, [])
|
||||
const parsed = parseBlocks(markdown, claims)
|
||||
const parsed = parseBlocks(markdown, flavour)
|
||||
const [only] = parsed.blocks
|
||||
if (parsed.blocks.length === 1 && only?.kind === 'directive' && only.name === documentName) {
|
||||
const fault = documentFault(only)
|
||||
return fault === undefined ? success({ type: 'doc', version: 1 }) : positioned(faulted(fault, []), only.position)
|
||||
}
|
||||
const reading: Reading = { claims, definitions: parsed.definitions, inExpand: false, memo: new Map() }
|
||||
const reading: Reading = { definitions: parsed.definitions, flavour, inExpand: false, memo: new Map() }
|
||||
const content = readBlocks(parsed.blocks, reading, [], 0)
|
||||
if (!content.ok) return content
|
||||
const document: AdfDocument = { content: content.value, type: 'doc', version: 1 }
|
||||
if (claims.taskMarkers) mintTaskIds(document, markdown)
|
||||
if (flavourClaims[flavour].taskMarkers) mintTaskIds(document, markdown)
|
||||
return success(document)
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ function readBlocks(blocks: readonly ReadBlock[], reading: Reading, path: Conver
|
||||
const fault = documentFault(block) ?? unsupportedNodeShape(`delete the ${documentSpelling} line to give the document content: it stands only as the whole document`)
|
||||
return positioned(faulted(fault, nodePath), block.position)
|
||||
}
|
||||
if (block.kind === 'bulletList' && reading.claims.taskMarkers) {
|
||||
if (block.kind === 'bulletList' && flavourClaims[reading.flavour].taskMarkers) {
|
||||
const listed = positioned(bulletBlocks(block.items, reading, nodePath, depth), block.position)
|
||||
if (!listed.ok) return listed
|
||||
for (const node of listed.value) content.push(node)
|
||||
@@ -113,7 +114,7 @@ function documentFault(block: DirectiveBlock): ConvertFault | undefined {
|
||||
function readBlock(block: ReadBlock, reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode> {
|
||||
switch (block.kind) {
|
||||
case 'blockquote':
|
||||
return reading.claims.alerts ? quoteNode(block.blocks, reading, path, depth) : containerNode({ type: 'blockquote' }, block.blocks, reading, path, depth)
|
||||
return flavourClaims[reading.flavour].alerts ? quoteNode(block.blocks, reading, path, depth) : containerNode({ type: 'blockquote' }, block.blocks, reading, path, depth)
|
||||
case 'bulletList':
|
||||
// readBlocks reads a portable task list before it reaches here.
|
||||
return listNode({ type: 'bulletList' }, block.items, reading, path, depth)
|
||||
@@ -165,7 +166,7 @@ function quoteNode(blocks: readonly Block[], reading: Reading, path: ConvertErro
|
||||
const { folded, panelType } = led.marker
|
||||
const { line, rest } = markerLine(led.text)
|
||||
if (!folded) return filledNode({ attrs: { panelType }, type: 'panel' }, readBlocks([...paragraphsOf(led.position, line, rest), ...body], reading, path, depth + 1))
|
||||
const title = parseInlineContent(line, reading.definitions, path, 'paragraph', reading.claims)
|
||||
const title = parseInlineContent(line, reading.definitions, path, 'paragraph', reading.flavour)
|
||||
if (!title.ok) return title
|
||||
const text = titleText(title.value.nodes)
|
||||
const type = reading.inExpand ? 'nestedExpand' : 'expand'
|
||||
@@ -369,7 +370,7 @@ function listNode(node: AdfNode, items: readonly Block[][], reading: Reading, pa
|
||||
}
|
||||
|
||||
function codeBlockNode(language: string, text: string, reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode> {
|
||||
const carriedType = reading.claims.carryFence ? carryFenceType(language) : undefined
|
||||
const carriedType = flavourClaims[reading.flavour].carryFence ? carryFenceType(language) : undefined
|
||||
if (carriedType !== undefined) {
|
||||
const carried = readCarriedBlock(carriedType, text, depth)
|
||||
return carried.fault === undefined ? success(carried.value) : faulted(carried.fault, path)
|
||||
@@ -380,14 +381,14 @@ function codeBlockNode(language: string, text: string, reading: Reading, path: C
|
||||
|
||||
// spec/flavour.md, The CommonMark image: only a `paragraph` block gives a lone image the block it needs.
|
||||
function paragraphNode(text: string, reading: Reading, path: ConvertErrorPath): Result<AdfNode> {
|
||||
const content = parseInlineContent(text, reading.definitions, path, 'paragraph', reading.claims)
|
||||
const content = parseInlineContent(text, reading.definitions, path, 'paragraph', reading.flavour)
|
||||
if (!content.ok) return content
|
||||
const image = content.value.image
|
||||
return success(image === undefined ? withContent({ type: 'paragraph' }, content.value.nodes) : image)
|
||||
}
|
||||
|
||||
function contentNode(node: AdfNode, text: string, reading: Reading, path: ConvertErrorPath, container: LineContainer): Result<AdfNode> {
|
||||
const content = parseInlineContent(text, reading.definitions, path, container, reading.claims)
|
||||
const content = parseInlineContent(text, reading.definitions, path, container, reading.flavour)
|
||||
if (!content.ok) return content
|
||||
return success(withContent(node, content.value.nodes))
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { blockNodeModel, isTaskItem } from '../../adf/block-nodes.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'
|
||||
import { flavourClaims, taskMarker } from './conventions.ts'
|
||||
import { flavourClaims } from '../flavour.ts'
|
||||
import { inlineLeaves, isBlockNodeType, oneLine, portableLink, reduceInline, writableHref } from './inline-reduction.ts'
|
||||
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
@@ -12,6 +12,7 @@ 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 { taskMarker } from './conventions.ts'
|
||||
import { toEditorNormal } from './editor-normal.ts'
|
||||
|
||||
// depth: the level the node reduced stands at, counted as the emitter counts it.
|
||||
|
||||
@@ -1,32 +1,5 @@
|
||||
import { isWordCharacter } from '../commonmark/emphasis-matching.ts'
|
||||
|
||||
type Flavour = 'commonmark' | 'lossless' | 'portable'
|
||||
|
||||
export type WrittenFlavour = Exclude<Flavour, 'commonmark'>
|
||||
|
||||
// What a reader reads past CommonMark, and so what its writer escapes (docs/decisions.md §Each call names its flavour).
|
||||
export type Claims = {
|
||||
alerts: boolean
|
||||
carryFence: boolean
|
||||
directives: boolean
|
||||
highlights: boolean
|
||||
// `gfm`: a row may drop its leading `|`, and an alignment colon is read and dropped; `flavour`: every row opens with `|`, the delimiter row `-` runs only.
|
||||
pipeTables: 'flavour' | 'gfm' | false
|
||||
strikethrough: boolean
|
||||
taskMarkers: boolean
|
||||
}
|
||||
|
||||
type WrittenClaims = Claims & { pipeTables: 'flavour' | 'gfm'; strikethrough: true }
|
||||
|
||||
// Directives exclude alerts and highlights: an expand directive leaves `inExpand` unset and a directive's slot reads `==`.
|
||||
type ReadClaims = Claims & ({ directives: false } | { alerts: false; directives: true; highlights: false })
|
||||
|
||||
export const flavourClaims: Readonly<{ commonmark: ReadClaims } & Record<WrittenFlavour, ReadClaims & WrittenClaims>> = {
|
||||
commonmark: { alerts: false, carryFence: false, directives: false, highlights: false, pipeTables: false, strikethrough: false, taskMarkers: false },
|
||||
lossless: { alerts: false, carryFence: true, directives: true, highlights: false, pipeTables: 'flavour', strikethrough: true, taskMarkers: false },
|
||||
portable: { alerts: true, carryFence: false, directives: false, highlights: true, pipeTables: 'gfm', strikethrough: true, taskMarkers: true },
|
||||
}
|
||||
|
||||
type AlertMarker = { folded: boolean; length: number; panelType: string }
|
||||
|
||||
export const foldedAlertMarker = '[!NOTE]-'
|
||||
|
||||
@@ -25,6 +25,9 @@ export type ParseError = ConvertError & { position: SourcePosition }
|
||||
|
||||
export type Result<T, E extends ConvertError = ConvertError> = { error: E; ok: false } | { ok: true; value: T }
|
||||
|
||||
// A reader with no path to name; the walk attaches the path where it knows it.
|
||||
export type Read<T> = { fault: ConvertFault; value?: undefined } | { fault?: undefined; value: T }
|
||||
|
||||
export function failure<T>(code: ConvertErrorCode, message: string, path: ConvertErrorPath): Result<T> {
|
||||
return { error: { code, message, path }, ok: false }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user