diff options
| author | Kyren223 <contact@kyren.codes> | 2026-10-10 02:41:26 +0300 |
|---|---|---|
| committer | Kyren223 <contact@kyren.codes> | 2026-10-10 02:41:26 +0300 |
| commit | 068e5fc397b2dc2a5c77687577e0c29c97b4a422 (patch) | |
| tree | c65f310112d16893dd5ceaadac7a28d074b2ba14 /website.go | |
| parent | 660e6fb1b4b4fa57596471d80c4e4264c711f915 (diff) | |
Saving before stuff goes bad
Diffstat (limited to 'website.go')
| -rw-r--r-- | website.go | 61 |
1 files changed, 56 insertions, 5 deletions
@@ -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, `<div class="codeblock" data-lang="%s">`, template.HTMLEscapeString(string(lang))) + } else { + w.WriteString(`</div>`) + } + }), + ), + ), 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) |
