mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 14:45:44 +01:00
Give each markdown input its own reader: commonMarkToAdf, losslessMarkdownToAdf and portableMarkdownToAdf, each claiming only its flavour
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
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 { ConvertFault } from '../../result.ts'
|
||||
import type { Flavour } from '../plain/conventions.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { LinkDefinitions } from './inline-content.ts'
|
||||
import { carryFenceType, readCarriedBlock } from '../opaque-carry.ts'
|
||||
@@ -10,11 +10,10 @@ import { commonMarkSpelling, type SpellingMemo } from '../emit/adf-to-markdown.t
|
||||
import { documentAttribute, documentName, documentSpelling, listBreakName, listBreakSpelling } from '../block-directive.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 { inlineLeaves } from '../plain/inline-reduction.ts'
|
||||
import { flavourClaims, leadingMarker, readAlertMarker, readTaskMarker } from '../portable/conventions.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { leadingMarker, readAlertMarker, readTaskMarker } from '../plain/conventions.ts'
|
||||
import { mintTaskIds } from '../plain/task-ids.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'
|
||||
@@ -24,32 +23,36 @@ import { spellsEmpty } from '../empty-keys.ts'
|
||||
type Paragraph = Extract<Block, { kind: 'paragraph' }>
|
||||
|
||||
// `inExpand` is whether an expand holds the blocks, which makes a folded callout a nestedExpand.
|
||||
type Reading = { carried: Set<AdfNode>; definitions: LinkDefinitions; flavour: Flavour; inExpand: boolean; memo: SpellingMemo }
|
||||
type Reading = { claims: Claims; definitions: LinkDefinitions; inExpand: boolean; memo: SpellingMemo }
|
||||
|
||||
const imageAfterMarker = 'an image fits only as a paragraph of its own: this one continues the paragraph a marker opens, which a blank line before it ends'
|
||||
const imageOnMarkerLine = 'an image fits only as a paragraph of its own: this one shares a line with a marker'
|
||||
|
||||
export function markdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, 'lossless')
|
||||
export function commonMarkToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, flavourClaims.commonmark)
|
||||
}
|
||||
|
||||
export function plainMarkdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, 'plain')
|
||||
export function losslessMarkdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, flavourClaims.lossless)
|
||||
}
|
||||
|
||||
function readDocument(markdown: string, flavour: Flavour): Result<AdfDocument, ParseError> {
|
||||
export function portableMarkdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||
return readDocument(markdown, flavourClaims.portable)
|
||||
}
|
||||
|
||||
function readDocument(markdown: string, claims: Claims): Result<AdfDocument, ParseError> {
|
||||
if (typeof markdown !== 'string') return positioned(failure('not-a-string', `markdown is a string: found ${typeName(markdown)}`, []), { line: 1, offset: 0 })
|
||||
const parsed = parseBlocks(markdown)
|
||||
const parsed = parseBlocks(markdown, claims)
|
||||
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 = { carried: new Set(), definitions: parsed.definitions, flavour, inExpand: false, memo: new Map() }
|
||||
const reading: Reading = { claims, definitions: parsed.definitions, inExpand: false, memo: new Map() }
|
||||
const content = positioned(readBlocks(parsed.blocks, reading, [], 0), { line: 1, offset: 0 })
|
||||
if (!content.ok) return content
|
||||
const document: AdfDocument = { content: content.value, type: 'doc', version: 1 }
|
||||
if (flavour === 'plain') mintTaskIds(document, markdown, reading.carried)
|
||||
if (claims.taskMarkers) mintTaskIds(document, markdown)
|
||||
return success(document)
|
||||
}
|
||||
|
||||
@@ -107,9 +110,9 @@ function partsFault(): ConvertFault {
|
||||
function readBlock(block: Block, reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode> {
|
||||
switch (block.kind) {
|
||||
case 'blockquote':
|
||||
return reading.flavour === 'plain' ? quoteNode(block.blocks, reading, path, depth) : containerNode({ type: 'blockquote' }, block.blocks, reading, path, depth)
|
||||
return reading.claims.alerts ? quoteNode(block.blocks, reading, path, depth) : containerNode({ type: 'blockquote' }, block.blocks, reading, path, depth)
|
||||
case 'bulletList':
|
||||
return reading.flavour === 'plain' ? bulletNode(block.items, reading, path, depth) : listNode({ type: 'bulletList' }, block.items, reading, path, depth)
|
||||
return reading.claims.taskMarkers ? bulletNode(block.items, reading, path, depth) : listNode({ type: 'bulletList' }, block.items, reading, path, depth)
|
||||
case 'code':
|
||||
return codeBlockNode(block.language, block.text, reading, path, depth)
|
||||
case 'directive':
|
||||
@@ -155,16 +158,10 @@ 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' }, readMarked(paragraphsOf(led.position, line, rest), line !== '', body, reading, path, depth))
|
||||
const title = parseInlineContent(line, reading.definitions, path, 'paragraph', 'lossless')
|
||||
const title = parseInlineContent(line, reading.definitions, path, 'paragraph', reading.claims)
|
||||
if (!title.ok) return title
|
||||
if (title.value.image !== undefined) return failure('unmappable-image', imageOnMarkerLine, path)
|
||||
const leaves: AdfNode[] = []
|
||||
for (const [index, node] of title.value.nodes.entries()) {
|
||||
const held = node.text === undefined ? inlineLeaves([node], 'paragraph', [...path, 'content', index], depth) : success([node])
|
||||
if (!held.ok) return held
|
||||
for (const leaf of held.value) leaves.push(leaf)
|
||||
}
|
||||
const text = titleText(leaves)
|
||||
const text = titleText(title.value.nodes)
|
||||
const type = reading.inExpand ? 'nestedExpand' : 'expand'
|
||||
return filledNode(text === '' ? { type } : { attrs: { title: text }, type }, readMarked(paragraphsOf(led.position, rest), false, body, { ...reading, inExpand: true }, path, depth))
|
||||
}
|
||||
@@ -231,8 +228,7 @@ function bulletNode(items: readonly Block[][], reading: Reading, path: ConvertEr
|
||||
function directiveNode(block: DirectiveBlock, reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode> {
|
||||
const read = readBlockDirectiveNode(block.name, block.argument, block.attributes, path)
|
||||
if (!read.ok) return read
|
||||
const inExpand = reading.inExpand || read.value.node.type === 'expand' || read.value.node.type === 'nestedExpand'
|
||||
const built = directiveBody(read.value, block.blocks, { ...reading, inExpand }, path, depth)
|
||||
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
|
||||
@@ -263,7 +259,7 @@ function codeDirectiveNode(node: AdfNode, blocks: readonly Block[], path: Conver
|
||||
if (fences.length > 1 && fences.some((fence) => fence.text === '')) return failure('unsupported-node-shape', `delete the empty fence: a fence beside another holds code, and this one holds none`, path)
|
||||
const attribute = nodeAttrs(node)['language']
|
||||
const fromFence = first.language !== ''
|
||||
const slot = languageSlot(fromFence ? first.language : attribute)
|
||||
const slot = languageSlot(fromFence ? first.language : attribute, flavourClaims.lossless)
|
||||
if (!fromFence && slot.kind === 'fence') {
|
||||
return failure('unsupported-node-shape', `move language=${spellStringAttribute(slot.info)} to the fences' info strings: they can spell this language`, path)
|
||||
}
|
||||
@@ -321,12 +317,10 @@ 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 = carryFenceType(language)
|
||||
const carriedType = reading.claims.carryFence ? carryFenceType(language) : undefined
|
||||
if (carriedType !== undefined) {
|
||||
const carried = readCarriedBlock(carriedType, text, depth)
|
||||
if (carried.fault !== undefined) return faulted(carried.fault, path)
|
||||
reading.carried.add(carried.value)
|
||||
return success(carried.value)
|
||||
return carried.fault === undefined ? success(carried.value) : faulted(carried.fault, path)
|
||||
}
|
||||
const node: AdfNode = language === '' ? { type: 'codeBlock' } : { attrs: { language }, type: 'codeBlock' }
|
||||
return success(text === '' ? node : { ...node, content: [{ text, type: 'text' }] })
|
||||
@@ -334,14 +328,14 @@ function codeBlockNode(language: string, text: string, reading: Reading, path: C
|
||||
|
||||
// spec/flavour.md, The CommonMark image: only a plain paragraph gives an image the block it needs.
|
||||
function paragraphNode(text: string, reading: Reading, path: ConvertErrorPath): Result<AdfNode> {
|
||||
const content = parseInlineContent(text, reading.definitions, path, 'paragraph', reading.flavour)
|
||||
const content = parseInlineContent(text, reading.definitions, path, 'paragraph', reading.claims)
|
||||
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.flavour)
|
||||
const content = parseInlineContent(text, reading.definitions, path, container, reading.claims)
|
||||
if (!content.ok) return content
|
||||
if (content.value.image !== undefined) return failure('unmappable-image', `no ADF node carries an image inside a ${node.type}`, path)
|
||||
return success(withContent(node, content.value.nodes))
|
||||
|
||||
Reference in New Issue
Block a user