boxwood 0.77.0 → 0.78.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.
- package/README.md +10 -5
- package/index.js +40 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,6 +48,8 @@ const html = template({ foo: "bar" })
|
|
|
48
48
|
console.log(html)
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
You can use [express-boxwood](https://www.npmjs.com/package/express-boxwood) for [express](https://www.npmjs.com/package/express).
|
|
52
|
+
|
|
51
53
|
## Syntax
|
|
52
54
|
|
|
53
55
|
```js
|
|
@@ -67,17 +69,20 @@ module.exports = () => {
|
|
|
67
69
|
|
|
68
70
|
```js
|
|
69
71
|
// example/layout/index.js
|
|
70
|
-
const { component, css, html, body } = require("boxwood")
|
|
72
|
+
const { component, css, doctype, html, body } = require("boxwood")
|
|
71
73
|
const head = require("./head")
|
|
72
74
|
|
|
73
75
|
const styles = css.load(__dirname)
|
|
74
76
|
|
|
75
77
|
module.exports = component(
|
|
76
78
|
({ language }, children) => {
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
return [
|
|
80
|
+
doctype(),
|
|
81
|
+
html({ lang: language }, [
|
|
82
|
+
head(),
|
|
83
|
+
body({ className: styles.layout }, children),
|
|
84
|
+
]),
|
|
85
|
+
]
|
|
81
86
|
},
|
|
82
87
|
{ styles }
|
|
83
88
|
)
|
package/index.js
CHANGED
|
@@ -10,7 +10,10 @@ async function compile(path) {
|
|
|
10
10
|
const tree = fn(...arguments)
|
|
11
11
|
const nodes = {}
|
|
12
12
|
const styles = []
|
|
13
|
-
const scripts =
|
|
13
|
+
const scripts = {
|
|
14
|
+
head: [],
|
|
15
|
+
body: [],
|
|
16
|
+
}
|
|
14
17
|
const walk = (node) => {
|
|
15
18
|
if (!node) {
|
|
16
19
|
return
|
|
@@ -29,16 +32,27 @@ async function compile(path) {
|
|
|
29
32
|
node.ignore = true
|
|
30
33
|
}
|
|
31
34
|
if (node.name === "script") {
|
|
32
|
-
const
|
|
35
|
+
const attributes = node.attributes || {}
|
|
33
36
|
if (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
attributes.src ||
|
|
38
|
+
["application/json", "application/ld+json"].includes(
|
|
39
|
+
attributes.type
|
|
40
|
+
)
|
|
37
41
|
) {
|
|
38
42
|
node.ignore = false
|
|
43
|
+
return
|
|
39
44
|
} else {
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
const script = node.children
|
|
46
|
+
if (script) {
|
|
47
|
+
if (attributes.target === "head") {
|
|
48
|
+
if (!scripts.head.includes(script)) {
|
|
49
|
+
scripts.head.push(script)
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
if (!scripts.body.includes(script)) {
|
|
53
|
+
scripts.body.push(script)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
42
56
|
}
|
|
43
57
|
node.ignore = true
|
|
44
58
|
}
|
|
@@ -57,12 +71,18 @@ async function compile(path) {
|
|
|
57
71
|
children: styles.join(""),
|
|
58
72
|
})
|
|
59
73
|
}
|
|
74
|
+
if (scripts.head.length > 0) {
|
|
75
|
+
nodes.head.children.push({
|
|
76
|
+
name: "script",
|
|
77
|
+
children: scripts.head.join(""),
|
|
78
|
+
})
|
|
79
|
+
}
|
|
60
80
|
}
|
|
61
81
|
if (nodes.body) {
|
|
62
|
-
if (scripts.length > 0) {
|
|
82
|
+
if (scripts.body.length > 0) {
|
|
63
83
|
nodes.body.children.push({
|
|
64
84
|
name: "script",
|
|
65
|
-
children: scripts.join(""),
|
|
85
|
+
children: scripts.body.join(""),
|
|
66
86
|
})
|
|
67
87
|
}
|
|
68
88
|
}
|
|
@@ -135,7 +155,12 @@ const attributes = (options) => {
|
|
|
135
155
|
const result = []
|
|
136
156
|
for (const key in options) {
|
|
137
157
|
const value = options[key]
|
|
138
|
-
if (
|
|
158
|
+
if (
|
|
159
|
+
typeof value === "string" ||
|
|
160
|
+
typeof value === "number" ||
|
|
161
|
+
value === true ||
|
|
162
|
+
Array.isArray(value)
|
|
163
|
+
) {
|
|
139
164
|
if (BOOLEAN_ATTRIBUTES.includes(key)) {
|
|
140
165
|
result.push(key)
|
|
141
166
|
} else {
|
|
@@ -309,10 +334,13 @@ js.load = function () {
|
|
|
309
334
|
const content = readFileSync(file, "utf8")
|
|
310
335
|
|
|
311
336
|
const options = arguments[arguments.length - 1]
|
|
337
|
+
const attributes = options.target ? { target: options.target } : {}
|
|
312
338
|
if (options && options.transform) {
|
|
313
|
-
return
|
|
339
|
+
return {
|
|
340
|
+
js: tag("script", attributes, options.transform(content)),
|
|
341
|
+
}
|
|
314
342
|
}
|
|
315
|
-
return js
|
|
343
|
+
return { js: tag("script", attributes, content) }
|
|
316
344
|
}
|
|
317
345
|
|
|
318
346
|
const node = (name) => (options, children) => tag(name, options, children)
|