orga-build 0.2.3 → 0.2.5

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/lib/build.d.ts CHANGED
@@ -2,4 +2,9 @@
2
2
  * @param {import('./config.js').Config} config
3
3
  */
4
4
  export function build({ outDir, root, containerClass, vitePlugins }: import("./config.js").Config): Promise<void>;
5
+ export const alias: {
6
+ react: string;
7
+ 'react-dom': string;
8
+ wouter: string;
9
+ };
5
10
  //# sourceMappingURL=build.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["build.js"],"names":[],"mappings":"AAWA;;GAEG;AACH,qEAFW,OAAO,aAAa,EAAE,MAAM,iBA0JtC"}
1
+ {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["build.js"],"names":[],"mappings":"AAmBA;;GAEG;AACH,qEAFW,OAAO,aAAa,EAAE,MAAM,iBA8JtC;AArKD;;;;EAIC"}
package/lib/build.js CHANGED
@@ -1,13 +1,21 @@
1
1
  import path from 'node:path'
2
+ import { createRequire } from 'node:module'
2
3
  import { build as viteBuild } from 'vite'
3
- import orga from '@orgajs/rollup'
4
+ import { setupOrga } from './orga.js'
4
5
  import react from '@vitejs/plugin-react'
5
6
  import { fileURLToPath, pathToFileURL } from 'node:url'
6
7
  import { copy, emptyDir, ensureDir } from './fs.js'
7
8
  import { pluginFactory } from './vite.js'
8
9
  import fs from 'fs/promises'
9
10
  import assert from 'node:assert'
10
- import { rehypeWrap } from './plugins.js'
11
+
12
+ const require = createRequire(import.meta.url)
13
+
14
+ export const alias = {
15
+ react: path.dirname(require.resolve('react/package.json')),
16
+ 'react-dom': path.dirname(require.resolve('react-dom/package.json')),
17
+ wouter: path.dirname(require.resolve('wouter'))
18
+ }
11
19
 
