content: TOC rendering (toc.sx) + 8 tests (594/594)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,7 @@ if [ ! -x "$SX_SERVER" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
SUITES=(block doc render api meta page page-full markdown text section compose tree-edit clone query transform stats table data wire validate store snapshot crdt crdt-store sync md-import md-doc fed)
|
||||
SUITES=(block doc render api meta page page-full markdown text section compose tree-edit clone query toc transform stats table data wire validate store snapshot crdt crdt-store sync md-import md-doc fed)
|
||||
|
||||
OUT_JSON="lib/content/scoreboard.json"
|
||||
OUT_MD="lib/content/scoreboard.md"
|
||||
@@ -49,6 +49,7 @@ run_suite() {
|
||||
(load "lib/content/tree-edit.sx")
|
||||
(load "lib/content/clone.sx")
|
||||
(load "lib/content/query.sx")
|
||||
(load "lib/content/toc.sx")
|
||||
(load "lib/content/transform.sx")
|
||||
(load "lib/content/stats.sx")
|
||||
(load "lib/content/table.sx")
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
"tree-edit": {"pass": 17, "fail": 0},
|
||||
"clone": {"pass": 10, "fail": 0},
|
||||
"query": {"pass": 13, "fail": 0},
|
||||
"toc": {"pass": 8, "fail": 0},
|
||||
"transform": {"pass": 12, "fail": 0},
|
||||
"stats": {"pass": 17, "fail": 0},
|
||||
"table": {"pass": 15, "fail": 0},
|
||||
@@ -29,7 +30,7 @@
|
||||
"md-doc": {"pass": 12, "fail": 0},
|
||||
"fed": {"pass": 20, "fail": 0}
|
||||
},
|
||||
"total_pass": 586,
|
||||
"total_pass": 594,
|
||||
"total_fail": 0,
|
||||
"total": 586
|
||||
"total": 594
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ _Generated by `lib/content/conformance.sh`_
|
||||
| tree-edit | 17 | 0 | 17 |
|
||||
| clone | 10 | 0 | 10 |
|
||||
| query | 13 | 0 | 13 |
|
||||
| toc | 8 | 0 | 8 |
|
||||
| transform | 12 | 0 | 12 |
|
||||
| stats | 17 | 0 | 17 |
|
||||
| table | 15 | 0 | 15 |
|
||||
@@ -32,4 +33,4 @@ _Generated by `lib/content/conformance.sh`_
|
||||
| md-import | 38 | 0 | 38 |
|
||||
| md-doc | 12 | 0 | 12 |
|
||||
| fed | 20 | 0 | 20 |
|
||||
| **Total** | **586** | **0** | **586** |
|
||||
| **Total** | **594** | **0** | **594** |
|
||||
|
||||
63
lib/content/tests/toc.sx
Normal file
63
lib/content/tests/toc.sx
Normal file
@@ -0,0 +1,63 @@
|
||||
;; Extension — table-of-contents rendering.
|
||||
|
||||
(st-bootstrap-classes!)
|
||||
(content/bootstrap!)
|
||||
(content-bootstrap-section!)
|
||||
|
||||
(define nl (str "\n"))
|
||||
|
||||
(define
|
||||
d
|
||||
(doc-append
|
||||
(doc-append
|
||||
(doc-append
|
||||
(doc-append (doc-empty "d") (mk-heading "intro" 1 "Intro"))
|
||||
(mk-text "p" "x"))
|
||||
(mk-heading "bg" 2 "Background"))
|
||||
(mk-section "s" (list (mk-heading "deep" 2 "Details")))))
|
||||
|
||||
;; ── markdown TOC (indented by level) ──
|
||||
(content-test
|
||||
"toc markdown"
|
||||
(content/toc-markdown d)
|
||||
(str
|
||||
"- [Intro](#intro)"
|
||||
nl
|
||||
" - [Background](#bg)"
|
||||
nl
|
||||
" - [Details](#deep)"))
|
||||
|
||||
;; ── html TOC (anchor links) ──
|
||||
(content-test
|
||||
"toc html"
|
||||
(content/toc-html d)
|
||||
"<ul><li><a href=\"#intro\">Intro</a></li><li><a href=\"#bg\">Background</a></li><li><a href=\"#deep\">Details</a></li></ul>")
|
||||
|
||||
;; ── html escapes heading text ──
|
||||
(content-test
|
||||
"toc html escapes"
|
||||
(content/toc-html
|
||||
(doc-append (doc-empty "d") (mk-heading "h" 1 "A < B")))
|
||||
"<ul><li><a href=\"#h\">A < B</a></li></ul>")
|
||||
|
||||
;; ── empty / no headings ──
|
||||
(content-test "toc html empty" (content/toc-html (doc-empty "e")) "")
|
||||
(content-test "toc markdown empty" (content/toc-markdown (doc-empty "e")) "")
|
||||
(content-test
|
||||
"toc no headings"
|
||||
(content/toc-html (doc-append (doc-empty "d") (mk-text "p" "just text")))
|
||||
"")
|
||||
|
||||
;; ── single heading ──
|
||||
(content-test
|
||||
"toc single md"
|
||||
(content/toc-markdown
|
||||
(doc-append (doc-empty "d") (mk-heading "h" 1 "Only")))
|
||||
"- [Only](#h)")
|
||||
|
||||
;; ── deep level indentation ──
|
||||
(content-test
|
||||
"toc deep indent"
|
||||
(content/toc-markdown
|
||||
(doc-append (doc-empty "d") (mk-heading "h" 3 "Deep")))
|
||||
" - [Deep](#h)")
|
||||
68
lib/content/toc.sx
Normal file
68
lib/content/toc.sx
Normal file
@@ -0,0 +1,68 @@
|
||||
;; content-on-sx — table-of-contents rendering.
|
||||
;;
|
||||
;; Turns content/headings into a user-facing TOC: a Markdown bullet list indented
|
||||
;; by heading level, and an HTML <ul> of anchor links (#id). The blog page links
|
||||
;; these to heading anchors.
|
||||
;;
|
||||
;; Requires (loaded by harness): query.sx (content/headings), render.sx
|
||||
;; (htmlEscaped).
|
||||
|
||||
(define toc-nl (str "\n"))
|
||||
(define
|
||||
toc-join
|
||||
(fn
|
||||
(sep parts)
|
||||
(cond
|
||||
((= (len parts) 0) "")
|
||||
((= (len parts) 1) (first parts))
|
||||
(else (str (first parts) sep (toc-join sep (rest parts)))))))
|
||||
|
||||
(define
|
||||
toc-indent
|
||||
(fn
|
||||
(n)
|
||||
(if (<= n 0) "" (str " " (toc-indent (- n 1))))))
|
||||
(define toc-esc (fn (s) (str (st-send s "htmlEscaped" (list)))))
|
||||
|
||||
(define
|
||||
content/toc-markdown
|
||||
(fn
|
||||
(doc)
|
||||
(toc-join
|
||||
toc-nl
|
||||
(map
|
||||
(fn
|
||||
(h)
|
||||
(str
|
||||
(toc-indent (- (get h :level) 1))
|
||||
"- ["
|
||||
(get h :text)
|
||||
"](#"
|
||||
(get h :id)
|
||||
")"))
|
||||
(content/headings doc)))))
|
||||
|
||||
(define
|
||||
content/toc-html
|
||||
(fn
|
||||
(doc)
|
||||
(let
|
||||
((hs (content/headings doc)))
|
||||
(if
|
||||
(= (len hs) 0)
|
||||
""
|
||||
(str
|
||||
"<ul>"
|
||||
(toc-join
|
||||
""
|
||||
(map
|
||||
(fn
|
||||
(h)
|
||||
(str
|
||||
"<li><a href=\"#"
|
||||
(get h :id)
|
||||
"\">"
|
||||
(toc-esc (get h :text))
|
||||
"</a></li>"))
|
||||
hs))
|
||||
"</ul>")))))
|
||||
Reference in New Issue
Block a user