Skip to content

Commit b7ae891

Browse files
feat: validation errors
1 parent 86a1adc commit b7ae891

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

package-lock.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/process/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-markdoc-preprocess",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "A Svelte preprocessor that allows you to use Markdoc.",
55
"type": "commonjs",
66
"keywords": [
@@ -27,6 +27,7 @@
2727
"@markdoc/markdoc": "^0.3.0",
2828
"html-escaper": "^3.0.3",
2929
"js-yaml": "^4.1.0",
30+
"lovely-logs": "^1.2.2",
3031
"svelte": "^4.0.0",
3132
"typescript": "^5.0.0"
3233
},

packages/process/src/log.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createLogger, Logger } from 'lovely-logs';
2+
import type { ValidateError } from '@markdoc/markdoc';
3+
4+
createLogger({
5+
platform: 'console',
6+
timestampEnabled: false,
7+
});
8+
9+
export function log_validation_error(
10+
validate_error: ValidateError,
11+
filename: string,
12+
) {
13+
const { error } = validate_error;
14+
const prefix = `[svelte-markdoc-preprocess] ${filename}:${validate_error.lines.join(
15+
':',
16+
)} (${error.id}) `;
17+
18+
switch (error.level) {
19+
case 'debug':
20+
case 'info':
21+
return Logger.info(prefix, error.message);
22+
case 'warning':
23+
return Logger.warn(prefix, error.message);
24+
case 'error':
25+
case 'critical':
26+
return Logger.error(prefix, error.message);
27+
}
28+
}
29+
30+
export function log_error(message: string) {
31+
Logger.error(`[svelte-markdoc-preprocess]: ${message}`);
32+
}
33+
export function log_warning(message: string) {
34+
Logger.warn(`[svelte-markdoc-preprocess]: ${message}`);
35+
}
36+
export function log_info(message: string) {
37+
Logger.info(`[svelte-markdoc-preprocess]: ${message}`);
38+
}

packages/process/src/transformer.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
NodeType,
77
Tag,
88
ConfigType,
9+
validate,
910
} from '@markdoc/markdoc';
1011
import {
1112
ScriptTarget,
@@ -30,6 +31,7 @@ import {
3031
import * as default_schema from './default_schema';
3132
import type { Config } from './config';
3233
import { LAYOUT_IMPORT, NODES_IMPORT, TAGS_IMPORT } from './constants';
34+
import { log_error, log_validation_error } from './log';
3335

3436
type Var = {
3537
name: string;
@@ -140,6 +142,11 @@ export function transformer({
140142
validation: config?.validation,
141143
};
142144

145+
const errors = validate(ast, configuration);
146+
for (const error of errors) {
147+
log_validation_error(error, filename);
148+
}
149+
143150
/**
144151
* transform the ast with svelte components
145152
*/
@@ -453,7 +460,11 @@ function create_schema(tags: Record<string, Schema>): void {
453460
}
454461
write_to_file(target_file, content);
455462
} catch (err) {
456-
console.error(err);
463+
if (err instanceof Error) {
464+
log_error(err.message);
465+
} else {
466+
console.error(err);
467+
}
457468
}
458469
}
459470
}

0 commit comments

Comments
 (0)