Files
hotdog-cms/internal/site/sourcelinks.go
T
jcoffey-dev eefa8ac0a8
ci / editor-ui (pull_request) Successful in 28s
ci / go (pull_request) Successful in 1m37s
Links to a page's source file go to the page
A link written to another page's Markdown file, as MkDocs sites and most
editors write them (linux.md, ../install/linux.md#docker, /index.md), now
goes to that page's address, with its #fragment kept. Paths are relative to
the linking page's own source, as on disk; a path starting with / is from
content/. A section's index.md or README.md finds its _index.md.

A link to a .md file no page came from is left as written, so the link check
reports it as broken; addresses with a scheme, and paths that climb out of
content/, are left alone.
2026-10-12 20:02:41 -07:00

60 lines
1.8 KiB
Go

package site
import (
"html/template"
"path"
"regexp"
"strings"
)
// srcLinkRe finds links written to a page's source file, the way MkDocs
// sites and most Markdown editors write them: about.md,
// ../install/linux.md#docker. Only relative paths: an address with a scheme
// is somewhere else.
var srcLinkRe = regexp.MustCompile(`(<a\s[^>]*?href=")([^"#?:]+\.md)((?:#[^"]*)?)"`)
// resolveSourceLinks turns each link to another page's source file into that
// page's address, keeping any #fragment. Paths are relative to the linking
// page's own source, as on disk; a path starting with / is from content/. A
// section's index.md also finds its _index.md, MkDocs' name for what HotDog
// calls _index.md. A link to a .md file no page came from is left as written,
// for the link check to report.
func (s *Site) resolveSourceLinks() {
bySource := map[string]*Page{}
for _, p := range s.Pages {
if p.Source != "" {
bySource[p.Source] = p
}
}
for _, p := range s.Pages {
if p.Source == "" || !strings.Contains(string(p.Content), ".md") {
continue
}
dir := path.Dir(p.Source)
p.Content = template.HTML(srcLinkRe.ReplaceAllStringFunc(string(p.Content), func(m string) string {
sub := srcLinkRe.FindStringSubmatch(m)
target := sub[2]
var rel string
if strings.HasPrefix(target, "/") {
rel = path.Clean(strings.TrimPrefix(target, "/"))
} else {
rel = path.Clean(path.Join(dir, target))
}
if strings.HasPrefix(rel, "../") || rel == ".." {
return m
}
to := bySource[rel]
if to == nil && path.Base(rel) == "index.md" {
to = bySource[path.Join(path.Dir(rel), "_index.md")]
}
if to == nil && path.Base(rel) == "README.md" {
to = bySource[path.Join(path.Dir(rel), "_index.md")]
}
if to == nil {
return m
}
return sub[1] + to.Path + sub[3] + `"`
}))
}
}