@kiln-js/md 0.0.0-stage → 0.1.1
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 +5 -0
- package/dist/index.js +115 -0
- package/dist/parse.d.ts +19 -0
- package/dist/plugin.d.ts +14 -0
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.js +123 -0
- package/package.json +49 -5
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/plugin.ts
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
// src/parse.ts
|
|
7
|
+
import { marked } from "marked";
|
|
8
|
+
function splitFrontmatter(src) {
|
|
9
|
+
const m = src.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?/);
|
|
10
|
+
if (!m)
|
|
11
|
+
return { data: {}, body: src };
|
|
12
|
+
const data = {};
|
|
13
|
+
for (const line of m[1].split(/\r?\n/)) {
|
|
14
|
+
const kv = line.match(/^([\w-]+):\s*(.*)$/);
|
|
15
|
+
if (kv)
|
|
16
|
+
data[kv[1]] = kv[2].replace(/^(["'])(.*)\1$/, "$2");
|
|
17
|
+
}
|
|
18
|
+
return { data, body: src.slice(m[0].length) };
|
|
19
|
+
}
|
|
20
|
+
function slugify(text) {
|
|
21
|
+
return text.toLowerCase().trim().replace(/[^\p{L}\p{N}\s-]/gu, "").replace(/\s+/g, "-");
|
|
22
|
+
}
|
|
23
|
+
var plain = (s) => s.replace(/[*_`~[\]]/g, "");
|
|
24
|
+
function strip(t) {
|
|
25
|
+
const { raw: _raw, ...rest } = t;
|
|
26
|
+
for (const key of ["tokens", "items"]) {
|
|
27
|
+
if (Array.isArray(rest[key]))
|
|
28
|
+
rest[key] = rest[key].map(strip);
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(rest.header))
|
|
31
|
+
rest.header = rest.header.map(strip);
|
|
32
|
+
if (Array.isArray(rest.rows))
|
|
33
|
+
rest.rows = rest.rows.map((row) => row.map(strip));
|
|
34
|
+
return rest;
|
|
35
|
+
}
|
|
36
|
+
function parseMarkdown(src) {
|
|
37
|
+
const { data, body } = splitFrontmatter(src);
|
|
38
|
+
const lexed = marked.lexer(body);
|
|
39
|
+
const seen = new Map;
|
|
40
|
+
const headings = [];
|
|
41
|
+
for (const t of lexed) {
|
|
42
|
+
if (t.type !== "heading")
|
|
43
|
+
continue;
|
|
44
|
+
const text = plain(t.text);
|
|
45
|
+
const base = slugify(text) || "section";
|
|
46
|
+
const n = seen.get(base) ?? 0;
|
|
47
|
+
seen.set(base, n + 1);
|
|
48
|
+
t.id = n ? `${base}-${n}` : base;
|
|
49
|
+
headings.push({ depth: t.depth, text, id: t.id });
|
|
50
|
+
}
|
|
51
|
+
return { frontmatter: data, tokens: lexed.map(strip), headings };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/plugin.ts
|
|
55
|
+
var SUFFIX = ".kiln-md.tsx";
|
|
56
|
+
var norm = (p) => p.replace(/\\/g, "/");
|
|
57
|
+
function generateModule(src, opts) {
|
|
58
|
+
const { frontmatter, tokens, headings } = parseMarkdown(src);
|
|
59
|
+
return [
|
|
60
|
+
`import { createElement as h, Fragment } from "react";`,
|
|
61
|
+
`import { Title } from "@kiln-js/core";`,
|
|
62
|
+
`import { Markdown } from "@kiln-js/md/runtime";`,
|
|
63
|
+
opts.components ? `import __mdComponents from ${JSON.stringify(opts.components)};` : `const __mdComponents = undefined;`,
|
|
64
|
+
`export const frontmatter = ${JSON.stringify(frontmatter)};`,
|
|
65
|
+
`export const headings = ${JSON.stringify(headings)};`,
|
|
66
|
+
`const tokens = ${JSON.stringify(tokens)};`,
|
|
67
|
+
`export default function MarkdownPage() {`,
|
|
68
|
+
` return h(Fragment, null,`,
|
|
69
|
+
` frontmatter.title ? h(Title, null, frontmatter.title) : null,`,
|
|
70
|
+
` h(Markdown, { tokens, components: __mdComponents, allowHtml: ${opts.html} }));`,
|
|
71
|
+
`}`
|
|
72
|
+
].join(`
|
|
73
|
+
`);
|
|
74
|
+
}
|
|
75
|
+
function mdPlugin(opts = {}) {
|
|
76
|
+
const html = opts.html ?? false;
|
|
77
|
+
let root = process.cwd();
|
|
78
|
+
let pagesRoot = "";
|
|
79
|
+
let components = "";
|
|
80
|
+
return {
|
|
81
|
+
name: "kiln:md",
|
|
82
|
+
configResolved(config) {
|
|
83
|
+
root = config.root;
|
|
84
|
+
pagesRoot = norm(path.resolve(root, opts.dir ?? "app/pages")) + "/";
|
|
85
|
+
components = opts.components ? norm(path.resolve(root, opts.components)) : "";
|
|
86
|
+
},
|
|
87
|
+
resolveId(source) {
|
|
88
|
+
const file = norm(source);
|
|
89
|
+
if (file.endsWith(".md") && file.startsWith(pagesRoot)) {
|
|
90
|
+
return source + SUFFIX;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
load(id) {
|
|
94
|
+
if (!id.endsWith(SUFFIX))
|
|
95
|
+
return;
|
|
96
|
+
const mdPath = id.slice(0, -SUFFIX.length);
|
|
97
|
+
const src = fs.readFileSync(mdPath, "utf8");
|
|
98
|
+
this.addWatchFile(mdPath);
|
|
99
|
+
return { code: generateModule(src, { components, html }), map: null };
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/index.ts
|
|
105
|
+
function md(options = {}) {
|
|
106
|
+
return {
|
|
107
|
+
name: "@kiln-js/md",
|
|
108
|
+
vite: () => [mdPlugin(options)]
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
var src_default = md;
|
|
112
|
+
export {
|
|
113
|
+
src_default as default,
|
|
114
|
+
md
|
|
115
|
+
};
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type Frontmatter = Record<string, string>;
|
|
2
|
+
export interface Heading {
|
|
3
|
+
depth: number;
|
|
4
|
+
text: string;
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
export interface ParsedMarkdown {
|
|
8
|
+
frontmatter: Frontmatter;
|
|
9
|
+
tokens: Tok[];
|
|
10
|
+
headings: Heading[];
|
|
11
|
+
}
|
|
12
|
+
type Tok = Record<string, any>;
|
|
13
|
+
export declare function splitFrontmatter(src: string): {
|
|
14
|
+
data: Frontmatter;
|
|
15
|
+
body: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function slugify(text: string): string;
|
|
18
|
+
export declare function parseMarkdown(src: string): ParsedMarkdown;
|
|
19
|
+
export {};
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
export interface MdOptions {
|
|
3
|
+
/** Folder whose .md files become pages. Default: app/pages */
|
|
4
|
+
dir?: string;
|
|
5
|
+
/** Module whose default export maps tag names to components, such as app/markdown.tsx */
|
|
6
|
+
components?: string;
|
|
7
|
+
/** Allow raw HTML inside Markdown. Enable only for trusted content. Default: false */
|
|
8
|
+
html?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function generateModule(src: string, opts: {
|
|
11
|
+
components?: string;
|
|
12
|
+
html: boolean;
|
|
13
|
+
}): string;
|
|
14
|
+
export declare function mdPlugin(opts?: MdOptions): Plugin;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ComponentType, type ReactNode } from "react";
|
|
2
|
+
export type Token = Record<string, any>;
|
|
3
|
+
export interface MdCodeProps {
|
|
4
|
+
lang?: string;
|
|
5
|
+
code: string;
|
|
6
|
+
}
|
|
7
|
+
export type MdTag = "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p" | "a" | "img" | "pre" | "code" | "strong" | "em" | "del" | "br" | "ul" | "ol" | "li" | "input" | "blockquote" | "hr" | "table" | "thead" | "tbody" | "tr" | "th" | "td";
|
|
8
|
+
export type MdComponents = Partial<Record<MdTag, ComponentType<any> | string>>;
|
|
9
|
+
export interface MarkdownProps {
|
|
10
|
+
tokens: Token[];
|
|
11
|
+
components?: MdComponents;
|
|
12
|
+
allowHtml?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function Markdown({ tokens, components, allowHtml }: MarkdownProps): import("react").FunctionComponentElement<{
|
|
15
|
+
children?: ReactNode | undefined;
|
|
16
|
+
}>;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/runtime.ts
|
|
3
|
+
import { createElement as h, Fragment } from "react";
|
|
4
|
+
var TAGS = [
|
|
5
|
+
"h1",
|
|
6
|
+
"h2",
|
|
7
|
+
"h3",
|
|
8
|
+
"h4",
|
|
9
|
+
"h5",
|
|
10
|
+
"h6",
|
|
11
|
+
"p",
|
|
12
|
+
"a",
|
|
13
|
+
"img",
|
|
14
|
+
"pre",
|
|
15
|
+
"code",
|
|
16
|
+
"strong",
|
|
17
|
+
"em",
|
|
18
|
+
"del",
|
|
19
|
+
"br",
|
|
20
|
+
"ul",
|
|
21
|
+
"ol",
|
|
22
|
+
"li",
|
|
23
|
+
"input",
|
|
24
|
+
"blockquote",
|
|
25
|
+
"hr",
|
|
26
|
+
"table",
|
|
27
|
+
"thead",
|
|
28
|
+
"tbody",
|
|
29
|
+
"tr",
|
|
30
|
+
"th",
|
|
31
|
+
"td"
|
|
32
|
+
];
|
|
33
|
+
function CodeBlock({ lang, code }) {
|
|
34
|
+
return h("pre", { "data-lang": lang || undefined }, h("code", null, code));
|
|
35
|
+
}
|
|
36
|
+
var defaults = Object.fromEntries(TAGS.map((t) => [t, t]));
|
|
37
|
+
defaults.pre = CodeBlock;
|
|
38
|
+
var ENTITIES = {
|
|
39
|
+
"&": "&",
|
|
40
|
+
"<": "<",
|
|
41
|
+
">": ">",
|
|
42
|
+
""": '"',
|
|
43
|
+
"'": "'"
|
|
44
|
+
};
|
|
45
|
+
var decode = (s) => s.replace(/&(?:amp|lt|gt|quot|#39);/g, (m) => ENTITIES[m]);
|
|
46
|
+
function createRenderer(C, allowHtml) {
|
|
47
|
+
const tag = (name) => C[name] ?? name;
|
|
48
|
+
const inline = (tokens = []) => tokens.map((t, i) => inlineNode(t, i));
|
|
49
|
+
function inlineNode(t, key) {
|
|
50
|
+
switch (t.type) {
|
|
51
|
+
case "strong":
|
|
52
|
+
return h(tag("strong"), { key }, ...inline(t.tokens));
|
|
53
|
+
case "em":
|
|
54
|
+
return h(tag("em"), { key }, ...inline(t.tokens));
|
|
55
|
+
case "del":
|
|
56
|
+
return h(tag("del"), { key }, ...inline(t.tokens));
|
|
57
|
+
case "codespan":
|
|
58
|
+
return h(tag("code"), { key }, decode(t.text));
|
|
59
|
+
case "br":
|
|
60
|
+
return h(tag("br"), { key });
|
|
61
|
+
case "link":
|
|
62
|
+
return h(tag("a"), { key, href: t.href, title: t.title || undefined }, ...inline(t.tokens));
|
|
63
|
+
case "image":
|
|
64
|
+
return h(tag("img"), { key, src: t.href, alt: t.text, title: t.title || undefined });
|
|
65
|
+
case "html":
|
|
66
|
+
return allowHtml ? h("span", { key, dangerouslySetInnerHTML: { __html: t.text } }) : h(Fragment, { key }, t.text);
|
|
67
|
+
default:
|
|
68
|
+
return t.tokens ? h(Fragment, { key }, ...inline(t.tokens)) : h(Fragment, { key }, decode(t.text ?? ""));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const blocks = (tokens = []) => tokens.map((t, i) => blockNode(t, i));
|
|
72
|
+
function blockNode(t, key) {
|
|
73
|
+
switch (t.type) {
|
|
74
|
+
case "space":
|
|
75
|
+
return null;
|
|
76
|
+
case "heading":
|
|
77
|
+
return h(tag(`h${t.depth}`), { key, id: t.id }, ...inline(t.tokens));
|
|
78
|
+
case "paragraph":
|
|
79
|
+
return h(tag("p"), { key }, ...inline(t.tokens));
|
|
80
|
+
case "code":
|
|
81
|
+
return h(tag("pre"), { key, lang: t.lang || undefined, code: t.text });
|
|
82
|
+
case "blockquote":
|
|
83
|
+
return h(tag("blockquote"), { key }, ...blocks(t.tokens));
|
|
84
|
+
case "hr":
|
|
85
|
+
return h(tag("hr"), { key });
|
|
86
|
+
case "list":
|
|
87
|
+
return list(t, key);
|
|
88
|
+
case "table":
|
|
89
|
+
return table(t, key);
|
|
90
|
+
case "html":
|
|
91
|
+
return allowHtml ? h("div", { key, dangerouslySetInnerHTML: { __html: t.text } }) : h(tag("p"), { key }, t.text);
|
|
92
|
+
case "text":
|
|
93
|
+
return h(Fragment, { key }, ...t.tokens ? inline(t.tokens) : [decode(t.text)]);
|
|
94
|
+
default:
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function list(t, key) {
|
|
99
|
+
const ordered = !!t.ordered;
|
|
100
|
+
const items = t.items.map((item, i) => h(tag("li"), { key: i }, ...listItem(item)));
|
|
101
|
+
return h(tag(ordered ? "ol" : "ul"), { key, start: ordered && t.start !== 1 ? t.start : undefined }, ...items);
|
|
102
|
+
}
|
|
103
|
+
function listItem(item) {
|
|
104
|
+
const out = [];
|
|
105
|
+
if (item.task) {
|
|
106
|
+
out.push(h(tag("input"), { key: "task", type: "checkbox", checked: !!item.checked, disabled: true, readOnly: true }));
|
|
107
|
+
}
|
|
108
|
+
item.tokens.forEach((t, i) => out.push(blockNode(t, i)));
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
function table(t, key) {
|
|
112
|
+
const cell = (c, i, name) => h(tag(name), { key: i, style: c.align ? { textAlign: c.align } : undefined }, ...inline(c.tokens));
|
|
113
|
+
return h(tag("table"), { key }, h(tag("thead"), { key: "head" }, h(tag("tr"), { key: "row" }, ...t.header.map((c, i) => cell(c, i, "th")))), h(tag("tbody"), { key: "body" }, ...t.rows.map((row, r) => h(tag("tr"), { key: r }, ...row.map((c, i) => cell(c, i, "td"))))));
|
|
114
|
+
}
|
|
115
|
+
return { blocks };
|
|
116
|
+
}
|
|
117
|
+
function Markdown({ tokens, components, allowHtml = false }) {
|
|
118
|
+
const renderer = createRenderer({ ...defaults, ...components }, allowHtml);
|
|
119
|
+
return h(Fragment, null, ...renderer.blocks(tokens));
|
|
120
|
+
}
|
|
121
|
+
export {
|
|
122
|
+
Markdown
|
|
123
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@kiln-js/md",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Markdown pages for Kiln with customizable tag components",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./runtime": {
|
|
13
|
+
"types": "./dist/runtime.d.ts",
|
|
14
|
+
"import": "./dist/runtime.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"kiln",
|
|
23
|
+
"markdown",
|
|
24
|
+
"mdx",
|
|
25
|
+
"react",
|
|
26
|
+
"ssr"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "bun build ./src/index.ts ./src/runtime.ts --outdir ./dist --target bun --packages external && tsc -p tsconfig.json --emitDeclarationOnly",
|
|
33
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
34
|
+
"prepublishOnly": "bun run build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@kiln-js/vite-plugin": "*",
|
|
38
|
+
"marked": "^14.1.2"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=18",
|
|
42
|
+
"vite": ">=5"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.0.0",
|
|
46
|
+
"@types/react": "^18.3.0",
|
|
47
|
+
"react": "^18.3.1",
|
|
48
|
+
"vite": "5.4.21"
|
|
49
|
+
}
|
|
6
50
|
}
|
package/README.md
DELETED