@kiln-js/md 0.1.0 → 0.1.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 CHANGED
@@ -1,5 +1,5 @@
1
- import type { KilnPlugin } from "@kiln-js/vite-plugin";
1
+ import type { Plugin } from "vite";
2
2
  import { type MdOptions } from "./plugin";
3
3
  export type { MdOptions } from "./plugin";
4
- export declare function md(options?: MdOptions): KilnPlugin;
4
+ export declare function md(options?: MdOptions): Plugin;
5
5
  export default md;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // @bun
2
2
  // src/plugin.ts
3
+ import fs from "fs";
3
4
  import path from "path";
4
5
 
5
6
  // src/parse.ts
@@ -51,6 +52,7 @@ function parseMarkdown(src) {
51
52
  }
52
53
 
53
54
  // src/plugin.ts
55
+ var SUFFIX = ".kiln-md.tsx";
54
56
  var norm = (p) => p.replace(/\\/g, "/");
55
57
  function generateModule(src, opts) {
56
58
  const { frontmatter, tokens, headings } = parseMarkdown(src);
@@ -71,15 +73,36 @@ function generateModule(src, opts) {
71
73
  `);
72
74
  }
73
75
  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
76
  const html = opts.html ?? false;
77
+ let root = process.cwd();
78
+ let pagesRoot = "";
79
+ let components = "";
80
+ const isPage = (file) => norm(file).endsWith(".md") && norm(file).startsWith(pagesRoot);
77
81
  return {
78
82
  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
+ configResolved(config) {
84
+ root = config.root;
85
+ pagesRoot = norm(path.resolve(root, opts.dir ?? "app/pages")) + "/";
86
+ components = opts.components ? norm(path.resolve(root, opts.components)) : "";
87
+ },
88
+ resolveId(source, importer) {
89
+ if (!source.endsWith(".md"))
90
+ return;
91
+ const abs = path.isAbsolute(source) ? source : path.resolve(importer ? path.dirname(importer.split("?")[0]) : root, source);
92
+ if (isPage(abs))
93
+ return norm(abs) + SUFFIX;
94
+ },
95
+ load(id) {
96
+ if (!id.endsWith(SUFFIX))
97
+ return;
98
+ const mdPath = id.slice(0, -SUFFIX.length);
99
+ this.addWatchFile(mdPath);
100
+ const src = fs.readFileSync(mdPath, "utf8");
101
+ return { code: generateModule(src, { components, html }), map: null };
102
+ },
103
+ transform(code, id) {
104
+ const file = id.split("?")[0];
105
+ if (!isPage(file))
83
106
  return;
84
107
  return { code: generateModule(code, { components, html }), map: null };
85
108
  }
@@ -88,10 +111,7 @@ function mdPlugin(opts = {}) {
88
111
 
89
112
  // src/index.ts
90
113
  function md(options = {}) {
91
- return {
92
- name: "@kiln-js/md",
93
- vite: () => [mdPlugin(options)]
94
- };
114
+ return mdPlugin(options);
95
115
  }
96
116
  var src_default = md;
97
117
  export {
package/dist/plugin.d.ts CHANGED
@@ -4,7 +4,7 @@ export interface MdOptions {
4
4
  dir?: string;
5
5
  /** Module whose default export maps tag names to components. */
6
6
  components?: string;
7
- /** Allow raw HTML inside Markdown. Enable only for trusted content. Default: false */
7
+ /** Allow raw HTML inside Markdown. Default: false */
8
8
  html?: boolean;
9
9
  }
10
10
  export declare function generateModule(src: string, opts: {
package/package.json CHANGED
@@ -1,50 +1,49 @@
1
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
- }
2
+ "name": "@kiln-js/md",
3
+ "version": "0.1.2",
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 \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
33
+ "typecheck": "tsc -p tsconfig.json --noEmit",
34
+ "prepublishOnly": "bun run build"
35
+ },
36
+ "dependencies": {
37
+ "marked": "^14.1.2"
38
+ },
39
+ "peerDependencies": {
40
+ "react": "\u003e=18",
41
+ "vite": "\u003e=5"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "^22.0.0",
45
+ "@types/react": "^18.3.0",
46
+ "react": "^18.3.1",
47
+ "vite": "5.4.21"
48
+ }
50
49
  }