Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Oct 1, 2023
2 parents 311dd9a + 96d80ac commit 2d6125b
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apps/demo/src/lib/layouts/Default.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</script>

<svelte:head>
<title>svelte-markdoc-preprocess{title ? ` | ${title}` : ''}</title>
<title>{title ? `${title} | ` : ''}svelte-markdoc-preprocess</title>
<meta
name="description"
content="A Svelte preprocessor that brings the power of Markdoc right into your Svelte applications!"
Expand Down
5 changes: 5 additions & 0 deletions apps/demo/src/routes/documentation/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
name: 'Install',
is_selected: $page.url.pathname === '/documentation',
},
{
path: '/documentation/configuration',
name: 'Configuration',
is_selected: $page.url.pathname === '/documentation/configuration',
},
{
path: '/documentation/nodes',
name: 'Nodes',
Expand Down
78 changes: 78 additions & 0 deletions apps/demo/src/routes/documentation/configuration/+page.markdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Configuration
---

## Usage

You can pass the configuration to the preprocessor in the `svelte.config.js` like this:

```js
import { markdoc } from 'svelte-markdoc-preprocess';

const config = {
preprocess: [
vitePreprocess(),
markdoc({
// configuration here
})
]
};
```

## Options

#### `extensions`

**Type**: `string[]`

**Default**: `['.markdoc', '.mdoc', '.markdown', '.md']`

Extensions to be processed.

#### `nodes`

**Type**: `string | null`

**Default**: `null`

Absoulute path to the `.svelte` file exporting components for nodes.

#### tags

**Type**: `string | null`

**Default**: `null`

Absoulute path to the `.svelte` file exporting components for tags.

#### partials

**Type**: `string`

**Default**: `null`

Absoulute path to the folder for partials.

#### generateSchema

**Type**: `boolean`

**Default**: `true`

Generate schema files under `./svelte-kit/markdoc-schema.json` to be used with the official [Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=Stripe.markdoc-language-support).

#### layouts

**Type**: `Record<string, string> | 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.
2 changes: 1 addition & 1 deletion packages/process/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tests/**/*.test.mjs
tests/
tsconfig.json
.prettierrc
6 changes: 5 additions & 1 deletion packages/process/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigType } from '@markdoc/markdoc';
import { ConfigType, ValidateError } from '@markdoc/markdoc';

export type Config = {
/**
Expand Down Expand Up @@ -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.
*/
Expand Down
1 change: 0 additions & 1 deletion packages/process/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function log_validation_error(
case 'error':
case 'critical':
Logger.error(prefix, error.message);
throw new Error(error.message);
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/process/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const default_config: Config = {
tags: null,
partials: null,
config: null,
validationThreshold: 'error',
};

const processor = ({
Expand All @@ -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',
Expand All @@ -45,6 +47,7 @@ const processor = ({
nodes_file: nodes,
tags_file: tags,
partials_dir: partials,
validation_threshold: validationThreshold,
});

return {
Expand Down
17 changes: 15 additions & 2 deletions packages/process/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function transformer({
layouts,
generate_schema,
config,
validation_threshold,
}: {
content: string;
filename: string;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -152,9 +154,21 @@ export function transformer({
/**
* validate markdoc asd and log errors, warnings & co
*/
const thresholds = new Map<Config['validationThreshold'], number>([
['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);
}
}

/**
Expand Down Expand Up @@ -358,7 +372,6 @@ function get_node_defaults(node_type: NodeType): Partial<Schema> {

function prepare_nodes(
nodes_file: Config['nodes'],
dependencies: string,
): Partial<Record<NodeType, Schema>> {
const nodes: Record<string, Schema> = {};
if (nodes_file) {
Expand Down
28 changes: 18 additions & 10 deletions packages/process/tests/processor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});
}),
);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { markdoc } from '../../../dist/module.js';

export default markdoc();

export const exception = "Undefined tag: 'unknown_component'";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% unknown_component /%}

0 comments on commit 2d6125b

Please sign in to comment.