orga-build 0.5.3 → 0.5.4
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/orga.js +32 -29
- package/package.json +1 -1
package/lib/orga.js
CHANGED
|
@@ -16,7 +16,7 @@ export function setupOrga({ containerClass, root }) {
|
|
|
16
16
|
rehypePlugins: [
|
|
17
17
|
[rehypeWrap, { className: containerClass }],
|
|
18
18
|
[rewriteOrgFileLinks, { root }],
|
|
19
|
-
|
|
19
|
+
mediaAssets
|
|
20
20
|
],
|
|
21
21
|
reorgRehypeOptions: {
|
|
22
22
|
linkHref: (link) => link.path.value
|
|
@@ -26,6 +26,37 @@ export function setupOrga({ containerClass, root }) {
|
|
|
26
26
|
|
|
27
27
|
// --- plugins ---
|
|
28
28
|
|
|
29
|
+
function mediaAssets() {
|
|
30
|
+
/**
|
|
31
|
+
* @param {any} tree
|
|
32
|
+
*/
|
|
33
|
+
return function (tree) {
|
|
34
|
+
/** @type {Record<string, string>} */
|
|
35
|
+
const imports = {}
|
|
36
|
+
visitParents(tree, [{ tagName: 'img' }, { tagName: 'video' }], (node) => {
|
|
37
|
+
node.type = 'jsx'
|
|
38
|
+
const { src, ...rest } = node.properties
|
|
39
|
+
if (typeof src !== 'string') return
|
|
40
|
+
if (src.startsWith('http')) return
|
|
41
|
+
const tagName = node.tagName
|
|
42
|
+
const name = (imports[src] ??= `asset_${genId()}`)
|
|
43
|
+
const attrs = Object.entries(rest)
|
|
44
|
+
.filter(([, v]) => v !== undefined && v !== false)
|
|
45
|
+
.map(([k, v]) => (v === true ? k : `${k}='${v}'`))
|
|
46
|
+
.join(' ')
|
|
47
|
+
node.value = `<${tagName} src={${name}}${attrs ? ` ${attrs}` : ''}/>`
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
for (const [src, name] of Object.entries(imports)) {
|
|
51
|
+
tree.children.unshift({
|
|
52
|
+
type: 'jsx',
|
|
53
|
+
value: `import ${name} from '${src}'`,
|
|
54
|
+
children: []
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
29
60
|
/**
|
|
30
61
|
* @param {Object} options
|
|
31
62
|
* @param {string[]} options.className
|
|
@@ -57,34 +88,6 @@ function rehypeWrap({ className = [] }) {
|
|
|
57
88
|
}
|
|
58
89
|
}
|
|
59
90
|
|
|
60
|
-
function image() {
|
|
61
|
-
/**
|
|
62
|
-
* @param {any} tree
|
|
63
|
-
*/
|
|
64
|
-
return function (tree) {
|
|
65
|
-
/** @type {Record<string, string>} */
|
|
66
|
-
const imports = {}
|
|
67
|
-
visitParents(tree, { tagName: 'img' }, (node) => {
|
|
68
|
-
node.type = 'jsx'
|
|
69
|
-
const { src, target } = node.properties
|
|
70
|
-
if (typeof src !== 'string') return
|
|
71
|
-
if (src.startsWith('http')) {
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
const name = (imports[src] ??= `asset_${genId()}`)
|
|
75
|
-
node.value = `<img src={${name}} target='${target}'/>`
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
for (const [src, name] of Object.entries(imports)) {
|
|
79
|
-
tree.children.unshift({
|
|
80
|
-
type: 'jsx',
|
|
81
|
-
value: `import ${name} from '${src}'`,
|
|
82
|
-
children: []
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
91
|
/**
|
|
89
92
|
* @param {Object} options
|
|
90
93
|
* @param {string} options.root
|