Skip to content

Commit 3b0b9f0

Browse files
committedApr 21, 2021
tasks/transpile/html: prefix local href and src html urls (/) with WEB_ROOT
1 parent 99d8db4 commit 3b0b9f0

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed
 

‎src/tasks/transpile/html.mjs

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { renderToString } from '@magic/hyperapp'
33
import deep from '@magic/deep'
44
import log from '@magic/log'
55

6+
import { replaceSlashSlash } from '../../lib/replaceSlashSlash.mjs'
7+
68
export default ({ app, root }) => {
79
app.state.root = root
810

@@ -13,7 +15,24 @@ export default ({ app, root }) => {
1315

1416
const view = app.View(page, app.hashes)
1517

16-
const rendered = renderToString(view(state))
18+
let rendered = renderToString(view(state))
19+
20+
// dirty url cleanup. makes all local urls in html files point to WEB_ROOT
21+
const tags = ['href', 'src']
22+
tags.map(tag => {
23+
const urlsTangle = rendered.split(`${tag}="`)
24+
25+
const urls = urlsTangle.map(url => {
26+
url = url.split('"')[0]
27+
if (url.startsWith('/') && !url.startsWith(root)) {
28+
return [url, replaceSlashSlash(`${root}/${url}`)]
29+
}
30+
}).filter(a => a)
31+
32+
urls.forEach(([oldUrl, newUrl]) => {
33+
rendered = rendered.replace(new RegExp(`${tag}="${oldUrl}"`, 'gim'), `${tag}="${newUrl}"`)
34+
})
35+
})
1736

1837
return {
1938
...page,

0 commit comments

Comments
 (0)
Please sign in to comment.