Sites → Previews builds a branch with Hugo (drafts included) and serves it under /preview/<site>/<branch>/, links rewritten to stay inside, behind a sandboxing CSP so a branch's scripts can't act on the app or phone home. The image now builds Hugo v0.170.1 from source; Alpine's 0.147.2 couldn't build the site. Hugo's errors show their innermost cause. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package sites
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Preview is a built copy of one branch of a site.
|
|
type Preview struct {
|
|
Branch string `json:"branch"`
|
|
Slug string `json:"slug"` // its part of the preview's address
|
|
Commit string `json:"commit"` // what was built
|
|
At time.Time `json:"at"`
|
|
Err string `json:"error,omitempty"`
|
|
}
|
|
|
|
var nonSlug = regexp.MustCompile(`[^a-z0-9]+`)
|
|
|
|
// Slug names a branch in a preview's address: lower case letters, digits
|
|
// and hyphens, different from the slugs already taken by other branches.
|
|
func Slug(branch string, taken []Preview) string {
|
|
base := strings.Trim(nonSlug.ReplaceAllString(strings.ToLower(branch), "-"), "-")
|
|
if base == "" {
|
|
base = "branch"
|
|
}
|
|
slug := base
|
|
for n := 2; ; n++ {
|
|
clash := false
|
|
for _, p := range taken {
|
|
if p.Slug == slug && p.Branch != branch {
|
|
clash = true
|
|
}
|
|
}
|
|
if !clash {
|
|
return slug
|
|
}
|
|
slug = base + "-" + strconv.Itoa(n)
|
|
}
|
|
}
|
|
|
|
var (
|
|
attrURL = regexp.MustCompile(`(\s(?:href|src|action|poster|data-src)=["']?)/([^/]|["'\s>]|$)`)
|
|
srcsetRe = regexp.MustCompile(`(\ssrcset=)(["'])([^"']*)(["'])`)
|
|
refreshRe = regexp.MustCompile(`(?i)(content=["']?\d+\s*;\s*url=)/([^/])`)
|
|
cssURL = regexp.MustCompile(`(url\(\s*["']?)/([^/])`)
|
|
)
|
|
|
|
// Rehome rewrites a site built for "/" to be served under prefix
|
|
// ("/preview/0/main/"): every address that starts at the site's root, in
|
|
// HTML attributes, srcsets, redirects and CSS, gets the prefix. Addresses
|
|
// to other sites are left alone.
|
|
func Rehome(dir, prefix string) error {
|
|
prefix = "/" + strings.Trim(prefix, "/") + "/"
|
|
return filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() {
|
|
return err
|
|
}
|
|
ext := strings.ToLower(filepath.Ext(p))
|
|
if ext != ".html" && ext != ".htm" && ext != ".css" {
|
|
return nil
|
|
}
|
|
raw, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s := RehomeText(string(raw), prefix, ext == ".css")
|
|
if s == string(raw) {
|
|
return nil
|
|
}
|
|
return os.WriteFile(p, []byte(s), 0o644)
|
|
})
|
|
}
|
|
|
|
// RehomeText does Rehome's rewriting on one file's text.
|
|
func RehomeText(s, prefix string, css bool) string {
|
|
s = cssURL.ReplaceAllString(s, "${1}"+prefix+"${2}")
|
|
if css {
|
|
return s
|
|
}
|
|
s = attrURL.ReplaceAllString(s, "${1}"+prefix+"${2}")
|
|
s = refreshRe.ReplaceAllString(s, "${1}"+prefix+"${2}")
|
|
return srcsetRe.ReplaceAllStringFunc(s, func(m string) string {
|
|
g := srcsetRe.FindStringSubmatch(m)
|
|
parts := strings.Split(g[3], ",")
|
|
for i, c := range parts {
|
|
t := strings.TrimLeft(c, " \t\n")
|
|
if strings.HasPrefix(t, "/") && !strings.HasPrefix(t, "//") {
|
|
parts[i] = c[:len(c)-len(t)] + prefix + t[1:]
|
|
}
|
|
}
|
|
return g[1] + g[2] + strings.Join(parts, ",") + g[4]
|
|
})
|
|
}
|