@clarusjs/content 0.2.0 → 0.2.2
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 +28 -0
- package/dist/index.js +43 -2
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -27,7 +27,35 @@ export type MarkdownOptions = {
|
|
|
27
27
|
};
|
|
28
28
|
/** `::: quiz` is for lessons (frontmatter `steps: true`); 'everywhere' allows it on any page. */
|
|
29
29
|
quizzes?: 'lessons' | 'everywhere';
|
|
30
|
+
/**
|
|
31
|
+
* Validate each page's frontmatter: one [Standard Schema](https://standardschema.dev) (Zod, Valibot, ArkType…), or
|
|
32
|
+
* one per folder under src/pages (`{ 'guide/**': Guide, 'blog/*.md': Post }`, the first matching glob wins). A
|
|
33
|
+
* page that doesn't match fails the build; the schema's output (defaults, coercions) is what the page gets.
|
|
34
|
+
* Synchronous schemas only.
|
|
35
|
+
* @see https://clarusjs.com/guide/markdown#frontmatter-schemas
|
|
36
|
+
*/
|
|
37
|
+
schema?: StandardSchema | Record<string, StandardSchema>;
|
|
38
|
+
};
|
|
39
|
+
/** The Standard Schema interface (https://standardschema.dev), as much of it as validating frontmatter needs. */
|
|
40
|
+
export type StandardSchema<Output = unknown> = {
|
|
41
|
+
readonly '~standard': {
|
|
42
|
+
readonly version: 1;
|
|
43
|
+
readonly vendor: string;
|
|
44
|
+
validate(value: unknown): StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
type StandardResult<Output> = {
|
|
48
|
+
readonly value: Output;
|
|
49
|
+
readonly issues?: undefined;
|
|
50
|
+
} | {
|
|
51
|
+
readonly issues: ReadonlyArray<{
|
|
52
|
+
readonly message: string;
|
|
53
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
54
|
+
readonly key: PropertyKey;
|
|
55
|
+
}>;
|
|
56
|
+
}>;
|
|
30
57
|
};
|
|
31
58
|
/** 'Hello, World!' → 'hello-world'. Shared by heading ids and (later) link checking. */
|
|
32
59
|
export declare function slugify(text: string): string;
|
|
33
60
|
export default function markdown(options?: MarkdownOptions): Source;
|
|
61
|
+
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' };
|
|
@@ -270,7 +297,21 @@ function pageHead(data, title) {
|
|
|
270
297
|
return tags.length || raws.length || ld.length ? `<cl-head>${staticHtml(tags.join(''))}${raws.join('')}${ld.join('')}</cl-head>` : '';
|
|
271
298
|
}
|
|
272
299
|
function staticHtml(html) {
|
|
273
|
-
|
|
300
|
+
const inertText = (s) => s.replace(/\{\{/g, '{{').replace(/@/g, '@');
|
|
301
|
+
let out = '';
|
|
302
|
+
let at = 0;
|
|
303
|
+
for (const m of html.matchAll(/<([\w-]+)(\s[^>]*)?>/g)) {
|
|
304
|
+
const names = (m[2] ?? '').replace(/=\s*("[^"]*"|'[^']*'|[^\s>]*)/g, '');
|
|
305
|
+
if (m.index < at || !/(^|\s)verbatim(?=[\s/]|$)/.test(names))
|
|
306
|
+
continue;
|
|
307
|
+
const start = m.index + m[0].length;
|
|
308
|
+
const close = html.indexOf(`</${m[1]}>`, start);
|
|
309
|
+
if (close === -1)
|
|
310
|
+
continue;
|
|
311
|
+
out += inertText(html.slice(at, start)) + html.slice(start, close);
|
|
312
|
+
at = close;
|
|
313
|
+
}
|
|
314
|
+
return out + inertText(html.slice(at));
|
|
274
315
|
}
|
|
275
316
|
function protectTags(body, components, saved) {
|
|
276
317
|
const code = /(?:^|\n)[ \t]*(`{3,}|~{3,})[^\n]*\n[\s\S]*?(?:\n[ \t]*\1[`~]*[ \t\r]*(?=\n|$)|$)|(`+)[\s\S]*?[^`]\2(?!`)/;
|
|
@@ -293,7 +334,7 @@ export default function markdown(options = {}) {
|
|
|
293
334
|
toTscl(code, file, context) {
|
|
294
335
|
const { route } = context;
|
|
295
336
|
const front = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(code);
|
|
296
|
-
const data = (front ? parseYaml(front[1]) : null) ?? {};
|
|
337
|
+
const data = validateFrontmatter((front ? parseYaml(front[1]) : null) ?? {}, options.schema, file, context.root);
|
|
297
338
|
let body = expand(front ? code.slice(front[0].length) : code, file, context);
|
|
298
339
|
const imports = [];
|
|
299
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.2",
|
|
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.9.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.9.0",
|
|
54
54
|
"typescript": "^7.0.2",
|
|
55
55
|
"vite": "^8.3.8",
|
|
56
56
|
"vitest": "^5.1.1"
|