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:
@@ -364,9 +364,11 @@ const riddenBlocks: [string, AdfNode][] = [
|
||||
['a blockquote holding no content', { type: 'blockquote' }],
|
||||
['a code block holding another attribute', { attrs: { language: 'java', title: 'a' }, content: [text('x')], type: 'codeBlock' }],
|
||||
['a code block of a marked text', { attrs: { language: 'java' }, content: [text('x', { type: 'strong' })], type: 'codeBlock' }],
|
||||
['a code block of an empty text', { attrs: { language: 'java' }, content: [text('')], type: 'codeBlock' }],
|
||||
['a code block of no content', { attrs: { language: 'java' }, type: 'codeBlock' }],
|
||||
['a code block of two texts', { attrs: { language: 'java' }, content: [text('x'), text('y', { type: 'strong' })], type: 'codeBlock' }],
|
||||
['a code block whose language is no string', { attrs: { language: 5 }, content: [text('x')], type: 'codeBlock' }],
|
||||
['a heading holding an empty content', { attrs: { level: 1 }, content: [], type: 'heading' }],
|
||||
['a list holding a list item and a paragraph', { content: [item(paragraph(text('a'))), paragraph(text('b'))], type: 'bulletList' }],
|
||||
['a list holding a paragraph', { content: [paragraph(text('a'))], type: 'bulletList' }],
|
||||
['a list item holding a localId', list('bulletList', { attrs: { localId: 'i1' }, content: [paragraph(text('a'))], type: 'listItem' })],
|
||||
@@ -381,7 +383,9 @@ const riddenBlocks: [string, AdfNode][] = [
|
||||
['a table row holding a cell and a paragraph', { attrs: plainTable, content: [{ content: [cell('tableCell', text('a')), paragraph(text('b'))], type: 'tableRow' }], type: 'table' }],
|
||||
['a table row holding a localId', { attrs: plainTable, content: [{ attrs: { localId: 'r1' }, content: [cell('tableCell', text('a'))], type: 'tableRow' }], type: 'table' }],
|
||||
['a table row holding a paragraph', { attrs: plainTable, content: [{ content: [paragraph(text('a'))], type: 'tableRow' }], type: 'table' }],
|
||||
['a table whose cell holds a paragraph of an empty content', table(plainTable, [{ content: [{ content: [], type: 'paragraph' }], type: 'tableCell' }])],
|
||||
['an image of a stored file with a URL', image({ type: 'file', url: 'https://example.com/a.png' })],
|
||||
['an image whose URL holds no ://', image({ type: 'external', url: 'a.png' })],
|
||||
['an image whose URL is no string', image({ type: 'external', url: 5 })],
|
||||
['an image whose alt ends in whitespace', image({ ...external, alt: 'A cat ' })],
|
||||
['an image whose alt is no string', image({ ...external, alt: 5 })],
|
||||
@@ -502,6 +506,10 @@ for (const [name, node] of riddenInlines) {
|
||||
})
|
||||
}
|
||||
|
||||
test('carries a text holding nothing', () => {
|
||||
assert.equal(wiki(paragraph(text('a'), text('', { type: 'strong' }))), `a${inline('{"marks":[{"type":"strong"}],"text":"","type":"text"}')}`)
|
||||
})
|
||||
|
||||
test('refuses a carry past 500 levels in a list by the path of the first', () => {
|
||||
let value: AdfAttributes[string] = 1
|
||||
for (let level = 0; level < largestNesting - 1; level += 1) value = [value]
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import { escapedText } from './text-escaping.ts'
|
||||
import { holdsOnlyAttributes, identicalMark, identicalMarks, isSpellableText, nodeAttrs, nodeMarks } from '../../adf/document.ts'
|
||||
import { inlineCarry } from '../carry.ts'
|
||||
import { spellableColour, spellableTitle, spellableUrl } from '../spellings.ts'
|
||||
import { success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
|
||||
// `carried`: whether any node of the line rides the carry.
|
||||
@@ -18,8 +19,6 @@ type SpelledMark = { delimiters: Delimiters; mark: AdfMark }
|
||||
type Token = { checked: boolean; kind: 'atom' | 'close' | 'open'; text: string }
|
||||
|
||||
const characterDelimiters: ReadonlyMap<string, string> = new Map([['em', '_'], ['strike', '-'], ['strong', '*'], ['underline', '+']])
|
||||
const spellableUrl = /^[^\s[\]{}|\\]+$/
|
||||
const spellableColour = /^#[0-9a-f]{6}$/i
|
||||
|
||||
// spec/wiki.md, Inline and Marks.
|
||||
export function inlineLine(nodes: readonly AdfNode[], path: ConvertErrorPath, inCell: boolean): Result<Line> {
|
||||
@@ -53,7 +52,7 @@ function atom(node: AdfNode, nodes: readonly AdfNode[], index: number, inCell: b
|
||||
const spelled = spelledMarks(marks)
|
||||
const previous = nodes[index - 1]
|
||||
const joinsPrevious = previous?.type === 'text' && identicalMarks(nodeMarks(previous), marks)
|
||||
if (!isSpellableText(node) || joinsPrevious || spelled === undefined) return { marks, spelled: undefined }
|
||||
if (!isSpellableText(node) || text === '' || joinsPrevious || spelled === undefined) return { marks, spelled: undefined }
|
||||
const href = hrefSpelling(node, nodes, index)
|
||||
if (href !== undefined) return { marks: marks.slice(0, -1), spelled: { marks: spelled.slice(0, -1), text: href } }
|
||||
const escaped = escapedText(text, index === 0, index === nodes.length - 1, inCell)
|
||||
@@ -108,7 +107,7 @@ function linkDelimiters(attrs: Readonly<Record<string, unknown>>): Delimiters |
|
||||
const { href, title, ...others } = attrs
|
||||
if (Object.keys(others).length > 0 || typeof href !== 'string' || !spellableUrl.test(href)) return undefined
|
||||
if (title === undefined) return { checked: false, close: `|${href}]`, open: '[' }
|
||||
return typeof title === 'string' && /^[^\s[\]{}|\\](?:[^[\]{}|\\\r\n]*[^\s[\]{}|\\])?$/.test(title) ? { checked: false, close: `|${href}|${title}]`, open: '[' } : undefined
|
||||
return typeof title === 'string' && spellableTitle.test(title) ? { checked: false, close: `|${href}|${title}]`, open: '[' } : undefined
|
||||
}
|
||||
|
||||
// Runs of nodes sharing at least their outermost mark, each spelled or carried as a whole.
|
||||
|
||||
@@ -16,7 +16,8 @@ const panelMacro = 2
|
||||
|
||||
const lists = ['bulletList', 'orderedList']
|
||||
const spellableAlt = /^[^\s!|,\\[\]{}](?:[^!|,\\[\]{}\r\n]*[^\s!|,\\[\]{}])?$/
|
||||
const spellableImageUrl = /^[^\s!|\\[\]{}]+$/
|
||||
// Jira reads an address holding no `://` as an attachment's name.
|
||||
const spellableImageUrl = /^[^\s!|\\[\]{}]*:\/\/[^\s!|\\[\]{}]*$/
|
||||
|
||||
// spec/wiki.md, Blocks: each block wiki markup spells, found after its children, since a block rides where one it holds does. A stack rather than recursion (docs/decisions.md §Nothing recurses unbounded).
|
||||
export function spelledBlocks(content: readonly AdfNode[]): Result<Map<AdfNode, Spelling>> {
|
||||
@@ -93,6 +94,7 @@ function leafSpelling(node: AdfNode, path: ConvertErrorPath, inCell: boolean): R
|
||||
function paragraphSpelling(node: AdfNode, path: ConvertErrorPath, inCell: boolean): Result<Spelling | undefined> {
|
||||
if (!holdsExactly(node, {})) return success(undefined)
|
||||
if (node.content === undefined) return success({ blank: true, macros: 0, text: ' ' })
|
||||
if (node.content.length === 0) return success(undefined)
|
||||
const line = inlineLine(node.content, path, inCell)
|
||||
if (!line.ok) return line
|
||||
return success({ blank: node.content.every((child) => child.type === 'text') && /^\s*$/u.test(node.content.map(({ text }) => text).join('')), macros: 0, text: line.value.line })
|
||||
@@ -117,7 +119,7 @@ function codeText(node: AdfNode): string | undefined {
|
||||
const [text, ...others] = nodeContent(node)
|
||||
const code = text?.text
|
||||
if (!holdsOnlyAttributes(node, ['language']) || typeof language !== 'string' || text === undefined || code === undefined || others.length > 0 || !isUnmarkedSpellableText(text)) return undefined
|
||||
if (code.includes('{code}') || code.includes('{noformat}')) return undefined
|
||||
if (code === '' || code.includes('{code}') || code.includes('{noformat}')) return undefined
|
||||
if (language === '') return `{noformat}\n${code}\n{noformat}`
|
||||
return codeLanguages.includes(language) ? `{code:${language}}\n${code}\n{code}` : undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user