From 068e5fc397b2dc2a5c77687577e0c29c97b4a422 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sat, 10 Oct 2026 02:41:26 +0300 Subject: Saving before stuff goes bad --- website.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) (limited to 'website.go') diff --git a/website.go b/website.go index 47b4051..b54030c 100644 --- a/website.go +++ b/website.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "html/template" "io/fs" "log" "net/http" @@ -18,9 +19,15 @@ import ( "time" "github.com/a-h/templ" + chromahtml "github.com/alecthomas/chroma/v2/formatters/html" "github.com/yuin/goldmark" + highlighting "github.com/yuin/goldmark-highlighting/v2" + "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/extension" + "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/text" + "github.com/yuin/goldmark/util" "gopkg.in/yaml.v3" ) @@ -167,8 +174,25 @@ type Blog struct { } var md = goldmark.New( - goldmark.WithExtensions(extension.GFM, extension.Footnote), + goldmark.WithExtensions( + extension.GFM, + extension.Footnote, + highlighting.NewHighlighting( + highlighting.WithFormatOptions(chromahtml.WithClasses(true)), + highlighting.WithWrapperRenderer(func(w util.BufWriter, ctx highlighting.CodeBlockContext, entering bool) { + if entering { + lang, _ := ctx.Language() + fmt.Fprintf(w, `
`, template.HTMLEscapeString(string(lang))) + } else { + w.WriteString(`
`) + } + }), + ), + ), goldmark.WithRendererOptions(html.WithUnsafe()), + goldmark.WithParserOptions( + parser.WithASTTransformers(util.Prioritized(externalLinks{}, 100)), + ), ) type frontmatter struct { @@ -295,6 +319,33 @@ func latestBlog(blogs []Blog) *Blog { return &blogs[0] } +type externalLinks struct{} + +func openInNewTab(n ast.Node) { + n.SetAttributeString("target", []byte("_blank")) + n.SetAttributeString("rel", []byte("noopener noreferrer")) +} + +func (externalLinks) Transform(doc *ast.Document, reader text.Reader, pc parser.Context) { + src := reader.Source() + ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { + if !entering { + return ast.WalkContinue, nil + } + switch link := n.(type) { + case *ast.Link: + if bytes.HasPrefix(link.Destination, []byte("http")) { + openInNewTab(link) + } + case *ast.AutoLink: + if link.AutoLinkType == ast.AutoLinkURL && bytes.HasPrefix(link.URL(src), []byte("http")) { + openInNewTab(link) + } + } + return ast.WalkContinue, nil + }) +} + var projects = []Project{ { Slug: "krypton", @@ -545,13 +596,13 @@ func main() { } jetbrains := FontPack{ - Monospace: "JetBrains Mono", - Normal: `system-ui, sans-serif`, + Monospace: `JetBrains Mono, monospace`, + Normal: `Inter, system-ui, sans-serif`, } minecraft := FontPack{ - Monospace: `"Monocraft", "JetBrains Mono", monospace`, - Normal: `"Monocraft", system-ui, sans-serif`, + Monospace: `Monocraft, JetBrains Mono, monospace`, + Normal: `Monocraft, Inter, system-ui, sans-serif`, } SetTheme("carbonight-cat", carbonightCat) -- cgit v1.3.1