-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (160 loc) · 4.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import path from 'pathe'
import postcss from 'postcss'
import isUrl from 'is-url-superb'
import { defu as merge } from 'defu'
import safe from 'postcss-safe-parser'
import { walk } from 'posthtml/lib/api.js'
import { parseSrcset, stringifySrcset } from 'srcset'
import postcssBaseUrl from './plugins/postcss-baseurl.js'
const defaultTags = {
a: {
href: true,
},
area: {
href: true,
},
audio: {
src: true,
},
base: {
href: true,
},
body: {
background: true,
},
embed: {
src: true,
},
iframe: {
src: true,
},
img: {
src: true,
srcset: true,
},
input: {
src: true,
},
link: {
href: true,
},
script: {
src: true,
},
source: {
src: true,
srcset: true,
},
table: {
background: true,
},
td: {
background: true,
},
th: {
background: true,
},
track: {
src: true,
},
video: {
poster: true,
},
}
const plugin = (options = {}) => tree => {
options.url = options.url || ''
options.attributes = options.attributes || {}
options.allTags = options.allTags || false
options.styleTag = options.styleTag || false
options.inlineCss = options.inlineCss || false
options.tags = options.allTags ? merge(options.tags, defaultTags) : options.tags || {}
tree.walk = tree.walk || walk
const process = node => {
// Skip if `url` was not provided
if (options.url && (typeof options.url !== 'string' || options.url === '')) {
return node
}
// Skip if `options.tags` is not an array or object
if (!['array', 'object'].includes(typeof options.tags)) {
return node
}
// Handle embedded stylesheets
if (node.tag === 'style' && node.content && options.styleTag) {
node.content = postcss([
postcssBaseUrl({
base: options.url,
})
])
.process(node.content.join('').trim())
.css
}
// Handle style attribute
if (node.attrs?.style && options.inlineCss) {
node.attrs.style = prependUrl(node.attrs.style, options.url)
}
// Handle defined HTML attributes
for (const [attribute, value] of Object.entries(options.attributes)) {
if (node.attrs?.[attribute]) {
handleSingleValueAttributes(node, attribute, value)
}
}
// Handle defined HTML tags
const tags = Array.isArray(options.tags) ?
Object.entries(defaultTags).filter(([tag]) => options.tags.includes(tag)) :
Object.entries(options.tags)
// biome-ignore lint: fails with for...of
tags.forEach(([tag, attributes]) => {
if (node.tag !== tag) {
return node
}
// Handle tag attributes
for (const [attribute, value] of Object.entries(attributes)) {
if (node.attrs?.[attribute]) {
// Handle "single-value" attributes
if (['href', 'src', 'poster', 'background'].includes(attribute)) {
handleSingleValueAttributes(node, attribute, value)
}
// Handle `srcset` attribute
if (attribute === 'srcset') {
const parsed = parseSrcset(node.attrs[attribute])
parsed.map(p => {
if (!isUrl(p.url)) {
p.url = typeof value === 'boolean'
? isUrl(options.url)
? options.url + p.url
: path.join(options.url, p.url)
: isUrl(value)
? value + p.url
: path.join(value, p.url)
}
return p
})
node.attrs[attribute] = stringifySrcset(parsed)
}
}
}
})
// Return the node
return node
}
const handleSingleValueAttributes = (node, attribute, value) => {
// Don't prepend to URLs
if (typeof node.attrs[attribute] === 'string' && isUrl(node.attrs[attribute])) {
return node
}
node.attrs[attribute] = typeof value === 'boolean'
? isUrl(options.url)
? options.url + node.attrs[attribute]
: path.join(options.url, node.attrs[attribute])
: isUrl(value)
? value + node.attrs[attribute]
: path.join(value, node.attrs[attribute])
}
const prependUrl = (value, url) => {
const { css } = postcss([postcssBaseUrl({ base: url })])
.process(`div { ${value} }`, { parser: safe })
return css.replace(/div {\s|\s}$/gm, '')
}
return tree.walk(process)
}
export default plugin