Skip to content

Commit

Permalink
feat: validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Sep 10, 2023
1 parent 86a1adc commit b7ae891
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/process/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-markdoc-preprocess",
"version": "0.3.1",
"version": "0.3.2",
"description": "A Svelte preprocessor that allows you to use Markdoc.",
"type": "commonjs",
"keywords": [
Expand All @@ -27,6 +27,7 @@
"@markdoc/markdoc": "^0.3.0",
"html-escaper": "^3.0.3",
"js-yaml": "^4.1.0",
"lovely-logs": "^1.2.2",
"svelte": "^4.0.0",
"typescript": "^5.0.0"
},
Expand Down
38 changes: 38 additions & 0 deletions packages/process/src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createLogger, Logger } from 'lovely-logs';
import type { ValidateError } from '@markdoc/markdoc';

createLogger({
platform: 'console',
timestampEnabled: false,
});

export function log_validation_error(
validate_error: ValidateError,
filename: string,
) {
const { error } = validate_error;
const prefix = `[svelte-markdoc-preprocess] ${filename}:${validate_error.lines.join(
':',
)} (${error.id}) `;

switch (error.level) {
case 'debug':
case 'info':
return Logger.info(prefix, error.message);
case 'warning':
return Logger.warn(prefix, error.message);
case 'error':
case 'critical':
return Logger.error(prefix, error.message);
}
}

export function log_error(message: string) {
Logger.error(`[svelte-markdoc-preprocess]: ${message}`);
}
export function log_warning(message: string) {
Logger.warn(`[svelte-markdoc-preprocess]: ${message}`);
}
export function log_info(message: string) {
Logger.info(`[svelte-markdoc-preprocess]: ${message}`);
}
13 changes: 12 additions & 1 deletion packages/process/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
NodeType,
Tag,
ConfigType,
validate,
} from '@markdoc/markdoc';
import {
ScriptTarget,
Expand All @@ -30,6 +31,7 @@ import {
import * as default_schema from './default_schema';
import type { Config } from './config';
import { LAYOUT_IMPORT, NODES_IMPORT, TAGS_IMPORT } from './constants';
import { log_error, log_validation_error } from './log';

type Var = {
name: string;
Expand Down Expand Up @@ -140,6 +142,11 @@ export function transformer({
validation: config?.validation,
};

const errors = validate(ast, configuration);
for (const error of errors) {
log_validation_error(error, filename);
}

/**
* transform the ast with svelte components
*/
Expand Down Expand Up @@ -453,7 +460,11 @@ function create_schema(tags: Record<string, Schema>): void {
}
write_to_file(target_file, content);
} catch (err) {
console.error(err);
if (err instanceof Error) {
log_error(err.message);
} else {
console.error(err);
}
}
}
}

0 comments on commit b7ae891

Please sign in to comment.