@kiln-js/md 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ import type { KilnPlugin } from "@kiln-js/vite-plugin";
2
+ import { type MdOptions } from "./plugin";
3
+ export type { MdOptions } from "./plugin";
4
+ export declare function md(options?: MdOptions): KilnPlugin;
5
+ export default md;
package/dist/index.js ADDED
@@ -0,0 +1,100 @@
1
+ // @bun
2
+ // src/plugin.ts
3
+ import path from "path";
4
+
5
+ // src/parse.ts
6
+ import { marked } from "marked";
7
+ function splitFrontmatter(src) {
8
+ const m = src.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?/);
9
+ if (!m)
10
+ return { data: {}, body: src };
11
+ const data = {};
12
+ for (const line of m[1].split(/\r?\n/)) {
13
+ const kv = line.match(/^([\w-]+):\s*(.*)$/);
14
+ if (kv)
15
+ data[kv[1]] = kv[2].replace(/^(["'])(.*)\1$/, "$2");
16
+ }
17
+ return { data, body: src.slice(m[0].length) };
18
+ }
19
+ function slugify(text) {
20
+ return text.toLowerCase().trim().replace(/[^\p{L}\p{N}\s-]/gu, "").replace(/\s+/g, "-");
21
+ }
22
+ var plain = (s) => s.replace(/[*_`~[\]]/g, "");
23
+ function strip(t) {
24
+ const { raw: _raw, ...rest } = t;
25
+ for (const key of ["tokens", "items"]) {
26
+ if (Array.isArray(rest[key]))
27
+ rest[key] = rest[key].map(strip);
28
+ }
29
+ if (Array.isArray(rest.header))
30
+ rest.header = rest.header.map(strip);
31
+ if (Array.isArray(rest.rows))
32
+ rest.rows = rest.rows.map((row) => row.map(strip));
33
+ return rest;
34
+ }
35
+ function parseMarkdown(src) {
36
+ const { data, body } = splitFrontmatter(src);
37
+ const lexed = marked.lexer(body);
38
+ const seen = new Map;
39
+ const headings = [];
40
+ for (const t of lexed) {
41
+ if (t.type !== "heading")
42
+ continue;
43
+ const text = plain(t.text);
44
+ const base = slugify(text) || "section";
45
+ const n = seen.get(base) ?? 0;
46
+ seen.set(base, n + 1);
47
+ t.id = n ? `${base}-${n}` : base;
48
+ headings.push({ depth: t.depth, text, id: t.id });
49
+ }
50
+ return { frontmatter: data, tokens: lexed.map(strip), headings };
51
+ }
52
+
53
+ // src/plugin.ts
54
+ var norm = (p) => p.replace(/\\/g, "/");
55
+ function generateModule(src, opts) {
56
+ const { frontmatter, tokens, headings } = parseMarkdown(src);
57
+ return [
58
+ `import { createElement as h, Fragment } from "react";`,
59
+ `import { Title } from "@kiln-js/core";`,
60
+ `import { Markdown } from "@kiln-js/md/runtime";`,
61
+ opts.components ? `import __mdComponents from ${JSON.stringify(opts.components)};` : `const __mdComponents = undefined;`,
62
+ `export const frontmatter = ${JSON.stringify(frontmatter)};`,
63
+ `export const headings = ${JSON.stringify(headings)};`,
64
+ `const tokens = ${JSON.stringify(tokens)};`,
65
+ `export default function MarkdownPage() {`,
66
+ ` return h(Fragment, null,`,
67
+ ` frontmatter.title ? h(Title, null, frontmatter.title) : null,`,
68
+ ` h(Markdown, { tokens, components: __mdComponents, allowHtml: ${opts.html} }));`,
69
+ `}`
70
+ ].join(`
71
+ `);
72
+ }
73
+ function mdPlugin(opts = {}) {
74
+ const pagesRoot = norm(path.resolve(process.cwd(), opts.dir ?? "app/pages")) + "/";
75
+ const components = opts.components ? norm(path.resolve(process.cwd(), opts.components)) : undefined;
76
+ const html = opts.html ?? false;
77
+ return {
78
+ name: "kiln:md",
79
+ enforce: "pre",
80
+ transform(code, rawId) {
81
+ const id = rawId.split("?")[0];
82
+ if (!id.endsWith(".md") || !norm(id).startsWith(pagesRoot))
83
+ return;
84
+ return { code: generateModule(code, { components, html }), map: null };
85
+ }
86
+ };
87
+ }
88
+
89
+ // src/index.ts
90
+ function md(options = {}) {
91
+ return {
92
+ name: "@kiln-js/md",
93
+ vite: () => [mdPlugin(options)]
94
+ };
95
+ }
96
+ var src_default = md;
97
+ export {
98
+ src_default as default,
99
+ md
100
+ };
@@ -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 {};
@@ -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. */
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
+ }>;
@@ -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
+ "&amp;": "&",
40
+ "&lt;": "<",
41
+ "&gt;": ">",
42
+ "&quot;": '"',
43
+ "&#39;": "'"
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
- "name": "@kiln-js/md",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
1
+ {
2
+ "name": "@kiln-js/md",
3
+ "version": "0.1.0",
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
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.