@clarusjs/content 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +59 -1
- package/dist/index.js +29 -2
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { Source } from 'clarusjs/vite';
|
|
2
|
+
/**
|
|
3
|
+
* `markdown()`'s options: the layout, components for blocks, code copy and quizzes, and frontmatter schemas.
|
|
4
|
+
* @see https://clarusjs.com/reference/content#markdown
|
|
5
|
+
*/
|
|
2
6
|
export type MarkdownOptions = {
|
|
3
7
|
/** The layout every page renders in, root-relative (`/src/layouts/docs.tscl`). Frontmatter `layout` overrides it; `false` means none. */
|
|
4
8
|
layout?: string | false;
|
|
@@ -27,7 +31,61 @@ export type MarkdownOptions = {
|
|
|
27
31
|
};
|
|
28
32
|
/** `::: quiz` is for lessons (frontmatter `steps: true`); 'everywhere' allows it on any page. */
|
|
29
33
|
quizzes?: 'lessons' | 'everywhere';
|
|
34
|
+
/**
|
|
35
|
+
* Validate each page's frontmatter: one [Standard Schema](https://standardschema.dev) (Zod, Valibot, ArkType…), or
|
|
36
|
+
* one per folder under src/pages (`{ 'guide/**': Guide, 'blog/*.md': Post }`, the first matching glob wins). A
|
|
37
|
+
* page that doesn't match fails the build; the schema's output (defaults, coercions) is what the page gets.
|
|
38
|
+
* Synchronous schemas only.
|
|
39
|
+
* @see https://clarusjs.com/guide/markdown#frontmatter-schemas
|
|
40
|
+
*/
|
|
41
|
+
schema?: StandardSchema | Record<string, StandardSchema>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* The Standard Schema interface (https://standardschema.dev), as much of it as validating frontmatter needs.
|
|
45
|
+
* @see https://clarusjs.com/reference/content#markdown
|
|
46
|
+
*/
|
|
47
|
+
export type StandardSchema<Output = unknown> = {
|
|
48
|
+
readonly '~standard': {
|
|
49
|
+
readonly version: 1;
|
|
50
|
+
readonly vendor: string;
|
|
51
|
+
validate(value: unknown): StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
type StandardResult<Output> = {
|
|
55
|
+
readonly value: Output;
|
|
56
|
+
readonly issues?: undefined;
|
|
57
|
+
} | {
|
|
58
|
+
readonly issues: ReadonlyArray<{
|
|
59
|
+
readonly message: string;
|
|
60
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
61
|
+
readonly key: PropertyKey;
|
|
62
|
+
}>;
|
|
63
|
+
}>;
|
|
30
64
|
};
|
|
31
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* 'Hello, World!' → 'hello-world'. Shared by heading ids and (later) link checking.
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* slugify('Getting started!') // 'getting-started'
|
|
70
|
+
* ```
|
|
71
|
+
* @see https://clarusjs.com/reference/content#slugify
|
|
72
|
+
*/
|
|
32
73
|
export declare function slugify(text: string): string;
|
|
74
|
+
/**
|
|
75
|
+
* Markdown pages as a page source: `src/pages/**\/*.md` become pages, rendered in your layout, with frontmatter, headings
|
|
76
|
+
* with anchors, highlighted code, `::: tip` blocks, components imported at the top, and (in lessons) quizzes.
|
|
77
|
+
* Build-time only: nothing of it reaches the browser.
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* // vite.config.ts
|
|
81
|
+
* import clarusjs from 'clarusjs/vite'
|
|
82
|
+
* import markdown from '@clarusjs/content'
|
|
83
|
+
*
|
|
84
|
+
* export default defineConfig({
|
|
85
|
+
* plugins: [clarusjs({ sources: [markdown({ layout: '/src/layouts/docs.tscl' })] })],
|
|
86
|
+
* })
|
|
87
|
+
* ```
|
|
88
|
+
* @see https://clarusjs.com/reference/content#markdown
|
|
89
|
+
*/
|
|
33
90
|
export default function markdown(options?: MarkdownOptions): Source;
|
|
91
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,33 @@ import MarkdownIt from 'markdown-it';
|
|
|
3
3
|
import { parse as parseYaml } from 'yaml';
|
|
4
4
|
import { highlight } from './highlight.js';
|
|
5
5
|
import { expand } from './snippets.js';
|
|
6
|
+
function globToRegex(glob) {
|
|
7
|
+
const pattern = glob
|
|
8
|
+
.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
|
|
9
|
+
.replace(/\*\*\//g, '\0A').replace(/\/\*\*$/, '\0B').replace(/\*\*/g, '\0C')
|
|
10
|
+
.replace(/\*/g, '[^/]*')
|
|
11
|
+
.replace(/\0A/g, '(?:.*/)?').replace(/\0B/g, '(?:/.*)?').replace(/\0C/g, '.*');
|
|
12
|
+
return new RegExp(`^${pattern}$`);
|
|
13
|
+
}
|
|
14
|
+
function validateFrontmatter(data, schema, file, root) {
|
|
15
|
+
if (!schema)
|
|
16
|
+
return data;
|
|
17
|
+
const rel = relative(root, file).split(sep).join('/');
|
|
18
|
+
const underPages = rel.replace(/^src\/pages\//, '');
|
|
19
|
+
const pick = '~standard' in schema ? schema : Object.entries(schema).find(([glob]) => globToRegex(glob).test(underPages))?.[1];
|
|
20
|
+
if (!pick)
|
|
21
|
+
return data;
|
|
22
|
+
const result = pick['~standard'].validate(data);
|
|
23
|
+
if (result instanceof Promise)
|
|
24
|
+
throw new Error(`${rel}: the frontmatter schema validated asynchronously; markdown({ schema }) needs a synchronous schema`);
|
|
25
|
+
if (result.issues) {
|
|
26
|
+
throw new Error(result.issues.map((issue) => {
|
|
27
|
+
const path = (issue.path ?? []).map((p) => String(typeof p === 'object' ? p.key : p)).join('.');
|
|
28
|
+
return `${rel}: frontmatter${path ? `.${path}` : ''}: ${issue.message}`;
|
|
29
|
+
}).join('\n'));
|
|
30
|
+
}
|
|
31
|
+
return result.value;
|
|
32
|
+
}
|
|
6
33
|
const TYPES = ['tip', 'info', 'warning', 'danger', 'details', 'code-group', 'quiz'];
|
|
7
34
|
const TITLES = { tip: 'Tip', info: 'Info', warning: 'Warning', danger: 'Danger', details: 'Details', note: 'Note', important: 'Important', caution: 'Caution' };
|
|
8
35
|
const ALERTS = { note: 'info', tip: 'tip', important: 'info', warning: 'warning', caution: 'danger' };
|
|
@@ -132,7 +159,7 @@ function renderer(options) {
|
|
|
132
159
|
const q = meta;
|
|
133
160
|
if (!options.quiz)
|
|
134
161
|
return `<div class="quiz" data-quiz="${q.name}">`;
|
|
135
|
-
tags.push(`<${options.quiz.tag} [name]="${quote(q.name)}" [question]="${
|
|
162
|
+
tags.push(`<${options.quiz.tag} [name]="${quote(q.name)}" [question]="${quote(title)}" [options]="[${q.options.map(quote).join(', ')}]" [answer]="${q.answer}">`);
|
|
136
163
|
return `<!--cl-tag-${tags.length - 1}-->`;
|
|
137
164
|
}
|
|
138
165
|
if (options.container) {
|
|
@@ -307,7 +334,7 @@ export default function markdown(options = {}) {
|
|
|
307
334
|
toTscl(code, file, context) {
|
|
308
335
|
const { route } = context;
|
|
309
336
|
const front = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(code);
|
|
310
|
-
const data = (front ? parseYaml(front[1]) : null) ?? {};
|
|
337
|
+
const data = validateFrontmatter((front ? parseYaml(front[1]) : null) ?? {}, options.schema, file, context.root);
|
|
311
338
|
let body = expand(front ? code.slice(front[0].length) : code, file, context);
|
|
312
339
|
const imports = [];
|
|
313
340
|
for (;;) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarusjs/content",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Markdown pages for clarusjs: frontmatter, layouts, components in Markdown, rendered to static HTML at build time.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"clarusjs": "^0.
|
|
22
|
+
"clarusjs": "^0.10.0"
|
|
23
23
|
},
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@types/markdown-it": "^14.2.0",
|
|
52
52
|
"@types/node": "^26.6.6",
|
|
53
|
-
"clarusjs": "^0.
|
|
53
|
+
"clarusjs": "^0.10.0",
|
|
54
54
|
"typescript": "^7.0.2",
|
|
55
55
|
"vite": "^8.3.8",
|
|
56
56
|
"vitest": "^5.1.1"
|