mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 06:35:44 +01:00
Spell colwidth as a comma list and a block's marks by name
This commit is contained in:
@@ -9,14 +9,14 @@ import type { AttributeKind, AttributeVocabulary } from '../adf/attribute-vocabu
|
||||
import { blockArgument } from '../markdown/block-directive.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { markAttributes } from '../adf/mark-attributes.ts'
|
||||
import { blockMarkAttributes, markAttributes } from '../adf/mark-attributes.ts'
|
||||
|
||||
type Held = Map<string, Set<AttributeKind>>
|
||||
type Properties = Map<string, SchemaObject[]>
|
||||
type SchemaObject = Readonly<Record<string, unknown>>
|
||||
type Spelled = [string, Map<string, AttributeKind>]
|
||||
|
||||
const carried = ['alignment', 'annotation', 'backgroundColor', 'blockCard', 'bodiedRule', 'breakout', 'dataConsumer', 'embedCard', 'fontSize', 'fragment', 'indentation', 'inlineExtension', 'placeholder']
|
||||
const carried = ['annotation', 'backgroundColor', 'blockCard', 'bodiedRule', 'embedCard', 'fontSize', 'inlineExtension', 'placeholder']
|
||||
const definitionReference = '#/definitions/'
|
||||
const gaps: string[] = []
|
||||
const grammarOwn = ['doc', 'text']
|
||||
@@ -85,7 +85,7 @@ function spelled(): Spelled[] {
|
||||
return [
|
||||
...Object.entries(blockNodes).map(([type, model]) => spelledType(type, model.attributes, blockArgument(type))),
|
||||
...Object.entries(inlineNodes).map(([type, model]) => spelledType(type, model.attributes)),
|
||||
...Object.entries(markAttributes).map(([type, attributes]) => spelledType(type, attributes)),
|
||||
...Object.entries({ ...blockMarkAttributes, ...markAttributes }).map(([type, attributes]) => spelledType(type, attributes)),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -166,6 +166,7 @@ function propertyKinds(property: SchemaObject, where: string): AttributeKind[] {
|
||||
if (type === 'boolean') return ['boolean']
|
||||
if (type === 'integer' || type === 'number') return ['number']
|
||||
if (type === 'string') return ['string']
|
||||
if (type === 'array' && isSchemaObject(property['items']) && property['items']['type'] === 'number') return ['numbers']
|
||||
if (type === 'array' || type === 'object') return ['json']
|
||||
if (type !== undefined) return assert.fail(`${where}: the schema types it ${JSON.stringify(type)}, which reads as no attribute kind`)
|
||||
const combinator = ['$ref', 'allOf', 'anyOf'].find((keyword) => property[keyword] !== undefined)
|
||||
|
||||
@@ -7,7 +7,7 @@ import test from 'node:test'
|
||||
import type { AttributeKind, AttributeVocabulary } from '../adf/attribute-vocabulary.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { markAttributes } from '../adf/mark-attributes.ts'
|
||||
import { blockMarkAttributes, markAttributes } from '../adf/mark-attributes.ts'
|
||||
import { textDirectiveName } from '../markdown/text-directive.ts'
|
||||
|
||||
type Declared = { attributes: AttributeVocabulary }
|
||||
@@ -80,7 +80,7 @@ function attributeList(heading: string, prose: string): AttributeVocabulary {
|
||||
|
||||
function attributeKind(heading: string, parenthesized: string): AttributeKind {
|
||||
const first = parenthesized.split(/[\s,]/)[0] ?? ''
|
||||
if (first === 'boolean' || first === 'json' || first === 'number' || first === 'string') return first
|
||||
if (first === 'boolean' || first === 'json' || first === 'number' || first === 'numbers' || first === 'string') return first
|
||||
assert.ok(first.startsWith('`'), `${heading}: ${first} is neither an attribute kind nor a value set`)
|
||||
return 'string'
|
||||
}
|
||||
@@ -104,5 +104,5 @@ test('no name is spelled in more than one position', () => {
|
||||
})
|
||||
|
||||
test('the mark table holds the attributes spec/flavour.md gives each mark', () => {
|
||||
assert.deepEqual(declarations('Marks'), vocabularies(Object.fromEntries(Object.entries(markAttributes).map(([type, attributes]) => [type, { attributes }]))))
|
||||
assert.deepEqual(declarations('Marks'), vocabularies(Object.fromEntries(Object.entries({ ...blockMarkAttributes, ...markAttributes }).map(([type, attributes]) => [type, { attributes }]))))
|
||||
})
|
||||
|
||||
@@ -8,13 +8,13 @@ import type { AttributeKind, AttributeVocabulary } from '../adf/attribute-vocabu
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import type { LinkDefinition } from '../markdown/commonmark/link-syntax.ts'
|
||||
import { blockArgument } from '../markdown/block-directive.ts'
|
||||
import { blockMarkAttributes, markAttributes } from '../adf/mark-attributes.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { centeredImage, externalMedia } from '../markdown/external-image.ts'
|
||||
import { directivePrefix } from '../markdown/directive-syntax.ts'
|
||||
import { emptyKeys, mergeAdjacentText, nodeContent } from '../adf/document.ts'
|
||||
import { inlineNodes } from '../adf/inline-nodes.ts'
|
||||
import { joinsWhenRead } from '../markdown/adjacent-text.ts'
|
||||
import { markAttributes } from '../adf/mark-attributes.ts'
|
||||
|
||||
type Positions = { block: AdfNode; inline: AdfNode }
|
||||
|
||||
@@ -61,6 +61,7 @@ const valueByKind: Readonly<Record<AttributeKind, Arbitrary<JsonValue>>> = {
|
||||
boolean: fc.boolean(),
|
||||
json: jsonValue,
|
||||
number: numberValue,
|
||||
numbers: fc.array(numberValue, { maxLength: 3 }),
|
||||
string: textOf(0),
|
||||
}
|
||||
|
||||
@@ -108,6 +109,7 @@ const mark: Arbitrary<AdfMark> = occasionallyEmpty(fc.oneof(
|
||||
{ arbitrary: fc.record({ attrs: fc.dictionary(jsonKey, jsonValue, { maxKeys: 2, noNullPrototype: true }), type: unknownType }), weight: 1 },
|
||||
))
|
||||
const marks = fc.uniqueArray(mark, { maxLength: 3, selector: (held) => held.type })
|
||||
const blockMark: Arbitrary<AdfMark> = occasionallyEmpty(fc.oneof(...Object.entries(blockMarkAttributes).map(([type, vocabulary]) => attributes(vocabulary).map((attrs) => ({ attrs, type })))))
|
||||
|
||||
const textNode = occasionallyEmpty(fc.record({ marks, text: fc.oneof({ arbitrary: text, weight: 19 }, { arbitrary: fc.constant(''), weight: 1 }) }).map((held): AdfNode => ({ ...held, type: 'text' })))
|
||||
|
||||
@@ -136,7 +138,11 @@ const positions = fc.letrec<Positions>((tie) => {
|
||||
inline: inlineContent,
|
||||
none: fc.constant<AdfNode[]>([]),
|
||||
}
|
||||
const blockMarks = fc.oneof({ arbitrary: fc.constant<AdfMark[]>([]), weight: 4 }, { arbitrary: marks, weight: 1 })
|
||||
const blockMarks = fc.oneof(
|
||||
{ arbitrary: fc.constant<AdfMark[]>([]), weight: 4 },
|
||||
{ arbitrary: marks, weight: 1 },
|
||||
{ arbitrary: fc.uniqueArray(fc.oneof(blockMark, mark), { maxLength: 3, selector: (held) => held.type }), weight: 1 },
|
||||
)
|
||||
const blockArbitraries = Object.entries(blockNodes).map(([type, model]) => {
|
||||
const argument = blockArgument(type)
|
||||
const vocabulary: AttributeVocabulary = argument === undefined ? model.attributes : { ...model.attributes, [argument]: 'string' }
|
||||
|
||||
Reference in New Issue
Block a user