orga-build 0.2.4 → 0.2.6

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.
@@ -1 +1 @@
1
- {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["build.js"],"names":[],"mappings":"AAoBA;;GAEG;AACH,qEAFW,OAAO,aAAa,EAAE,MAAM,iBAgKtC;AAvKD;;;;EAIC"}
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,14 +1,13 @@
1
1
  import path from 'node:path'
2
2
  import { createRequire } from 'node:module'
3
3
  import { build as viteBuild } from 'vite'
4
- import orga from '@orgajs/rollup'
4
+ import { setupOrga } from './orga.js'
5
5
  import react from '@vitejs/plugin-react'
6
6
  import { fileURLToPath, pathToFileURL } from 'node:url'
7
7
  import { copy, emptyDir, ensureDir } from './fs.js'
8
8
  import { pluginFactory } from './vite.js'
9
9
  import fs from 'fs/promises'
10
10
  import assert from 'node:assert'
11
- import { rehypeWrap } from './plugins.js'
12
11
 
13
12
  const require = createRequire(import.meta.url)
14
13
 
@@ -33,9 +32,7 @@ export async function build({
33
32
  const clientOutDir = path.join(outDir, '.client')
34
33
 
35
34
  const plugins = [
36
- orga({
37
- rehypePlugins: [[rehypeWrap, { className: containerClass }]]
38
- }),
35
+ setupOrga({ containerClass }),
39
36
  react(),
40
37
  pluginFactory({ dir: root }),
41
38
  ...vitePlugins
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,mCAaA"}
package/lib/orga.js ADDED
@@ -0,0 +1,90 @@
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
+ if (link.path.protocol === 'file') {
17
+ return link.path.value.replace(/\.org$/, '')
18
+ }
19
+ return link.path.value
20
+ }
21
+ }
22
+ })
23
+ }
24
+
25
+ // --- plugins ---
26
+
27
+ /**
28
+ * @param {Object} options
29
+ * @param {string[]} options.className
30
+ */
31
+ function rehypeWrap({ className = [] }) {
32
+ /**
33
+ * Transform.
34
+ *
35
+ * @param {HastTree} tree
36
+ * Tree.
37
+ * @returns {HastTree}
38
+ * Nothing.
39
+ */
40
+ return (tree) => {
41
+ return {
42
+ ...tree,
43
+ children: [
44
+ {
45
+ type: 'element',
46
+ tagName: 'div',
47
+ properties: {
48
+ className
49
+ },
50
+ // @ts-ignore
51
+ children: tree.children
52
+ }
53
+ ]
54
+ }
55
+ }
56
+ }
57
+
58
+ function image() {
59
+ /**
60
+ * @param {any} tree
61
+ */
62
+ return function (tree) {
63
+ /** @type {Record<string, string>} */
64
+ const imports = {}
65
+ visitParents(tree, { tagName: 'img' }, (node) => {
66
+ node.type = 'jsx'
67
+ const { src, target } = node.properties
68
+ if (typeof src !== 'string') return
69
+ if (src.startsWith('http')) {
70
+ return
71
+ }
72
+ const name = (imports[src] ??= `asset_${genId()}`)
73
+ node.value = `<img src={${name}} target='${target}'/>`
74
+ })
75
+
76
+ for (const [src, name] of Object.entries(imports)) {
77
+ tree.children.unshift({
78
+ type: 'jsx',
79
+ value: `import ${name} from '${src}'`,
80
+ children: []
81
+ })
82
+ }
83
+ }
84
+ }
85
+
86
+ function genId(length = 8) {
87
+ const array = new Uint8Array(length)
88
+ crypto.getRandomValues(array)
89
+ return Array.from(array, (byte) => (byte % 36).toString(36)).join('')
90
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["serve.js"],"names":[],"mappings":"AASA;;;GAGG;AACH,8BAHW,OAAO,aAAa,EAAE,MAAM,SAC5B,MAAM,iBA6ChB"}
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,11 +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'
8
6
  import { alias } from './build.js'
7
+ import { setupOrga } from './orga.js'
9
8
 
10
9
  /**
11
10
  * @param {import('./config.js').Config} config
@@ -15,9 +14,7 @@ export async function serve(config, port = 3000) {
15
14
  const app = express()
16
15
  const vite = await createServer({
17
16
  plugins: [
18
- orga({
19
- rehypePlugins: [[rehypeWrap, { className: config.containerClass }]]
20
- }),
17
+ setupOrga({ containerClass: config.containerClass }),
21
18
  react(),
22
19
  pluginFactory({ dir: config.root }),
23
20
  ...config.vitePlugins
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orga-build",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
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
- }