vitrine

display.go

// Package display is vitrine's rendering heart, shared with sibling tools:
// markdown-to-HTML conversion (with callouts, diagrams, and heading anchors),
// the page chrome template, and hostname-derived theming.
// It is pure presentation: no HTTP, no filesystem access.
package display

import (
	"bytes"
	_ "embed"
	"html/template"
	"io"
	"strings"

	"github.com/yuin/goldmark"
	gmext "github.com/yuin/goldmark/extension"
	"github.com/yuin/goldmark/parser"
	gmutil "github.com/yuin/goldmark/util"
)

// MaxRenderSize caps how much callers should read into memory for rendered views.
// Bigger files still work fine via raw views, which stream.
const MaxRenderSize = 4 << 20

var markdown = goldmark.New(
	goldmark.WithExtensions(gmext.GFM, callouts{}, diagrams{}),
	goldmark.WithParserOptions(
		parser.WithAutoHeadingID(),
		parser.WithASTTransformers(gmutil.Prioritized(headingAnchors{}, 100)),
	),
)

// Markdown converts markdown source to HTML.
func Markdown(src []byte) (template.HTML, error) {
	var buf bytes.Buffer
	// A fresh IDs registry per document keeps duplicate-heading suffixes deterministic.
	ctx := parser.NewContext(parser.WithIDs(newSlugIDs()))
	if err := markdown.Convert(src, &buf, parser.WithContext(ctx)); err != nil {
		return "", err
	}
	return template.HTML(buf.String()), nil
}

//go:embed style.css
var StyleCSS []byte

type DirEntry struct {
	Name string
	Href string
}

type Crumb struct {
	Name string
	Href string
}

type PageData struct {
	Title     string
	ThemeName string // host theme name, shown beside the site name in the nav
	Crumbs    []Crumb
	RawHref   string        // if set, a "raw" link is shown
	Listing   []DirEntry    // directory listing mode
	Content   template.HTML // rendered-markdown mode
	Iframe    string        // iframe-passthrough mode: src URL
}

// JoinURL joins a mount name and a tree-relative path into one URL path
// (no leading slash); rel of "." means the mount root itself.
func JoinURL(mount, rel string) string {
	if rel == "." {
		return mount
	}
	return mount + "/" + rel
}

// Crumbs builds a breadcrumb trail from a URL path (no leading slash):
// home (shown as the site name), then each path segment.
// The last segment gets no href; it's the page being viewed.
func Crumbs(site, urlpath string) []Crumb {
	cs := []Crumb{{Name: site, Href: "/"}}
	if urlpath == "" {
		cs[0].Href = ""
		return cs
	}
	href := "/"
	for _, seg := range strings.Split(urlpath, "/") {
		href += seg + "/"
		cs = append(cs, Crumb{Name: seg, Href: href})
	}
	cs[len(cs)-1].Href = ""
	return cs
}

var pageTmpl = template.Must(template.New("page").Parse(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Title}}</title>
<link rel="stylesheet" href="/-/style.css">
</head>
<body>
<nav>
{{range $i, $c := .Crumbs}}{{if $i}} / {{end}}{{if $c.Href}}<a href="{{$c.Href}}">{{$c.Name}}</a>{{else}}{{$c.Name}}{{end}}{{if and (eq $i 0) $.ThemeName}}<span class="theme">@{{$.ThemeName}}</span>{{end}}{{end}}
{{if .RawHref}}<a class="raw" href="{{.RawHref}}">raw</a>{{end}}
</nav>
{{if .Listing}}<ul class="listing">{{range .Listing}}<li><a href="{{.Href}}">{{.Name}}</a></li>{{end}}</ul>{{end}}
{{if .Content}}<article>{{.Content}}</article>{{end}}
{{if .Iframe}}<iframe class="passthrough" src="{{.Iframe}}"></iframe>{{end}}
</body>
</html>
`))

// Page executes the page chrome template.
func Page(w io.Writer, pd PageData) error {
	return pageTmpl.Execute(w, pd)
}

display/display.go · 3.4 KiB · a92ee56422