mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Gather the plain flavour's code in markdown/plain/
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { DirectiveSpan, NestedSpans } from '../directive-syntax.ts'
|
||||
import type { EmphasisPairing } from '../commonmark/emphasis-matching.ts'
|
||||
import type { Flavour } from '../plain-conventions.ts'
|
||||
import type { Flavour } from '../plain/conventions.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
|
||||
import { backslashEscape, decodeTextEscapes, inlineHtmlConstruct, readBracketedAutolink, readEmailAutolink, trimTrailingSpace } from '../commonmark/grammar.ts'
|
||||
@@ -9,7 +9,7 @@ import { backtickRun, closingBacktickRun } from '../commonmark/backtick-runs.ts'
|
||||
import { commonMarkLink, linkHref } from '../mark-spellings.ts'
|
||||
import { delimiterFlags, matchEmphasis, runLength } from '../commonmark/emphasis-matching.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { highlightDelimiter, highlightFlanking } from '../plain-conventions.ts'
|
||||
import { highlightDelimiter, highlightFlanking } from '../plain/conventions.ts'
|
||||
import { identicalMarks, mergeAdjacentText, nodeAttrs, nodeMarks } from '../../adf/document.ts'
|
||||
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import { noSpans, readInlineDirective } from '../directive-syntax.ts'
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import type { Block, DirectiveBlock } from './blocks.ts'
|
||||
import type { BlockDirectiveNode } from './directive-nodes.ts'
|
||||
import type { ConvertFault } from '../../result.ts'
|
||||
import type { Flavour } from '../plain-conventions.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,11 @@ 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 '../emit/plain-inline.ts'
|
||||
import { inlineLeaves } from '../plain/inline-reduction.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { leadingMarker, readAlertMarker, readTaskMarker } from '../plain-conventions.ts'
|
||||
import { mintTaskIds } from './task-ids.ts'
|
||||
import { leadingMarker, readAlertMarker, readTaskMarker } from '../plain/conventions.ts'
|
||||
import { mintTaskIds } from '../plain/task-ids.ts'
|
||||
import { parseBlocks } from './blocks.ts'
|
||||
import { parseInlineContent } from './inline-content.ts'
|
||||
import { readBlockDirectiveNode } from './directive-nodes.ts'
|
||||
|
||||
@@ -3,7 +3,7 @@ import test from 'node:test'
|
||||
|
||||
import type { AdfAttributes, AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import { adfToMarkdown } from '../emit/adf-to-markdown.ts'
|
||||
import { adfToPlainMarkdown } from '../emit/plain-reduction.ts'
|
||||
import { adfToPlainMarkdown } from '../plain/adf-to-plain-markdown.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { markdownToAdf, plainMarkdownToAdf } from './markdown-to-adf.ts'
|
||||
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import { mintTaskIds } from './task-ids.ts'
|
||||
|
||||
const uuidV4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
|
||||
|
||||
function taskIds(document: AdfDocument): unknown[] {
|
||||
const ids: unknown[] = []
|
||||
const pending: AdfNode[] = [...(document.content ?? [])].reverse()
|
||||
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
||||
if (['blockTaskItem', 'taskItem', 'taskList'].includes(node.type)) ids.push(node.attrs?.['localId'])
|
||||
for (const child of [...(node.content ?? [])].reverse()) pending.push(child)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
function tasks(...content: AdfNode[]): AdfDocument {
|
||||
return { content, type: 'doc', version: 1 }
|
||||
}
|
||||
|
||||
test('mints each task node lacking a localId a UUID v4 in document order, unique and the same every run', () => {
|
||||
const unminted = (): AdfDocument => tasks({ content: [{ attrs: { state: 'TODO' }, type: 'taskItem' }, { content: [{ attrs: { state: 'DONE' }, type: 'blockTaskItem' }], type: 'taskList' }], type: 'taskList' })
|
||||
const minted = unminted()
|
||||
mintTaskIds(minted, '- [ ] a\n', new Set())
|
||||
const ids = taskIds(minted)
|
||||
assert.equal(ids.length, 4)
|
||||
for (const id of ids) assert.match(String(id), uuidV4)
|
||||
assert.equal(new Set(ids).size, 4)
|
||||
const again = unminted()
|
||||
mintTaskIds(again, '- [ ] a\n', new Set())
|
||||
assert.deepEqual(taskIds(again), ids)
|
||||
const other = unminted()
|
||||
mintTaskIds(other, '- [ ] b\n', new Set())
|
||||
assert.equal(taskIds(other).some((id) => ids.includes(id)), false)
|
||||
})
|
||||
|
||||
test('keeps a localId the document spells and skips it when minting, a carried one too', () => {
|
||||
const first = tasks({ type: 'taskList' })
|
||||
mintTaskIds(first, 'x', new Set())
|
||||
const [taken] = taskIds(first)
|
||||
const carried: AdfNode = { content: [{ attrs: { localId: String(taken) }, type: 'taskList' }], type: 'futureBlock' }
|
||||
const spelled = tasks({ type: 'taskList' }, carried)
|
||||
mintTaskIds(spelled, 'x', new Set([carried]))
|
||||
const [minted, kept] = taskIds(spelled)
|
||||
assert.equal(kept, taken)
|
||||
assert.notEqual(minted, taken)
|
||||
assert.match(String(minted), uuidV4)
|
||||
})
|
||||
|
||||
test('leaves a node the carry restores as carried, minting neither it nor what it holds', () => {
|
||||
const list = (): AdfNode => ({ content: [{ attrs: { state: 'TODO' }, type: 'taskItem' }], type: 'taskList' })
|
||||
const carriedList = list()
|
||||
const carriedPanel: AdfNode = { attrs: { panelType: 'info' }, content: [list()], type: 'panel' }
|
||||
const future: AdfNode = { content: [list()], type: 'futureBlock' }
|
||||
const document = tasks(carriedList, carriedPanel, future)
|
||||
mintTaskIds(document, 'x', new Set([carriedList, carriedPanel]))
|
||||
assert.deepEqual(document, tasks(list(), { attrs: { panelType: 'info' }, content: [list()], type: 'panel' }, { content: [list()], type: 'futureBlock' }))
|
||||
})
|
||||
@@ -1,70 +0,0 @@
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import { blockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import { nodeAttrs, nodeContent } from '../../adf/document.ts'
|
||||
|
||||
const taskTypes = new Set(['blockTaskItem', 'taskItem', 'taskList'])
|
||||
|
||||
export function mintTaskIds(document: AdfDocument, markdown: string, carried: ReadonlySet<AdfNode>): void {
|
||||
const taken = new Set<string>()
|
||||
for (const node of preorder(document, () => true)) {
|
||||
const localId = nodeAttrs(node)['localId']
|
||||
if (typeof localId === 'string') taken.add(localId)
|
||||
}
|
||||
const seed = hash128(markdown).join(' ')
|
||||
let count = 0
|
||||
for (const node of preorder(document, (held) => !carried.has(held) && blockNodeModel(held.type)?.contentModel === 'block')) {
|
||||
if (!taskTypes.has(node.type) || carried.has(node) || typeof nodeAttrs(node)['localId'] === 'string') continue
|
||||
let localId = ''
|
||||
do {
|
||||
count += 1
|
||||
localId = uuidV4(hash128(`${seed} ${count}`))
|
||||
} while (taken.has(localId))
|
||||
taken.add(localId)
|
||||
node.attrs = { ...node.attrs, localId }
|
||||
}
|
||||
}
|
||||
|
||||
function* preorder(document: AdfDocument, entered: (node: AdfNode) => boolean): Generator<AdfNode> {
|
||||
const pending: AdfNode[] = []
|
||||
const pushReversed = (nodes: readonly AdfNode[]): void => {
|
||||
for (let index = nodes.length - 1; index >= 0; index -= 1) {
|
||||
const node = nodes[index]
|
||||
if (node !== undefined) pending.push(node)
|
||||
}
|
||||
}
|
||||
pushReversed(document.content ?? [])
|
||||
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
||||
yield node
|
||||
if (entered(node)) pushReversed(nodeContent(node))
|
||||
}
|
||||
}
|
||||
|
||||
// cyrb128, public domain.
|
||||
function hash128(text: string): number[] {
|
||||
let h1 = 1779033703
|
||||
let h2 = 3144134277
|
||||
let h3 = 1013904242
|
||||
let h4 = 2773480762
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
const unit = text.charCodeAt(index)
|
||||
h1 = h2 ^ Math.imul(h1 ^ unit, 597399067)
|
||||
h2 = h3 ^ Math.imul(h2 ^ unit, 2869860233)
|
||||
h3 = h4 ^ Math.imul(h3 ^ unit, 951274213)
|
||||
h4 = h1 ^ Math.imul(h4 ^ unit, 2716044179)
|
||||
}
|
||||
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067)
|
||||
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233)
|
||||
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213)
|
||||
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179)
|
||||
h1 ^= h2 ^ h3 ^ h4
|
||||
h2 ^= h1
|
||||
h3 ^= h1
|
||||
h4 ^= h1
|
||||
return [h1 >>> 0, h2 >>> 0, h3 >>> 0, h4 >>> 0]
|
||||
}
|
||||
|
||||
function uuidV4(lanes: readonly number[]): string {
|
||||
const hex = lanes.map((lane) => lane.toString(16).padStart(8, '0')).join('')
|
||||
const variant = ((Number.parseInt(hex.charAt(16), 16) & 3) | 8).toString(16)
|
||||
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(13, 16)}-${variant}${hex.slice(17, 20)}-${hex.slice(20, 32)}`
|
||||
}
|
||||
Reference in New Issue
Block a user