Skip to content

Commit f7f1f0a

Browse files
committed
refactor: join relative paths
1 parent db9409c commit f7f1f0a

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

lib/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'pathe'
12
import postcss from 'postcss'
23
import isUrl from 'is-url-superb'
34
import { defu as merge } from 'defu'
@@ -148,13 +149,18 @@ const plugin = (options = {}) => tree => {
148149
}
149150

150151
const handleSingleValueAttributes = (node, attribute, value) => {
152+
// Don't prepend to URLs
151153
if (isUrl(node.attrs[attribute])) {
152154
return node
153155
}
154156

155157
node.attrs[attribute] = typeof value === 'boolean'
156-
? options.url + node.attrs[attribute]
157-
: value + node.attrs[attribute]
158+
? isUrl(options.url)
159+
? options.url + node.attrs[attribute]
160+
: path.join(options.url, node.attrs[attribute])
161+
: isUrl(value)
162+
? value + node.attrs[attribute]
163+
: path.join(value, node.attrs[attribute])
158164
}
159165

160166
const prependUrl = (value, url) => {

package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
"node": ">=18"
2525
},
2626
"scripts": {
27-
"dev": "vitest",
2827
"build": "unbuild",
2928
"release": "npx np",
3029
"prepack": "unbuild",
3130
"pretest": "npm run lint",
3231
"lint": "biome lint ./lib",
32+
"dev": "vitest --coverage",
3333
"test": "vitest run --coverage"
3434
},
3535
"keywords": [
@@ -48,6 +48,7 @@
4848
"dependencies": {
4949
"defu": "^6.1.4",
5050
"is-url-superb": "^6.1.0",
51+
"pathe": "^1.1.2",
5152
"postcss": "^8.4.35",
5253
"postcss-safe-parser": "^7.0.0",
5354
"srcset": "^5.0.1"

test/expected/joins.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script src="assets/test.js"></script>
2+
<img src="images/test.jpg">
3+
<img src="test.jpg">
4+
<img src="../test.jpg">

test/fixtures/joins.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script src="test.js"></script>
2+
<img src="test.jpg">
3+
<img src="../test.jpg">
4+
<img src="../../test.jpg">

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,17 @@ test('user-defined tags (array)', t => {
112112
tags: ['img', 'script'],
113113
})
114114
})
115+
116+
test('joins relative paths', t => {
117+
return process(t, 'joins', {
118+
url: 'images',
119+
tags: {
120+
img: {
121+
src: true,
122+
},
123+
script: {
124+
src: 'assets',
125+
},
126+
},
127+
})
128+
})

0 commit comments

Comments
 (0)