From 513f21ff40a928351d5fdc0211a7e375f5b15dc7 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Sun, 1 Oct 2023 14:22:18 +0200 Subject: [PATCH 1/2] feat: add validation error threshold --- apps/demo/src/lib/layouts/Default.svelte | 2 +- .../src/routes/documentation/+layout.svelte | 5 ++ .../documentation/configuration/+page.markdoc | 78 +++++++++++++++++++ packages/process/src/config.ts | 6 +- packages/process/src/log.ts | 1 - packages/process/src/processor.ts | 3 + packages/process/src/transformer.ts | 17 +++- packages/process/tests/processor.test.mjs | 28 ++++--- .../validation threshold/compiled.txt | 0 .../processor/validation threshold/config.mjs | 5 ++ .../validation threshold/source.markdoc | 1 + 11 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 apps/demo/src/routes/documentation/configuration/+page.markdoc create mode 100644 packages/process/tests/processor/validation threshold/compiled.txt create mode 100644 packages/process/tests/processor/validation threshold/config.mjs create mode 100644 packages/process/tests/processor/validation threshold/source.markdoc diff --git a/apps/demo/src/lib/layouts/Default.svelte b/apps/demo/src/lib/layouts/Default.svelte index bec27b9..d1ecf37 100644 --- a/apps/demo/src/lib/layouts/Default.svelte +++ b/apps/demo/src/lib/layouts/Default.svelte @@ -3,7 +3,7 @@ - svelte-markdoc-preprocess{title ? ` | ${title}` : ''} + {title ? `${title} | ` : ''}svelte-markdoc-preprocess | null` + +**Default**: `null` + +Layouts to be used for pages. + +#### validationThreshold + +**Type**: `"error" | "debug" | "info" | "warning" | "critical"` + +**Default**: `error` + +The threshold for validation errors to stop the build. diff --git a/packages/process/src/config.ts b/packages/process/src/config.ts index 7a47e3d..5dd9875 100644 --- a/packages/process/src/config.ts +++ b/packages/process/src/config.ts @@ -1,4 +1,4 @@ -import { ConfigType } from '@markdoc/markdoc'; +import { ConfigType, ValidateError } from '@markdoc/markdoc'; export type Config = { /** @@ -28,6 +28,10 @@ export type Config = { default: string; [key: string]: string; } | null; + /** + * The threshold for validation errors to stop the build. + */ + validationThreshold: ValidateError['error']['level'] | null; /** * Configuration for the markdoc compiler. */ diff --git a/packages/process/src/log.ts b/packages/process/src/log.ts index 748c184..96d1b51 100644 --- a/packages/process/src/log.ts +++ b/packages/process/src/log.ts @@ -27,7 +27,6 @@ export function log_validation_error( case 'error': case 'critical': Logger.error(prefix, error.message); - throw new Error(error.message); } } diff --git a/packages/process/src/processor.ts b/packages/process/src/processor.ts index a2bfb2f..34eb07c 100644 --- a/packages/process/src/processor.ts +++ b/packages/process/src/processor.ts @@ -10,6 +10,7 @@ const default_config: Config = { tags: null, partials: null, config: null, + validationThreshold: 'error', }; const processor = ({ @@ -20,6 +21,7 @@ const processor = ({ tags = default_config.tags, partials = default_config.partials, config = default_config.config, + validationThreshold = default_config.validationThreshold, }: Config = default_config): PreprocessorGroup => { return { name: 'svelte-markdoc-preprocess', @@ -45,6 +47,7 @@ const processor = ({ nodes_file: nodes, tags_file: tags, partials_dir: partials, + validation_threshold: validationThreshold, }); return { diff --git a/packages/process/src/transformer.ts b/packages/process/src/transformer.ts index 4574d6d..0a056f9 100644 --- a/packages/process/src/transformer.ts +++ b/packages/process/src/transformer.ts @@ -47,6 +47,7 @@ export function transformer({ layouts, generate_schema, config, + validation_threshold, }: { content: string; filename: string; @@ -56,6 +57,7 @@ export function transformer({ layouts: Config['layouts']; generate_schema: Config['generateSchema']; config: Config['config']; + validation_threshold: Config['validationThreshold']; }): string { /** * create ast for markdoc @@ -84,7 +86,7 @@ export function transformer({ let dependencies = ''; const tags = prepare_tags(tags_file); const has_tags = Object.keys(tags).length > 0; - const nodes = prepare_nodes(nodes_file, dependencies); + const nodes = prepare_nodes(nodes_file); const has_nodes = Object.keys(nodes).length > 0; const partials = prepare_partials(partials_dir); @@ -152,9 +154,21 @@ export function transformer({ /** * validate markdoc asd and log errors, warnings & co */ + const thresholds = new Map([ + ['debug', 0], + ['info', 1], + ['warning', 2], + ['error', 3], + ['critical', 4], + ]); + const threshold = thresholds.get(validation_threshold); const errors = validate(ast, configuration); for (const error of errors) { log_validation_error(error, filename); + const level = thresholds.get(error.error.level); + if (threshold && level && level >= threshold) { + throw new Error(error.error.message); + } } /** @@ -358,7 +372,6 @@ function get_node_defaults(node_type: NodeType): Partial { function prepare_nodes( nodes_file: Config['nodes'], - dependencies: string, ): Partial> { const nodes: Record = {}; if (nodes_file) { diff --git a/packages/process/tests/processor.test.mjs b/packages/process/tests/processor.test.mjs index 63341fe..0f7aefa 100644 --- a/packages/process/tests/processor.test.mjs +++ b/packages/process/tests/processor.test.mjs @@ -68,16 +68,24 @@ test('preprocessor', async (context) => { await Promise.all( files.map(async (entry) => { return context.test('tests ' + basename(entry), async () => { - const before = await read_file(join(entry, 'source.markdoc')); - const after = await read_file(join(entry, 'compiled.txt')); - const preprocess = await import( - '../' + join(entry, 'config.mjs') - ).then((m) => m.default); - const markup = await preprocess.markup({ - content: before, - filename: 'test.markdoc', - }); - assert.equal(markup.code, after); + const before = read_file(join(entry, 'source.markdoc')); + const after = read_file(join(entry, 'compiled.txt')); + const config = await import('../' + join(entry, 'config.mjs')); + const preprocess = config.default; + const exception = config.exception ?? false; + try { + const markup = await preprocess.markup({ + content: before, + filename: 'test.markdoc', + }); + assert.equal(markup.code, after); + } catch (error) { + if (exception) { + assert.equal(error.message, exception); + } else { + throw error; + } + } }); }), ); diff --git a/packages/process/tests/processor/validation threshold/compiled.txt b/packages/process/tests/processor/validation threshold/compiled.txt new file mode 100644 index 0000000..e69de29 diff --git a/packages/process/tests/processor/validation threshold/config.mjs b/packages/process/tests/processor/validation threshold/config.mjs new file mode 100644 index 0000000..3c169cc --- /dev/null +++ b/packages/process/tests/processor/validation threshold/config.mjs @@ -0,0 +1,5 @@ +import { markdoc } from '../../../dist/module.js'; + +export default markdoc(); + +export const exception = "Undefined tag: 'unknown_component'"; diff --git a/packages/process/tests/processor/validation threshold/source.markdoc b/packages/process/tests/processor/validation threshold/source.markdoc new file mode 100644 index 0000000..3986f20 --- /dev/null +++ b/packages/process/tests/processor/validation threshold/source.markdoc @@ -0,0 +1 @@ +{% unknown_component /%} From 96d80ac27e92e1724e646df6cfd1685412f7c704 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Sun, 1 Oct 2023 14:24:38 +0200 Subject: [PATCH 2/2] chore: bump package --- package-lock.json | 2 +- packages/process/.npmignore | 2 +- packages/process/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 63a451b..910af5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5819,7 +5819,7 @@ }, "packages/process": { "name": "svelte-markdoc-preprocess", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "dependencies": { "@markdoc/markdoc": "^0.3.0", diff --git a/packages/process/.npmignore b/packages/process/.npmignore index 4061877..e23f153 100644 --- a/packages/process/.npmignore +++ b/packages/process/.npmignore @@ -1,3 +1,3 @@ -tests/**/*.test.mjs +tests/ tsconfig.json .prettierrc diff --git a/packages/process/package.json b/packages/process/package.json index e0b8cc2..5e2d1c3 100644 --- a/packages/process/package.json +++ b/packages/process/package.json @@ -1,6 +1,6 @@ { "name": "svelte-markdoc-preprocess", - "version": "1.0.0", + "version": "1.0.1", "description": "A Svelte preprocessor that allows you to use Markdoc.", "type": "commonjs", "keywords": [