xonecas.io
8356
Wx

www-xonecas-io / commit 657db29

README code blocks are highlighted like the file pages (1024)

Co-Authored-By: Claude Opus 5.5

Sean Caetano Martin2026-11-08 657db290b3f052330e545a67e79d95a6b219bc4b parent e3ea431

8 files changed

internal/site/browse.go+29−14
@@ -6,6 +6,7 @@ import (
66 "fmt"
77 "html"
88 "html/template"
9+ "io"
910 "os"
1011 "path"
1112 "path/filepath"
@@ -107,10 +108,27 @@ func highlight(name, text string) (template.HTML, error) {
107108 if lexer == nil {
108109 lexer = lexers.Fallback
109110 }
110- it, err := lexer.Tokenise(nil, text)
111+ tokens, err := tokenise(lexer, text)
111112 if err != nil {
112113 return "", err
113114 }
115+ var buf strings.Builder
116+ buf.WriteString(`<pre class="code">`)
117+ for i, line := range chroma.SplitTokensIntoLines(tokens) {
118+ fmt.Fprintf(&buf, `<span id="L%d"><a href="#L%d">%d</a>`, i+1, i+1, i+1)
119+ writeTokens(&buf, line)
120+ buf.WriteString("</span>")
121+ }
122+ buf.WriteString("</pre>")
123+ return template.HTML(buf.String()), nil
124+}
125+
126+// tokenise lexes text into the palette's token types, merging neighbours of the same type.
127+func tokenise(lexer chroma.Lexer, text string) ([]chroma.Token, error) {
128+ it, err := lexer.Tokenise(nil, text)
129+ if err != nil {
130+ return nil, err
131+ }
114132 var tokens []chroma.Token
115133 for _, t := range it.Tokens() {
116134 t.Type = colour(t.Type)
@@ -120,21 +138,18 @@ func highlight(name, text string) (template.HTML, error) {
120138 tokens = append(tokens, t)
121139 }
122140 }
123- var buf strings.Builder
124- buf.WriteString(`<pre class="code">`)
125- for i, line := range chroma.SplitTokensIntoLines(tokens) {
126- fmt.Fprintf(&buf, `<span id="L%d"><a href="#L%d">%d</a>`, i+1, i+1, i+1)
127- for _, t := range line {
128- if c := chroma.StandardTypes[t.Type]; c != "" {
129- fmt.Fprintf(&buf, `<b class="%s">%s</b>`, c, html.EscapeString(t.Value))
130- } else {
131- buf.WriteString(html.EscapeString(t.Value))
132- }
141+ return tokens, nil
142+}
143+
144+// writeTokens writes tokens as escaped text, each coloured one in a <b> with its class.
145+func writeTokens(w io.StringWriter, tokens []chroma.Token) {
146+ for _, t := range tokens {
147+ if c := chroma.StandardTypes[t.Type]; c != "" {
148+ w.WriteString(`<b class="` + c + `">` + html.EscapeString(t.Value) + `</b>`)
149+ } else {
150+ w.WriteString(html.EscapeString(t.Value))
133151 }
134- buf.WriteString("</span>")
135152 }
136- buf.WriteString("</pre>")
137- return template.HTML(buf.String()), nil
138153 }
139154
140155 // palette is the token types site.css colours. Every other token is plain text, which needs no span,
internal/site/readme.go+36−1
@@ -8,10 +8,14 @@ import (
88 "regexp"
99 "strings"
1010
11+ "github.com/alecthomas/chroma/v2/lexers"
1112 "github.com/yuin/goldmark"
13+ "github.com/yuin/goldmark/ast"
1214 "github.com/yuin/goldmark/extension"
1315 "github.com/yuin/goldmark/parser"
16+ "github.com/yuin/goldmark/renderer"
1417 mdhtml "github.com/yuin/goldmark/renderer/html"
18+ "github.com/yuin/goldmark/util"
1519
1620 "xonecas.io/site/internal/repo"
1721 )
@@ -20,9 +24,40 @@ import (
2024 var md = goldmark.New(
2125 goldmark.WithExtensions(extension.GFM),
2226 goldmark.WithParserOptions(parser.WithAutoHeadingID()),
23- goldmark.WithRendererOptions(mdhtml.WithUnsafe()),
27+ goldmark.WithRendererOptions(mdhtml.WithUnsafe(), renderer.WithNodeRenderers(util.Prioritized(fenced{}, 100))),
2428 )
2529
30+// fenced renders a fenced code block in the file pages' colours, by the fence's language.
31+// A block with no language, or one chroma does not know, stays plain.
32+type fenced struct{}
33+
34+func (fenced) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) {
35+ r.Register(ast.KindFencedCodeBlock, func(w util.BufWriter, src []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
36+ if !entering {
37+ return ast.WalkSkipChildren, nil
38+ }
39+ b := n.(*ast.FencedCodeBlock)
40+ var text strings.Builder
41+ for i := range b.Lines().Len() {
42+ line := b.Lines().At(i)
43+ text.Write(line.Value(src))
44+ }
45+ lang := string(b.Language(src))
46+ lexer := lexers.Get(lang)
47+ if lexer == nil {
48+ lexer = lexers.Fallback
49+ }
50+ tokens, err := tokenise(lexer, text.String())
51+ if err != nil {
52+ return ast.WalkStop, err
53+ }
54+ w.WriteString(`<pre><code class="code language-` + html.EscapeString(lang) + `">`)
55+ writeTokens(w, tokens)
56+ w.WriteString("</code></pre>\n")
57+ return ast.WalkSkipChildren, nil
58+ })
59+}
60+
2661 var (
2762 attr = regexp.MustCompile(`\b(src|href)="([^"]*)"`)
2863 external = regexp.MustCompile(`(?i)^([a-z][a-z0-9+.-]*:|#|/)`)
internal/site/readme_test.go+20−0
@@ -0,0 +1,20 @@
1+package site
2+
3+import (
4+ "bytes"
5+ "strings"
6+ "testing"
7+)
8+
9+// TestFenced colours a README's fenced code by its language, and leaves an unknown one plain.
10+func TestFenced(t *testing.T) {
11+ var buf bytes.Buffer
12+ if err := md.Convert([]byte("```go\nfunc main() {}\n```\n\n```nope\n<x>\n```\n"), &buf); err != nil {
13+ t.Fatal(err)
14+ }
15+ for _, want := range []string{`<pre><code class="code language-go"><b class="k">func</b>`, `<pre><code class="code language-nope">&lt;x&gt;`} {
16+ if !strings.Contains(buf.String(), want) {
17+ t.Errorf("missing %q in %s", want, buf.String())
18+ }
19+ }
20+}
tickets/1024-markdown-code-snippets-are-not-syntax.md+13−2
@@ -2,12 +2,13 @@
22 id: 1024
33 title: markdown code snippets are not syntax highlighted
44 type: feature
5-status: inbox
5+status: review
66 priority: p2
77 created: 2026-11-08T04:12:36Z
8-updated: 2026-11-08T04:23:13Z
8+updated: 2026-11-08T04:40:31Z
99 source: devnote
1010 area: site
11+moves: 2026-11-08T04:37:43Z inbox doing drag; 2026-11-08T04:40:31Z doing review agent
1112 ---
1213
1314 ## Context
@@ -46,3 +47,13 @@ Filed from `http://192.168.122.10:8080/projects/cel-tui/`, on `pre`.
4647 - 2026-11-08 04:23Z (agent): Triage: type feature (it was bug: READMEs were never highlighted), priority p2, area site. No typos to fix.
4748
4849 READMEs render with goldmark (internal/site/readme.go) with no code highlighting, so fenced blocks come out as plain <pre><code class="language-ts">. The file browser already highlights with chroma (browse.go highlight(), palette in site.css), so the same palette can be reused for README code blocks by fence language. Related: 1008.
50+
51+- 2026-11-08 04:40Z (agent): Done. README fenced code blocks are now coloured by the fence's language, in the same palette as the file pages (keywords, strings, numbers, comments and so on).
52+
53+ - internal/site/readme.go: a small goldmark renderer for fenced blocks picks the chroma lexer from the fence language and writes <pre><code class="code language-x">. No language, or one chroma does not know (text, txt, none), stays plain.
54+ - browse.go: the token code is split out of highlight() into tokenise() and writeTokens(), so file pages and READMEs share it. File pages are unchanged.
55+ - readme_test.go: a go block gets <b class="k">func</b>; an unknown language stays escaped plain text.
56+
57+ CHECKED: go vet, go test pass. Rebuilt: 136 fenced blocks across all READMEs, 70 now carry colour. All ts, typescript, lua, json, javascript and html blocks are coloured. bash and sh only where they have something to colour (17 of 47, 7 of 12): one-line commands like 'npm i -g mini-coder' are plain words in bash's lexer. Screenshot of cel-tui's Quick Example in light theme looks like the file pages.
58+
59+ NOT CHECKED: dark theme (the code block is dark in both, same palette as file pages).
tickets/1028-let-s-make-the-slider-more-prominent-and-cool.md+3−2
@@ -2,12 +2,13 @@
22 id: 1028
33 title: let's make the slider more prominent and cool. As wide as the table below it.
44 type: feature
5-status: inbox
5+status: doing
66 priority: p2
77 created: 2026-11-08T04:27:51Z
8-updated: 2026-11-08T04:28:00Z
8+updated: 2026-11-08T04:37:47Z
99 source: devnote
1010 area: site
11+moves: 2026-11-08T04:37:47Z inbox doing drag
1112 ---
1213
1314 ## Context
tickets/1029-let-s-add-click-to-copy-to-clipboard-to-the.md+11−3
@@ -1,12 +1,14 @@
11 ---
22 id: 1029
33 title: Let's add click to copy to clipboard to the clone link
4-type: bug
5-status: inbox
4+type: feature
5+status: doing
66 priority: p2
77 created: 2026-11-08T04:29:39Z
8-updated: 2026-11-08T04:29:39Z
8+updated: 2026-11-08T04:37:49Z
99 source: devnote
10+area: site
11+moves: 2026-11-08T04:37:49Z inbox doing drag
1012 ---
1113
1214 ## Context
@@ -14,3 +16,9 @@ source: devnote
1416 Filed from `http://192.168.122.10:8080/projects/macotron-plugin-caffeinate/`, on `pre.bg-pre.text-pre-fg.rounded-[2px]`.
1517
1618 > git clone https://xonecas.io/macotron-plugin-caffeinate.git
19+
20+## Notes
21+
22+- 2026-11-08 04:34Z (agent): Triage: type feature (it was bug), priority p2, area site. No typos to fix.
23+
24+ The clone box is the <pre> in internal/site/templates/project.html (Clone section), 'git clone https://xonecas.io/<name>.git' since 1015. Today it only has select-all, so one click selects the text but copies nothing. Related: 1015.
tickets/1030-activity-and-projects-need-pagination.md+13−0
@@ -0,0 +1,13 @@
1+---
2+id: 1030
3+title: activity and projects need pagination
4+type: feature
5+status: doing
6+priority: p2
7+created: 2026-11-08T04:38:19Z
8+updated: 2026-11-08T04:38:50Z
9+source: human
10+moves: 2026-11-08T04:38:48Z inbox doing field
11+---
12+
13+use pagination buttons at the bottom, with arrows and dots, discreete.
tickets/1031-tags-need-to-be-much-more-specific-use.md+26−0
@@ -0,0 +1,26 @@
1+---
2+id: 1031
3+title: Tags need to be much more specific. use dependencies files and other leads to augment this. Even keyworrds from readmes or agents/claudes.
4+type: bug
5+status: inbox
6+priority: p2
7+created: 2026-11-08T04:40:26Z
8+updated: 2026-11-08T04:40:26Z
9+source: devnote
10+---
11+
12+## Context
13+
14+Filed from `http://192.168.122.10:8080/projects/`, on `span.inline-flex.items-center.gap-1.5`.
15+
16+> 80
17+> 58
18+> Ir
19+> irvc
20+> A WebRTC-powered private text and voice chat. Self-hosted: Go + SQLite backend, React frontend, LiveKit for voice and screen share.
21+> TypeScript
22+> 58
23+> post-AI
24+> big
25+> frontend
26+> backend