mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Refuse a markdown input that is not a string with not-a-string
This commit is contained in:
+1
-1
@@ -208,7 +208,7 @@ function notADocument(message: string): ConvertFault {
|
||||
return { code: 'not-an-adf-document', message }
|
||||
}
|
||||
|
||||
function describe(value: unknown): string {
|
||||
export function describe(value: unknown): string {
|
||||
if (typeof value === 'string') return JSON.stringify(value.length > 40 ? `${value.slice(0, 40)}…` : value)
|
||||
if (typeof value === 'function') return 'a function'
|
||||
if (typeof value === 'object' && value !== null) return Array.isArray(value) ? 'an array' : 'an object'
|
||||
|
||||
@@ -4,7 +4,7 @@ import test from 'node:test'
|
||||
import type { AdfAttributes, AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { ParseError, Result, SourcePosition } from '../../result.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { markdownToAdf } from './markdown-to-adf.ts'
|
||||
import { markdownToAdf, plainMarkdownToAdf } from './markdown-to-adf.ts'
|
||||
|
||||
const em: AdfMark = { type: 'em' }
|
||||
const strike: AdfMark = { type: 'strike' }
|
||||
@@ -84,6 +84,18 @@ function table(...rows: AdfNode[]): AdfNode {
|
||||
return { content: rows, type: 'table' }
|
||||
}
|
||||
|
||||
test('refuses a value that is not a string, in both readers, at the start of the input', () => {
|
||||
const found: [unknown, string][] = [[null, 'null'], [undefined, 'undefined'], [42, '42'], [{}, 'an object'], [['x'], 'an array']]
|
||||
for (const read of [markdownToAdf, plainMarkdownToAdf]) {
|
||||
for (const [value, described] of found) {
|
||||
assert.deepEqual(Reflect.apply(read, undefined, [value]), {
|
||||
error: { code: 'not-a-string', message: `markdown is a string: found ${described}`, path: [], position: { line: 1, offset: 0 } },
|
||||
ok: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test('builds an empty document from input holding no block, and one holding no content key from the doc directive alone', () => {
|
||||
assert.deepEqual(markdownToAdf(''), { ok: true, value: { content: [], type: 'doc', version: 1 } })
|
||||
assert.deepEqual(markdownToAdf('\n \n\t\n'), { ok: true, value: { content: [], type: 'doc', version: 1 } })
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { LinkDefinitions } from './inline-content.ts'
|
||||
import { carryFenceType, readCarriedBlock } from '../opaque-carry.ts'
|
||||
import { commonMarkSpelling, type SpellingMemo } from '../emit/adf-to-markdown.ts'
|
||||
import { documentAttribute, documentName, documentSpelling, listBreakName, listBreakSpelling } from '../block-directive.ts'
|
||||
import { emptyKeys, nodeAttrs, nodeContent } from '../../adf/document.ts'
|
||||
import { describe, 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 '../plain/inline-reduction.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
@@ -39,6 +39,7 @@ export function plainMarkdownToAdf(markdown: string): Result<AdfDocument, ParseE
|
||||
}
|
||||
|
||||
function readDocument(markdown: string, flavour: Flavour): Result<AdfDocument, ParseError> {
|
||||
if (typeof markdown !== 'string') return positioned(failure('not-a-string', `markdown is a string: found ${describe(markdown)}`, []), documentStart)
|
||||
const parsed = parseBlocks(markdown)
|
||||
const [only] = parsed.blocks
|
||||
if (parsed.blocks.length === 1 && only?.kind === 'directive' && only.name === documentName) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export type ConvertErrorCode =
|
||||
| 'malformed-directive'
|
||||
| 'malformed-pipe-table'
|
||||
| 'not-a-string'
|
||||
| 'not-an-adf-document'
|
||||
| 'unknown-directive-name'
|
||||
| 'unmappable-html'
|
||||
|
||||
Reference in New Issue
Block a user