Highlight returns SX tree, rendered to HTML/DOM by pipeline

highlight.sx now returns a list of (span :class "..." "text") elements
instead of a string. The rendering pipeline handles the rest:
- Server render-to-html: produces <span class="...">text</span>
- Client render-to-dom: produces DOM span elements
- Aser: serializes spans as SX for client rendering

Key fixes:
- hl-span uses (make-keyword "class") not :class (keywords evaluate
  to strings in list context)
- render-sx-tokens returns flat list of spans (no wrapper)
- hl-escape is identity (no escaping needed for tree values)
- highlight.sx added to browser bundle + platform loader
- ~docs/code renders src directly as child of pre

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-31 23:12:36 +00:00
parent 609be68c9c
commit b62dfb25e5
12 changed files with 901 additions and 650 deletions

View File

@@ -67,36 +67,7 @@
(define hl-ws? (fn (c) (or (= c " ") (= c "\n") (= c "\t") (= c "\r"))))
(define
hl-escape
(fn
(s)
(let
((result "") (i 0) (len (string-length s)))
(let
loop
()
(when
(< i len)
(let
((c (substring s i (+ i 1))))
(set!
result
(str
result
(if
(= c "\\")
"\\\\"
(if
(= c "\"")
"\\\""
(if
(= c "\n")
"\\n"
(if (= c "\t") "\\t" (if (= c "\r") "\\r" c)))))))
(set! i (+ i 1))
(loop))))
result)))
(define hl-escape (fn (s) s))
(define
hl-span
@@ -104,8 +75,8 @@
(class text)
(if
(= class "")
(str "(span \"" (hl-escape text) "\")")
(str "(span :class \"" class "\" \"" (hl-escape text) "\")"))))
(list (quote span) text)
(list (quote span) (make-keyword "class") class text))))
(define
tokenize-sx
@@ -309,9 +280,13 @@
render-sx-tokens
(fn
(tokens)
(let
((parts (map (fn (tok) (let ((kind (first tok)) (text (first (rest tok)))) (hl-span (get sx-token-classes kind "") text))) tokens)))
(str "(<> " (join " " parts) ")"))))
(map
(fn
(tok)
(let
((cls (or (dict-get sx-token-classes (first tok)) "")))
(hl-span cls (nth tok 1))))
tokens)))
(define highlight-sx (fn (code) (render-sx-tokens (tokenize-sx code))))
@@ -322,7 +297,4 @@
(if
(or (= lang "lisp") (= lang "sx") (= lang "sexp") (= lang "scheme"))
(highlight-sx code)
(str
"(pre :class \"text-sm overflow-x-auto\" (code \""
(hl-escape code)
"\"))"))))
(list (quote code) code))))