oficina / docs Public

Docs list: each row shows its document's opening words under the title

Paul, on the preview: the row needed something under the title. The
excerpt is the body text in order, headings skipped, cut near 240
characters; it comes from the same parse as the thumbnail and lives in
the same in-memory entry, so it costs no extra replay and is never
written down. One line on a desktop, two on a phone. A TensorX summary
can take this line over later.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 19551b3f1ee05eabb4bc221f5f82a0894112db81 parent 593737a
4 files changed, +144 −37
  • internal/docs/list_test.go +3 −3
  • internal/docs/preview.go +80 −24
  • internal/docs/preview_internal_test.go +34 −9
  • internal/docs/views/list.html +27 −1

CI passed — run details

diff --git a/internal/docs/list_test.go b/internal/docs/list_test.go
index 103e40a..3a6517c 100644
--- a/internal/docs/list_test.go
+++ b/internal/docs/list_test.go
@@ -256,9 +256,9 @@ func TestAThumbnailCarriesNoneOfTheDocumentsWords(t *testing.T) {
t.Fatal("no thumbnail was drawn, so this test would pass without checking anything")
}
- // Words from the seeded documents' bodies. The list shows titles,
- // never content, so none of these belongs anywhere on this page —
- // and certainly not inside a thumbnail.
+ // Words from the seeded documents' bodies. A row shows its
+ // document's opening words beside the title (the excerpt), so some
+ // of these are on the page — but never inside a thumbnail.
for _, canary := range []string{
"Millbrook", "allotment", "printer", "waterworks", "committee",
} {
diff --git a/internal/docs/preview.go b/internal/docs/preview.go
index c3c3986..45aa560 100644
--- a/internal/docs/preview.go
+++ b/internal/docs/preview.go
@@ -3,7 +3,9 @@ package docs
import (
"context"
"errors"
+ "strings"
"sync"
+ "unicode/utf8"
"github.com/carlosframework/docs/internal/export"
)
@@ -86,15 +88,19 @@ func bars(lines []export.LineBox) []previewBar {
return out
}
-// previews is the document list's thumbnail cache.
+// previews is the document list's thumbnail cache, and the cache of
+// each row's opening words.
//
// IN MEMORY, AND NOT IN THE DATABASE — the one decision in this file
// worth arguing with, so here is the argument. A previewBar carries no
// characters: it is four numbers, and the document's words never leave
-// export.PageOne (see LineBox's own note). So this is not plaintext,
-// and writing it to SQLite would not put document text at rest.
+// export.PageOne (see LineBox's own note). An excerpt is the
+// document's own words, so writing it to SQLite WOULD put text at
+// rest, outside the seal; it is the running instance holding what it
+// already holds whenever it replays a document, and nothing more.
//
-// It is still not written down, for a reason that outlives that. Spec
+// The bars are not written down either, for a reason that outlives
+// that. Spec
// §2 seals content at rest and §2's boundary test greps raw SQLite
// bytes for a fixture's known plaintext. A preview column would be a
// second thing to remember to seal when sealing lands, and that grep
@@ -143,6 +149,9 @@ type previewEntry struct {
// never read — get answered from bars being nil either way — so it
// recorded a distinction nothing acted on.
bars []previewBar
+ // excerpt is the document's opening words (see excerpt), "" when it
+ // has none. Cached and settled with the bars, from the same parse.
+ excerpt string
}
// maxPreviews bounds the map.
@@ -164,19 +173,19 @@ const maxPreviews = 512
func newPreviews() *previews { return &previews{by: map[int64]previewEntry{}} }
-// get returns the cached bars for doc at its current updated_at, and
-// whether there was a usable entry.
-func (p *previews) get(id int64, updatedAtNanos int64) ([]previewBar, bool) {
+// get returns the cached entry for doc at its current updated_at, and
+// whether there was a usable one.
+func (p *previews) get(id int64, updatedAtNanos int64) (previewEntry, bool) {
p.mu.Lock()
defer p.mu.Unlock()
e, ok := p.by[id]
if !ok || e.updatedAtNanos != updatedAtNanos {
- return nil, false
+ return previewEntry{}, false
}
- return e.bars, true
+ return e, true
}
-func (p *previews) put(id int64, updatedAtNanos int64, b []previewBar) {
+func (p *previews) put(id int64, updatedAtNanos int64, e previewEntry) {
p.mu.Lock()
defer p.mu.Unlock()
if _, replacing := p.by[id]; !replacing {
@@ -187,7 +196,8 @@ func (p *previews) put(id int64, updatedAtNanos int64, b []previewBar) {
}
}
}
- p.by[id] = previewEntry{updatedAtNanos: updatedAtNanos, bars: b}
+ e.updatedAtNanos = updatedAtNanos
+ p.by[id] = e
}
// thumbnail lays out one document's first page and caches the result.
@@ -210,20 +220,20 @@ func (p *previews) put(id int64, updatedAtNanos int64, b []previewBar) {
// timeout and a busy database are the same shape. Those say nothing
// about the document, so they are logged at a lower level and left
// uncached, and the next render simply tries again.
-func (h *handlers) thumbnail(ctx context.Context, doc *Document) []previewBar {
+func (h *handlers) thumbnail(ctx context.Context, doc *Document) previewEntry {
nanos := doc.UpdatedAt.UnixNano()
// remember records a settled "draw nothing" for this version. A
// context that is already done means the answer is about the
// request, not the document, so nothing is written down.
- remember := func(what string, err error) []previewBar {
+ remember := func(what string, err error) previewEntry {
if ctx.Err() != nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
h.logger.Debug("docs: list thumbnail abandoned with the request", "doc_id", doc.ID, "stage", what, "err", err)
- return nil
+ return previewEntry{}
}
h.logger.Error("docs: "+what+" for a list thumbnail", "doc_id", doc.ID, "err", err)
- h.previews.put(doc.ID, nanos, nil)
- return nil
+ h.previews.put(doc.ID, nanos, previewEntry{})
+ return previewEntry{}
}
structure, err := h.store.JSON(ctx, doc)
@@ -246,9 +256,51 @@ func (h *handlers) thumbnail(ctx context.Context, doc *Document) []previewBar {
return remember("parse structure", err)
}
- b := bars(export.PageOne(parsed))
- h.previews.put(doc.ID, nanos, b)
- return b
+ e := previewEntry{bars: bars(export.PageOne(parsed)), excerpt: excerpt(parsed.Root)}
+ h.previews.put(doc.ID, nanos, e)
+ return e
+}
+
+// excerptRunes is about two lines of a list row, enough that the one
+// line the row shows is always full; the stylesheet does the cutting.
+const excerptRunes = 240
+
+// excerpt is a document's opening words, for the line under its title
+// in the list: the text of its blocks in order, headings skipped (a
+// heading is usually the title again, or a section name that says less
+// than the paragraph under it), joined with spaces and cut at a word
+// near excerptRunes. "" when the document has no body text.
+func excerpt(root export.Node) string {
+ var b strings.Builder
+ var walk func(n export.Node) bool
+ walk = func(n export.Node) bool {
+ if n.Type == "heading" {
+ return true
+ }
+ if n.IsText() {
+ b.WriteString(n.Text)
+ return utf8.RuneCountInString(b.String()) < excerptRunes
+ }
+ for _, c := range n.Content {
+ if !walk(c) {
+ return false
+ }
+ }
+ if n.Type != "doc" && b.Len() > 0 && !strings.HasSuffix(b.String(), " ") {
+ b.WriteByte(' ')
+ }
+ return true
+ }
+ walk(root)
+ text := strings.Join(strings.Fields(b.String()), " ")
+ if utf8.RuneCountInString(text) <= excerptRunes {
+ return text
+ }
+ cut := []rune(text)[:excerptRunes]
+ if i := strings.LastIndexByte(string(cut), ' '); i > excerptRunes/2 {
+ return string(cut)[:i] + "…"
+ }
+ return string(cut) + "…"
}
// thumbnailBudget is how many documents one list render will lay out
@@ -282,11 +334,11 @@ const thumbnailBudget = 40
func (h *handlers) thumbnailer(ctx context.Context) func(*Document) thumbView {
budget := thumbnailBudget
return func(d *Document) thumbView {
- if b, ok := h.previews.get(d.ID, d.UpdatedAt.UnixNano()); ok {
- return newThumbView(b)
+ if e, ok := h.previews.get(d.ID, d.UpdatedAt.UnixNano()); ok {
+ return newThumbView(e)
}
if budget <= 0 {
- return newThumbView(nil)
+ return newThumbView(previewEntry{})
}
budget--
return newThumbView(h.thumbnail(ctx, d))
@@ -305,8 +357,12 @@ func (h *handlers) thumbnailer(ctx context.Context) func(*Document) thumbView {
type thumbView struct {
Bars []previewBar
W, H int
+ // Excerpt is the document's opening words, the line under a list
+ // row's title. It travels with the bars because it comes from the
+ // same parse and lives in the same cache entry.
+ Excerpt string
}
-func newThumbView(b []previewBar) thumbView {
- return thumbView{Bars: b, W: int(export.PageWidthPt), H: int(export.PageHeightPt)}
+func newThumbView(e previewEntry) thumbView {
+ return thumbView{Bars: e.bars, W: int(export.PageWidthPt), H: int(export.PageHeightPt), Excerpt: e.excerpt}
}
diff --git a/internal/docs/preview_internal_test.go b/internal/docs/preview_internal_test.go
index a04708b..f504c04 100644
--- a/internal/docs/preview_internal_test.go
+++ b/internal/docs/preview_internal_test.go
@@ -1,7 +1,9 @@
package docs
import (
+ "strings"
"testing"
+ "unicode/utf8"
"github.com/carlosframework/docs/internal/export"
)
@@ -69,9 +71,9 @@ func TestATinyLineStillDrawsSomething(t *testing.T) {
func TestAChangedUpdatedAtMissesTheCache(t *testing.T) {
p := newPreviews()
want := []previewBar{{X: 1, Y: 2, W: 3, H: 4}}
- p.put(7, 1000, want)
+ p.put(7, 1000, previewEntry{bars: want})
- if got, ok := p.get(7, 1000); !ok || len(got) != 1 || got[0] != want[0] {
+ if got, ok := p.get(7, 1000); !ok || len(got.bars) != 1 || got.bars[0] != want[0] {
t.Errorf("same version did not hit: got %+v ok=%v", got, ok)
}
if _, ok := p.get(7, 1001); ok {
@@ -87,20 +89,20 @@ func TestAChangedUpdatedAtMissesTheCache(t *testing.T) {
// laid out again on every single list render.
func TestACachedFailureIsAHit(t *testing.T) {
p := newPreviews()
- p.put(7, 1000, nil)
+ p.put(7, 1000, previewEntry{})
got, ok := p.get(7, 1000)
if !ok {
t.Fatal("a cached failure missed, so it will be retried on every render")
}
- if got != nil {
- t.Errorf("a cached failure returned bars: %+v", got)
+ if got.bars != nil || got.excerpt != "" {
+ t.Errorf("a cached failure returned a preview: %+v", got)
}
}
func TestTheCacheIsBounded(t *testing.T) {
p := newPreviews()
for i := int64(0); i < maxPreviews+10; i++ {
- p.put(i, 1, []previewBar{{X: 1}})
+ p.put(i, 1, previewEntry{bars: []previewBar{{X: 1}}})
}
p.mu.Lock()
n := len(p.by)
@@ -119,7 +121,7 @@ func TestTheCacheIsBounded(t *testing.T) {
func TestOverflowingTheCacheKeepsItWarm(t *testing.T) {
p := newPreviews()
for i := int64(0); i < maxPreviews*2; i++ {
- p.put(i, 1, []previewBar{{X: 1}})
+ p.put(i, 1, previewEntry{bars: []previewBar{{X: 1}}})
}
p.mu.Lock()
n := len(p.by)
@@ -134,10 +136,10 @@ func TestOverflowingTheCacheKeepsItWarm(t *testing.T) {
func TestReplacingAnEntryEvictsNothing(t *testing.T) {
p := newPreviews()
for i := int64(0); i < maxPreviews; i++ {
- p.put(i, 1, []previewBar{{X: 1}})
+ p.put(i, 1, previewEntry{bars: []previewBar{{X: 1}}})
}
for i := int64(0); i < maxPreviews; i++ {
- p.put(i, 2, []previewBar{{X: 2}})
+ p.put(i, 2, previewEntry{bars: []previewBar{{X: 2}}})
}
p.mu.Lock()
n := len(p.by)
@@ -146,3 +148,26 @@ func TestReplacingAnEntryEvictsNothing(t *testing.T) {
t.Errorf("cache holds %d entries after re-caching the same documents, want %d", n, maxPreviews)
}
}
+
+// The line under a row's title is the document's opening words: body
+// text in order, headings skipped, whitespace folded, cut at a word.
+func TestExcerpt(t *testing.T) {
+ text := func(s string) export.Node { return export.Node{Type: "text", Text: s} }
+ block := func(typ string, kids ...export.Node) export.Node { return export.Node{Type: typ, Content: kids} }
+ doc := block("doc",
+ block("heading", text("Early years")),
+ block("paragraph", text("Before 1887, "), text("Millbrook drew its water")),
+ block("bullet_list", block("list_item", block("paragraph", text("from wells")))),
+ )
+ if got, want := excerpt(doc), "Before 1887, Millbrook drew its water from wells"; got != want {
+ t.Errorf("excerpt = %q, want %q", got, want)
+ }
+ if got := excerpt(block("doc", block("heading", text("Only a heading")), block("paragraph"))); got != "" {
+ t.Errorf("a document with no body text has excerpt %q", got)
+ }
+ long := strings.Repeat("word ", 100)
+ got := excerpt(block("doc", block("paragraph", text(long))))
+ if n := utf8.RuneCountInString(got); n > excerptRunes+1 || !strings.HasSuffix(got, "word…") {
+ t.Errorf("a long document's excerpt is %d runes, ending %q", n, got[len(got)-12:])
+ }
+}
diff --git a/internal/docs/views/list.html b/internal/docs/views/list.html
index 2645df9..ef6779d 100644
--- a/internal/docs/views/list.html
+++ b/internal/docs/views/list.html
@@ -73,7 +73,7 @@
row, whose title link it stretches over the whole row itself. */}}
{{define "row"}}<div rst-row>
{{template "thumb" .Thumb}}
- <span rst-row-main><a href="{{.Href}}">{{.Title}}</a></span>
+ <span rst-row-main><a href="{{.Href}}">{{.Title}}</a>{{with .Thumb.Excerpt}}<span docs-row-excerpt>{{.}}</span>{{end}}</span>
{{if .SharedNote}}<span rst-muted docs-row-shared>{{.SharedNote}}</span>{{end}}<span docs-row-when>{{.When}}</span>
</div>{{end}}
{{define "content"}}<style>
@@ -378,6 +378,32 @@
font-weight: 600;
color: var(--rst-text-muted);
}
+ /* Title over the document's opening words: the row says what is in
+ it, not only what it is called. The words are the page's own
+ (preview.go's excerpt), one line, muted, cut by the stylesheet. */
+ [docs-list] [rst-row-main] {
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ min-inline-size: 0;
+ }
+ [docs-row-excerpt] {
+ overflow: hidden;
+ color: var(--rst-text-muted);
+ font-size: var(--rst-fs-sm, 0.875rem);
+ line-height: 1.4;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+ }
+ @media (max-width: 36rem) {
+ [docs-row-excerpt] {
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ -webkit-line-clamp: 2;
+ line-clamp: 2;
+ white-space: normal;
+ }
+ }
[docs-row-when] {
flex: none;
color: var(--rst-text-muted);