12
20
  /**
13
21
  * @param {import('./config.js').Config} config
@@ -24,9 +32,7 @@ export async function build({
24
32
  const clientOutDir = path.join(outDir, '.client')
25
33
 
26
34
  const plugins = [
27
- orga({
28
- rehypePlugins: [[rehypeWrap, { className: containerClass }]]
29
- }),
35
+ setupOrga({ containerClass }),
30
36
  react(),
31
37
  pluginFactory({ dir: root }),
32
38
  ...vitePlugins
@@ -53,6 +59,9 @@ export async function build({
53
59
  },
54
60
  ssr: {
55
61
  noExternal: true
62
+ },
63
+ resolve: {
64
+ alias: alias
56
65
  }
57
66
  })
58
67
 
@@ -78,6 +87,9 @@ export async function build({
78
87
  },
79
88
  ssr: {
80
89
  noExternal: true
90
+ },
91
+ resolve: {
92
+ alias: alias
81
93
  }
82
94
  })
83
95
 
package/lib/orga.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @param {Object} options
3
+ * @param {string|string[]} options.containerClass - CSS class name(s) to wrap the rendered content
4
+ */
5
+ export function setupOrga({ containerClass }: {
6
+ containerClass: string | string[];
7
+ }): import("@orgajs/rollup").Plugin;
8
+ //# sourceMappingURL=orga.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orga.d.ts","sourceRoot":"","sources":["orga.js"],"names":[],"mappings":"AAMA;;;GAGG;AACH,8CAFG;IAAiC,cAAc,EAAvC,MAAM,GAAC,MAAM,EAAE;CACzB,mCAcA"}
package/lib/orga.js ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @import {Root as HastTree} from 'hast'
3
+ */
4
+ import _orga from '@orgajs/rollup'
5
+ import { visitParents } from 'unist-util-visit-parents'
6
+
7
+ /**
8
+ * @param {Object} options
9
+ * @param {string|string[]} options.containerClass - CSS class name(s) to wrap the rendered content
10
+ */
11
+ export function setupOrga({ containerClass }) {
12
+ return _orga({
13
+ rehypePlugins: [[rehypeWrap, { className: containerClass }], image],
14
+ reorgRehypeOptions: {
15
+ linkHref: (link) => {
16
+ console.log({ link })
17
+ if (link.path.protocol === 'file') {
18
+ return link.path.value.replace(/\.org$/, '')
19
+ }
20
+ return link.path.value
21
+ }
22
+ }
23
+ })
24
+ }
25
+
26
+ // --- plugins ---
27
+
28
+ /**
29
+ * @param {Object} options
30
+ * @param {string[]} options.className
31
+ */
32
+ function rehypeWrap({ className = [] }) {
33
+ /**
34
+ * Transform.
35
+ *
36
+ * @param {HastTree} tree
37
+ * Tree.
38
+ * @returns {HastTree}
39
+ * Nothing.
40
+ */
41
+ return (tree) => {
42
+ return {
43
+ ...tree,
44
+ children: [
45
+ {
46
+ type: 'element',
47
+ tagName: 'div',
48
+ properties: {
49
+ className
50
+ },
51
+ // @ts-ignore
52
+ children: tree.children
53
+ }
54
+ ]
55
+ }
56
+ }
57
+ }
58
+
59
+ function image() {
60
+ /**
61
+ * @param {any} tree
62
+ */
63
+ return function (tree) {
64
+ /** @type {Record<string, string>} */
65
+ const imports = {}
66
+ visitParents(tree, { tagName: 'img' }, (node) => {
67
+ node.type = 'jsx'
68
+ const { src, target } = node.properties
69
+ if (typeof src !== 'string') return
70
+ if (src.startsWith('http')) {
71
+ return
72
+ }
73
+ const name = (imports[src] ??= `asset_${genId()}`)
74
+ node.value = `<img src={${name}} target='${target}'/>`
75
+ })
76
+
77
+ for (const [src, name] of Object.entries(imports)) {
78
+ tree.children.unshift({
79
+ type: 'jsx',
80
+ value: `import ${name} from '${src}'`,
81
+ children: []
82
+ })
83
+ }
84
+ }
85
+ }
86
+
87
+ function genId(length = 8) {
88
+ const array = new Uint8Array(length)
89
+ crypto.getRandomValues(array)
90
+ return Array.from(array, (byte) => (byte % 36).toString(36)).join('')
91
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["serve.js"],"names":[],"mappings":"AAQA;;;GAGG;AACH,8BAHW,OAAO,aAAa,EAAE,MAAM,SAC5B,MAAM,iBA0ChB"}
1
+ {"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["serve.js"],"names":[],"mappings":"AAQA;;;GAGG;AACH,8BAHW,OAAO,aAAa,EAAE,MAAM,SAC5B,MAAM,iBA2ChB"}
package/lib/serve.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import express from 'express'
2
2
  import { createServer } from 'vite'
3
3
  import fs from 'node:fs/promises'
4
- import orga from '@orgajs/rollup'
5
4
  import react from '@vitejs/plugin-react'
6
5
  import { pluginFactory } from './vite.js'
7
- import { rehypeWrap } from './plugins.js'
6
+ import { alias } from './build.js'
7
+ import { setupOrga } from './orga.js'
8
8
 
9
9
  /**
10
10
  * @param {import('./config.js').Config} config
@@ -14,15 +14,16 @@ export async function serve(config, port = 3000) {
14
14
  const app = express()
15
15
  const vite = await createServer({
16
16
  plugins: [
17
- orga({
18
- rehypePlugins: [[rehypeWrap, { className: config.containerClass }]]
19
- }),
17
+ setupOrga({ containerClass: config.containerClass }),
20
18
  react(),
21
19
  pluginFactory({ dir: config.root }),
22
20
  ...config.vitePlugins
23
21
  ],
24
22
  server: { middlewareMode: true },
25
- appType: 'custom'
23
+ appType: 'custom',
24
+ resolve: {
25
+ alias: alias
26
+ }
26
27
  })
27
28
 
28
29
  app.use(vite.middlewares)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orga-build",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "A simple tool that builds org-mode files into a website",
5
5
  "type": "module",
6
6
  "bin": {
@@ -47,7 +47,10 @@
47
47
  "@types/hast": "^3.0.4",
48
48
  "@types/node": "^22.13.1",
49
49
  "@types/react": "^19.0.8",
50
- "@types/react-dom": "^19.0.3"
50
+ "@types/react-dom": "^19.0.3",
51
+ "orga": "^4.5.1"
51
52
  },
52
- "scripts": {}
53
+ "scripts": {
54
+ "clean": "fd . -e d.ts -e d.ts.map -I -x rm {}"
55
+ }
53
56
  }
package/lib/plugins.d.ts DELETED
@@ -1,12 +0,0 @@
1
- /**
2
- * @import {Root} from 'hast'
3
- */
4
- /**
5
- * @param {Object} options
6
- * @param {string[]} options.className
7
- */
8
- export function rehypeWrap({ className }: {
9
- className: string[];
10
- }): (tree: Root) => Root;
11
- import type { Root } from 'hast';
12
- //# sourceMappingURL=plugins.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugins.d.ts","sourceRoot":"","sources":["plugins.js"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,0CAFG;IAA0B,SAAS,EAA3B,MAAM,EAAE;CAClB,UAKW,IAAI,KAEF,IAAI,CAmBjB;0BAhCsB,MAAM"}
package/lib/plugins.js DELETED
@@ -1,34 +0,0 @@
1
- /**
2
- * @import {Root} from 'hast'
3
- */
4
-
5
- /**
6
- * @param {Object} options
7
- * @param {string[]} options.className
8
- */
9
- export function rehypeWrap({ className = [] }) {
10
- /**
11
- * Transform.
12
- *
13
- * @param {Root} tree
14
- * Tree.
15
- * @returns {Root}
16
- * Nothing.
17
- */
18
- return (tree) => {
19
- return {
20
- ...tree,
21
- children: [
22
- {
23
- type: 'element',
24
- tagName: 'div',
25
- properties: {
26
- className
27
- },
28
- // @ts-ignore
29
- children: tree.children
30
- }
31
- ]
32
- }
33
- }
34
- }