35a - review: a title keeps its ==, an image beside a marker is refused as before
This commit is contained in:
@@ -8,7 +8,7 @@ import { backslashEscape, decodeTextEscapes, inlineHtmlConstruct, readBracketedA
|
|||||||
import { backtickRun, closingBacktickRun } from '../commonmark/backtick-runs.ts'
|
import { backtickRun, closingBacktickRun } from '../commonmark/backtick-runs.ts'
|
||||||
import { commonMarkLink, linkHref } from '../mark-spellings.ts'
|
import { commonMarkLink, linkHref } from '../mark-spellings.ts'
|
||||||
import { delimiterFlags, isWordCharacter, matchEmphasis, runLength } from '../commonmark/emphasis-matching.ts'
|
import { delimiterFlags, isWordCharacter, matchEmphasis, runLength } from '../commonmark/emphasis-matching.ts'
|
||||||
import { editorHighlight, highlightDelimiter } from '../plain-conventions.ts'
|
import { highlightDelimiter } from '../plain-conventions.ts'
|
||||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||||
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||||
import { mergeAdjacentText, sameMarks } from '../../adf/editor-normal.ts'
|
import { mergeAdjacentText, sameMarks } from '../../adf/editor-normal.ts'
|
||||||
@@ -59,6 +59,7 @@ type Scan = {
|
|||||||
type SlotContent = { carry: boolean; nodes: AdfNode[] }
|
type SlotContent = { carry: boolean; nodes: AdfNode[] }
|
||||||
|
|
||||||
const carriedInMark = 'no mark spelling wraps an opaque carry: the carried node restores exactly, marks included'
|
const carriedInMark = 'no mark spelling wraps an opaque carry: the carried node restores exactly, marks included'
|
||||||
|
const editorHighlight: AdfMark = { attrs: { color: '#f8e6a0' }, type: 'backgroundColor' }
|
||||||
const hreflessLink = 'the link mark spells its href: this one spells none'
|
const hreflessLink = 'the link mark spells its href: this one spells none'
|
||||||
const imageAlone = 'an image fits only as a paragraph of its own: this one sits inside other content'
|
const imageAlone = 'an image fits only as a paragraph of its own: this one sits inside other content'
|
||||||
const linkInLink = 'no link wraps a link: the [content] this one marks already holds one'
|
const linkInLink = 'no link wraps a link: the [content] this one marks already holds one'
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type Paragraph = Extract<Block, { kind: 'paragraph' }>
|
|||||||
type Reading = { definitions: LinkDefinitions; flavour: Flavour; inExpand: boolean; memo: SpellingMemo }
|
type Reading = { definitions: LinkDefinitions; flavour: Flavour; inExpand: boolean; memo: SpellingMemo }
|
||||||
|
|
||||||
const documentStart: SourcePosition = { line: 1, offset: 0 }
|
const documentStart: SourcePosition = { line: 1, offset: 0 }
|
||||||
|
const imageBesideMarker = 'an image fits only as a paragraph of its own: this one shares its paragraph with a marker'
|
||||||
|
|
||||||
export function markdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
export function markdownToAdf(markdown: string): Result<AdfDocument, ParseError> {
|
||||||
return readDocument(markdown, 'lossless')
|
return readDocument(markdown, 'lossless')
|
||||||
@@ -98,7 +99,6 @@ function readBlock(block: Block, reading: Reading, path: ConvertErrorPath, depth
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The marker opens the paragraph's source, and whitespace or the line's end follows it.
|
|
||||||
function markerLed<T extends { length: number }>(block: Block | undefined, read: (text: string) => T | undefined): { marker: T; position: SourcePosition; text: string } | undefined {
|
function markerLed<T extends { length: number }>(block: Block | undefined, read: (text: string) => T | undefined): { marker: T; position: SourcePosition; text: string } | undefined {
|
||||||
if (block?.kind !== 'paragraph') return undefined
|
if (block?.kind !== 'paragraph') return undefined
|
||||||
const marker = read(block.text)
|
const marker = read(block.text)
|
||||||
@@ -107,7 +107,6 @@ function markerLed<T extends { length: number }>(block: Block | undefined, read:
|
|||||||
return text === '' || /^(?:[ \t\n]|\\\n)/.test(text) ? { marker, position: block.position, text } : undefined
|
return text === '' || /^(?:[ \t\n]|\\\n)/.test(text) ? { marker, position: block.position, text } : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// The marker's line, less a hard break ending it, and the lines after.
|
|
||||||
function markerLine(text: string): { line: string; rest: string } {
|
function markerLine(text: string): { line: string; rest: string } {
|
||||||
const lineEnd = text.indexOf('\n')
|
const lineEnd = text.indexOf('\n')
|
||||||
const line = lineEnd === -1 ? text : text.slice(0, lineEnd)
|
const line = lineEnd === -1 ? text : text.slice(0, lineEnd)
|
||||||
@@ -125,18 +124,27 @@ function quoteNode(blocks: readonly Block[], reading: Reading, path: ConvertErro
|
|||||||
if (led === undefined) return containerNode({ type: 'blockquote' }, blocks, reading, path, depth)
|
if (led === undefined) return containerNode({ type: 'blockquote' }, blocks, reading, path, depth)
|
||||||
const { folded, panelType } = led.marker
|
const { folded, panelType } = led.marker
|
||||||
const { line, rest } = markerLine(led.text)
|
const { line, rest } = markerLine(led.text)
|
||||||
if (!folded) return filledNode({ attrs: { panelType }, type: 'panel' }, [...paragraphsOf(led.position, line, rest), ...body], reading, path, depth)
|
if (!folded) return filledNode({ attrs: { panelType }, type: 'panel' }, readMarked(paragraphsOf(led.position, line, rest), body, reading, path, depth))
|
||||||
const title = contentNode({ type: 'paragraph' }, line, reading, path, 'paragraph')
|
const title = parseInlineContent(line, reading.definitions, path, 'paragraph', 'lossless')
|
||||||
if (!title.ok) return title
|
if (!title.ok) return title
|
||||||
const text = nodeContent(title.value).map((node) => node.text ?? '').join('')
|
if (title.value.image !== undefined) return failure('unmappable-image', imageBesideMarker, path)
|
||||||
|
const text = title.value.nodes.map((node) => node.text ?? '').join('')
|
||||||
const type = reading.inExpand ? 'nestedExpand' : 'expand'
|
const type = reading.inExpand ? 'nestedExpand' : 'expand'
|
||||||
return filledNode(text === '' ? { type } : { attrs: { title: text }, type }, [...paragraphsOf(led.position, rest), ...body], { ...reading, inExpand: true }, path, depth)
|
return filledNode(text === '' ? { type } : { attrs: { title: text }, type }, readMarked(paragraphsOf(led.position, rest), body, { ...reading, inExpand: true }, path, depth))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Atlassian's schema requires a panel and an expand to hold a block.
|
// Atlassian's schema requires a panel and an expand to hold a block.
|
||||||
function filledNode(node: AdfNode, blocks: readonly Block[], reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode> {
|
function filledNode(node: AdfNode, content: Result<AdfNode[]>): Result<AdfNode> {
|
||||||
const built = containerNode(node, blocks, reading, path, depth)
|
if (!content.ok) return content
|
||||||
return built.ok && built.value.content === undefined ? success({ ...built.value, content: [{ type: 'paragraph' }] }) : built
|
return success({ ...node, content: content.value.length === 0 ? [{ type: 'paragraph' }] : content.value })
|
||||||
|
}
|
||||||
|
|
||||||
|
// A paragraph split off a marker still refuses the image it held beside it.
|
||||||
|
function readMarked(marked: readonly Paragraph[], others: readonly Block[], reading: Reading, path: ConvertErrorPath, depth: number): Result<AdfNode[]> {
|
||||||
|
const read = readBlocks([...marked, ...others], reading, path, depth + 1)
|
||||||
|
if (!read.ok) return read
|
||||||
|
const image = read.value.slice(0, marked.length).findIndex((node) => node.type === 'mediaSingle')
|
||||||
|
return image === -1 ? read : failure('unmappable-image', imageBesideMarker, [...path, 'content', image])
|
||||||
}
|
}
|
||||||
|
|
||||||
// A task list trailing an item's blocks stands beside it, as ADF nests one.
|
// A task list trailing an item's blocks stands beside it, as ADF nests one.
|
||||||
@@ -149,7 +157,7 @@ function bulletNode(items: readonly Block[][], reading: Reading, path: ConvertEr
|
|||||||
}
|
}
|
||||||
const tasks: AdfNode[] = []
|
const tasks: AdfNode[] = []
|
||||||
for (const [index, { marker, others, position, text }] of led.entries()) {
|
for (const [index, { marker, others, position, text }] of led.entries()) {
|
||||||
const read = readBlocks([...paragraphsOf(position, text.replace(/^(?:[ \t\n]|\\\n)+/, '')), ...others], reading, [...path, 'content', index], depth + 1)
|
const read = readMarked(paragraphsOf(position, text.replace(/^(?:[ \t\n]|\\\n)+/, '')), others, reading, [...path, 'content', index], depth)
|
||||||
if (!read.ok) return read
|
if (!read.ok) return read
|
||||||
let beside = read.value.length
|
let beside = read.value.length
|
||||||
while (read.value[beside - 1]?.type === 'taskList') beside -= 1
|
while (read.value[beside - 1]?.type === 'taskList') beside -= 1
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
import type { AdfMark } from '../adf/document.ts'
|
|
||||||
|
|
||||||
export type Flavour = 'lossless' | 'plain'
|
export type Flavour = 'lossless' | 'plain'
|
||||||
|
|
||||||
type AlertMarker = { folded: boolean; length: number; panelType: string }
|
type AlertMarker = { folded: boolean; length: number; panelType: string }
|
||||||
|
|
||||||
export const editorHighlight: AdfMark = { attrs: { color: '#f8e6a0' }, type: 'backgroundColor' }
|
|
||||||
|
|
||||||
export const foldedAlertMarker = '[!NOTE]-'
|
export const foldedAlertMarker = '[!NOTE]-'
|
||||||
export const highlightDelimiter = '=='
|
export const highlightDelimiter = '=='
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user