23 - the block directive's spellings are one file
This commit was merged in pull request #122.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import type { AdfMark } from '../adf/document.ts'
|
||||
import type { BlockType } from '../adf/block-nodes.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { blockNodeModel } from '../adf/block-nodes.ts'
|
||||
import { isAdfMark, nodeAttrs } from '../adf/document.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
import { spellDirectiveOpener } from './directive-syntax.ts'
|
||||
|
||||
const argumentByType = new Map(
|
||||
Object.entries({
|
||||
blockTaskItem: 'state',
|
||||
panel: 'panelType',
|
||||
taskItem: 'state',
|
||||
} satisfies Partial<Record<BlockType, string>>),
|
||||
)
|
||||
|
||||
export const listBreakName = 'listBreak'
|
||||
|
||||
export const listBreakSpelling = spellDirectiveOpener(listBreakName, undefined, '')
|
||||
|
||||
export const marksAttribute = 'marks'
|
||||
|
||||
export function blockArgument(type: string): string | undefined {
|
||||
return argumentByType.get(type)
|
||||
}
|
||||
|
||||
export function blockDirectiveForm(name: string): 'container' | 'leaf' | undefined {
|
||||
if (name === listBreakName) return 'leaf'
|
||||
const model = blockNodeModel(name)
|
||||
if (model === undefined) return undefined
|
||||
return model.contentModel === 'none' ? 'leaf' : 'container'
|
||||
}
|
||||
|
||||
export function markValues(marks: readonly AdfMark[]): JsonValue {
|
||||
return marks.map((mark) => {
|
||||
const attrs = nodeAttrs(mark)
|
||||
return Object.keys(attrs).length === 0 ? { type: mark.type } : { attrs, type: mark.type }
|
||||
})
|
||||
}
|
||||
|
||||
export function readMarkValues(value: JsonValue): AdfMark[] | undefined {
|
||||
if (!Array.isArray(value) || value.length === 0) return undefined
|
||||
const marks: AdfMark[] = []
|
||||
for (const item of value) {
|
||||
if (!isAdfMark(item)) return undefined
|
||||
marks.push(item)
|
||||
}
|
||||
return serializeCanonicalJson(markValues(marks), 'compact') === serializeCanonicalJson(value, 'compact') ? marks : undefined
|
||||
}
|
||||
Reference in New Issue
Block a user