3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 22:55:44 +01:00

Read Jira wiki markup into ADF with jiraWikiToAdf

This commit is contained in:
2026-11-17 04:08:49 +01:00
committed by Lillem4n
parent 6720beab71
commit 73e597b512
19 changed files with 1222 additions and 40 deletions
+32
View File
@@ -0,0 +1,32 @@
import type { AdfNode } from '../../adf/document.ts'
import type { ConvertFault, Read } from '../../result.ts'
import { carriedNesting, carriedNode, carriedValue } from '../../adf/carried-json.ts'
import { largestNesting } from '../../nesting.ts'
// `inline`: the macro says `display=inline`.
export type AdfMacro = { end: number; inline: boolean; node: AdfNode }
const closer = '{adf}'
const holder = 'an {adf} macro'
const opener = /\{adf(?::([^}\n]*))?\}/y
// spec/wiki.md, The carry: `undefined` where no `{adf}` macro opens at `at`. The macro closes before `end`.
export function readAdfMacro(wiki: string, at: number, end: number): Read<AdfMacro> | undefined {
opener.lastIndex = at
const opened = opener.exec(wiki)
if (opened === null) return undefined
const start = opener.lastIndex
const close = wiki.indexOf(closer, start)
if (close === -1 || close + closer.length > end) return { fault: refusal('closes with {adf}: this one is left open') }
const value = carriedValue(wiki.slice(start, close), holder, refusal("holds one ADF node's JSON: this is no JSON"))
if (value.fault !== undefined) return value
const bounded = carriedNesting(value.value, largestNesting)
if (bounded.fault !== undefined) return bounded
const node = carriedNode(bounded.value, holder)
if (node.fault !== undefined) return node
return { value: { end: close + closer.length, inline: opened[1] === 'display=inline', node: node.value } }
}
function refusal(message: string): ConvertFault {
return { code: 'unsupported-node-shape', message: `${holder} ${message}` }
}