Mapa markup: render the editor's DOM, with chips
Spec §5, §4.2: the server's HTML is now exactly what the editor's schema will draw, so mounting moves nothing. - Inline text is runs with marks, serialised by ProseMirror's DOMSerializer rule (inline.go): em outside strong, adjacent runs sharing a mark share its element. A triple-star pass pins ***x*** to <em><strong>x</strong></em>; the four outputs of §5's table hold. - [[links]] are chips (chips.go): team, through an alias (with the current title), pending, date in words, block, block removed, broken date, embed; each with a glyph and an accessible name. - Items render as <li><p>; a change of kind at one depth opens a new list; tasks carry mapa-task instead of a disabled input. - No text between blocks; a continuation line's indent is not drawn; the view's trailing break is drawn where the view would add it, and RenderPage ends a body in the editor's empty paragraph. - The resolver supplies the current title, whether the link came through an alias, and the date's long label. - SafeURL refuses bracketed IP literals, so the JavaScript port need not reimplement netip. Tests that pinned the old markup (mapa-link, <li> without <p>) are updated to the chips' markup.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+471
−111
internal/mapa/graph.go+19 −9internal/mapa/markup/chips.go+131 −0internal/mapa/markup/inline.go+181 −0internal/mapa/markup/markup_test.go+55 −8internal/mapa/markup/render.go+64 −81internal/mapa/markup/spans.go+4 −2internal/mapa/pages_test.go+7 −4internal/mapa/views.go+3 −3internal/mapatest/daily_test.go+6 −3internal/mapatest/pages_test.go+1 −1
diff --git a/internal/mapa/graph.go b/internal/mapa/graph.go| index c0b4158..3d5bf75 100644 |
| --- a/internal/mapa/graph.go |
| +++ b/internal/mapa/graph.go |
| @@ -3,6 +3,7 @@ package mapa |
| import ( |
| "net/url" |
| "sort" |
| + "time" |
| "amadan.net/oficina/mapa/internal/mapa/markup" |
| ) |
| @@ -155,30 +156,39 @@ type resolver struct { |
| // Resolve is §3.2 and §3.4: a date opens the reader's own daily note, a |
| // title or alias opens its team page, and anything else is pending. |
| -// Titles are team pages only, so a resolution reveals nothing private. |
| +// Titles are team pages only, so a resolution reveals nothing private: |
| +// the current title it carries for a chip (§4.2) is a team page's. |
| func (answer resolver) Resolve(target, |
| anchor string) markup.Resolution { |
| if markup.IsDateShaped(target) { |
| if !markup.IsDate(target) { |
| return markup.Resolution{Broken: true} |
| } |
| - return markup.Resolution{Href: "/d/" + target} |
| + return markup.Resolution{Href: "/d/" + target, |
| + Day: markup.DayLabel(target, thisYear())} |
| } |
| pageID, ok := answer.index.titles[Fold(target)] |
| if !ok { |
| return markup.Resolution{Href: "/new?title=" + |
| url.QueryEscape(target), Pending: true} |
| } |
| - href := "/p/" + publicID(answer.index.pages[pageID].nonce) |
| - if anchor == "" { |
| - return markup.Resolution{Href: href} |
| + page := answer.index.pages[pageID] |
| + found := markup.Resolution{Href: "/p/" + publicID(page.nonce), |
| + Title: page.record.Title, |
| + Alias: Fold(target) != Fold(page.record.Title)} |
| + switch { |
| + case anchor == "": |
| + case page.hasMarker(anchor): |
| + found.Href += "#b-" + anchor |
| + default: |
| + found.BlockGone = true |
| } |
| - if answer.index.pages[pageID].hasMarker(anchor) { |
| - return markup.Resolution{Href: href + "#b-" + anchor} |
| - } |
| - return markup.Resolution{Href: href, BlockGone: true} |
| + return found |
| } |
| +// thisYear is the year a date chip leaves out (§4.2). |
| +func thisYear() int { return time.Now().Year() } |
| + |
| func (page *entry) hasMarker(marker string) bool { |
| for _, block := range page.blocks { |
| if block.Marker == marker { |
diff --git a/internal/mapa/markup/chips.go b/internal/mapa/markup/chips.go| new file mode 100644 |
| index 0000000..e81e434 |
| --- /dev/null |
| +++ b/internal/mapa/markup/chips.go |
| @@ -0,0 +1,131 @@ |
| +package markup |
| + |
| +import ( |
| + "html" |
| + "strings" |
| + "time" |
| +) |
| + |
| +// Chips are how a [[link]] and an embed are drawn (spec §4.2): a link |
| +// whose mapa-chip names its kind, a glyph, the text as typed, and an |
| +// accessible name that says what the chip is. editor/chips.mjs draws the |
| +// same markup; gate L4 holds them together. |
| +// |
| +// team: <a href mapa-chip="team" aria-label="X, team page"> |
| +// alias: <a href mapa-chip="team" title="Now called Y" |
| +// aria-label="X, now called Y"> |
| +// pending: <a href mapa-chip="pending" |
| +// aria-label="X, page not written yet"> |
| +// date: <a href mapa-chip="date" aria-label="Thursday 12 |
| +// November, your note"> |
| +// block: <a href mapa-chip="block" aria-label="X, a block in Y"> |
| +// gone block: <a href mapa-chip="block gone" aria-label="X, block |
| +// removed from Y"> ... <span mapa-chip-note> |
| +// broken: <span mapa-chip="broken">2026-02-30</span> |
| +// embed: <span mapa-chip="embed" role="img" |
| +// aria-label="app:item, embedded item, not shown yet"> |
| + |
| +// glyphs are the chips' icons, Lucide's strokes on a 24px grid. |
| +var glyphs = map[string][]string{ |
| + "page": {"M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 " + |
| + "2-2V7Z", "M14 2v4a2 2 0 0 0 2 2h4", "M9 13h6", "M9 17h4"}, |
| + "page-plus": {"M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 " + |
| + "2-2V7Z", "M14 2v4a2 2 0 0 0 2 2h4", "M9 15h6", "M12 12v6"}, |
| + "calendar": {"M8 2v4", "M16 2v4", "M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 " + |
| + "1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z", "M3 10h18"}, |
| + "block": {"M13 4v16", "M17 4v16", "M19 4H9.5a4.5 4.5 0 0 0 0 9H13"}, |
| + "embed": {"M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 " + |
| + "8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z", |
| + "m3.3 7 8.7 5 8.7-5", "M12 22V12"}, |
| +} |
| + |
| +func glyph(name string) string { |
| + var out strings.Builder |
| + out.WriteString(`<svg mapa-i viewBox="0 0 24 24" aria-hidden="true">`) |
| + for _, path := range glyphs[name] { |
| + out.WriteString(`<path d="` + path + `"></path>`) |
| + } |
| + out.WriteString("</svg>") |
| + return out.String() |
| +} |
| + |
| +// chip is one link chip's parts. |
| +type chip struct { |
| + href, kind, title, label, glyph, shown, note string |
| +} |
| + |
| +func (parts chip) html() string { |
| + out := `<a href="` + html.EscapeString(parts.href) + `" mapa-chip="` + |
| + parts.kind + `"` |
| + if parts.title != "" { |
| + out += ` title="` + html.EscapeString(parts.title) + `"` |
| + } |
| + return out + ` aria-label="` + html.EscapeString(parts.label) + `">` + |
| + glyph(parts.glyph) + html.EscapeString(parts.shown) + parts.note + |
| + "</a>" |
| +} |
| + |
| +// wikiChip draws a [[link]] as the person reading sees it. |
| +func wikiChip(span Span, resolver Resolver) string { |
| + answer := resolver.Resolve(span.Target, span.Anchor) |
| + shown := firstOf(span.Text, span.Target) |
| + switch { |
| + case answer.Broken: |
| + return `<span mapa-chip="broken">` + html.EscapeString(shown) + |
| + "</span>" |
| + case answer.Day != "": |
| + return dateChip(span, answer) |
| + case answer.Pending: |
| + return chip{href: answer.Href, kind: "pending", glyph: "page-plus", |
| + label: shown + ", page not written yet", shown: shown}.html() |
| + } |
| + title := firstOf(answer.Title, span.Target) |
| + parts := chip{href: answer.Href, kind: "team", glyph: "page", |
| + label: shown + ", team page", shown: shown} |
| + if answer.Alias { |
| + parts.title = "Now called " + title |
| + parts.label = shown + ", now called " + title |
| + } |
| + if span.Anchor != "" { |
| + parts.kind, parts.glyph = "block", "block" |
| + parts.label = shown + ", a block in " + title |
| + } |
| + if span.Anchor != "" && answer.BlockGone { |
| + parts.kind = "block gone" |
| + parts.label = shown + ", block removed from " + title |
| + parts.note = "<span mapa-chip-note>block removed</span>" |
| + } |
| + return parts.html() |
| +} |
| + |
| +// dateChip is a date link: the day in words, never the ISO date. |
| +func dateChip(span Span, answer Resolution) string { |
| + parts := chip{href: answer.Href, kind: "date", glyph: "calendar", |
| + shown: answer.Day, label: answer.Day + ", your note"} |
| + if span.Text != "" { |
| + parts.shown = span.Text |
| + parts.label = span.Text + ", your note for " + answer.Day |
| + } |
| + return parts.html() |
| +} |
| + |
| +// embedChip is slice 1's embed: a quiet chip showing the reference. |
| +func embedChip(span Span) string { |
| + ref := strings.TrimSuffix(strings.TrimPrefix(span.Text, "![["), "]]") |
| + return `<span mapa-chip="embed" role="img" aria-label="` + |
| + html.EscapeString(ref+", embedded item, not shown yet") + `">` + |
| + glyph("embed") + html.EscapeString(ref) + "</span>" |
| +} |
| + |
| +// DayLabel is a date as a chip shows it: "Thursday 12 November", with |
| +// the year when it is not thisYear. It is "" for text that is no date. |
| +func DayLabel(date string, thisYear int) string { |
| + day, err := time.Parse(time.DateOnly, date) |
| + if err != nil || !IsDateShaped(date) { |
| + return "" |
| + } |
| + if day.Year() == thisYear { |
| + return day.Format("Monday 2 January") |
| + } |
| + return day.Format("Monday 2 January 2006") |
| +} |
diff --git a/internal/mapa/markup/inline.go b/internal/mapa/markup/inline.go| new file mode 100644 |
| index 0000000..1ed29bf |
| --- /dev/null |
| +++ b/internal/mapa/markup/inline.go |
| @@ -0,0 +1,181 @@ |
| +package markup |
| + |
| +import ( |
| + "html" |
| + "regexp" |
| + "strconv" |
| + "strings" |
| +) |
| + |
| +// The inline renderer draws a block's text exactly as the editor's schema |
| +// draws it (spec §5, §6.3): runs of text with marks, hard breaks and |
| +// atoms, serialised by ProseMirror's DOMSerializer rule. Marks nest in |
| +// the schema's order (link, em, strong, code), so em is always outside |
| +// strong, and adjacent runs that share a mark share its element. Gate L4 |
| +// holds this file to editor/schema.mjs's toDOM, byte for byte. |
| + |
| +// mark is one inline mark: the key that decides whether two runs share |
| +// it, and its tags. |
| +type mark struct{ key, open, close string } |
| + |
| +var ( |
| + emMark = mark{key: "em", open: "<em>", close: "</em>"} |
| + strongMark = mark{key: "strong", open: "<strong>", close: "</strong>"} |
| + codeMark = mark{key: "code", open: "<code>", close: "</code>"} |
| +) |
| + |
| +// piece is one inline node: text (escaped) with its marks, a hard break |
| +// with the marks around it, or an atom, which takes no marks. |
| +type piece struct { |
| + html string |
| + marks []mark |
| + text bool |
| +} |
| + |
| +// trailingBreak is what ProseMirror's view puts at the end of a textblock |
| +// that is empty or ends in something other than text, so the line has |
| +// height and a caret can sit there. The server draws it too, so mounting |
| +// the editor adds nothing (spec §6.2, §6.9). |
| +const trailingBreak = `<br class="ProseMirror-trailingBreak">` |
| + |
| +// EmptyParagraph is the editor's empty paragraph: the one a body ends |
| +// with when its last block is not a paragraph, so there is somewhere to |
| +// type (spec §6.2). |
| +const EmptyParagraph = "<p>" + trailingBreak + "</p>" |
| + |
| +var ( |
| + tripleStar = regexp.MustCompile(`\*\*\*(\S(?:[^*]*?\S)?)\*\*\*`) |
| + markTag = regexp.MustCompile(`</?(?:em|strong)>`) |
| + lineBreak = regexp.MustCompile(`\n[ \t]*`) |
| +) |
| + |
| +// inline draws a block's inline text. |
| +func inline(text string, resolver Resolver) string { |
| + var pieces []piece |
| + for _, span := range Spans(text) { |
| + pieces = append(pieces, spanPieces(span, resolver)...) |
| + } |
| + return serialise(pieces) |
| +} |
| + |
| +func spanPieces(span Span, resolver Resolver) []piece { |
| + switch span.Kind { |
| + case InCode: |
| + return []piece{{html: html.EscapeString(span.Text), |
| + marks: []mark{codeMark}, text: true}} |
| + case WebLink: |
| + return []piece{{html: html.EscapeString(span.Text), |
| + marks: []mark{linkMark(span)}, text: true}} |
| + case WikiLink: |
| + return []piece{{html: wikiChip(span, resolver)}} |
| + case Embed: |
| + return []piece{{html: embedChip(span)}} |
| + } |
| + return words(span.Text) |
| +} |
| + |
| +// linkMark is a web link's mark. A bare address and a [text](url) to the |
| +// same address are different marks, as they are in the schema. |
| +func linkMark(span Span) mark { |
| + bare := !strings.HasPrefix(span.Source, "[") |
| + return mark{key: "link " + span.URL + " " + strconv.FormatBool(bare), |
| + open: `<a href="` + html.EscapeString(span.URL) + |
| + `" rel="noopener noreferrer">`, |
| + close: "</a>"} |
| +} |
| + |
| +// words is a text run's emphasis and line breaks. The passes run over |
| +// escaped text, triple stars first, so ***x*** is em around strong |
| +// (spec §5). Their tags are then read as marks switching on and off, and |
| +// a line break drops the next line's indent, which the source keeps. |
| +func words(text string) []piece { |
| + tagged := html.EscapeString(text) |
| + tagged = tripleStar.ReplaceAllString(tagged, |
| + "<em><strong>$1</strong></em>") |
| + tagged = strong.ReplaceAllString(tagged, "<strong>$1</strong>") |
| + tagged = emphasis.ReplaceAllString(tagged, "<em>$1</em>") |
| + var pieces []piece |
| + ems, strongs, cursor := 0, 0, 0 |
| + for _, at := range markTag.FindAllStringIndex(tagged, -1) { |
| + pieces = lines(pieces, tagged[cursor:at[0]], marksOf(ems, strongs)) |
| + step := 1 |
| + if tagged[at[0]+1] == '/' { |
| + step = -1 |
| + } |
| + if strings.HasSuffix(tagged[at[0]:at[1]], "em>") { |
| + ems += step |
| + } else { |
| + strongs += step |
| + } |
| + cursor = at[1] |
| + } |
| + return lines(pieces, tagged[cursor:], marksOf(ems, strongs)) |
| +} |
| + |
| +func marksOf(ems, strongs int) []mark { |
| + var marks []mark |
| + if ems > 0 { |
| + marks = append(marks, emMark) |
| + } |
| + if strongs > 0 { |
| + marks = append(marks, strongMark) |
| + } |
| + return marks |
| +} |
| + |
| +// lines appends escaped text, its newlines as hard breaks. |
| +func lines(pieces []piece, text string, marks []mark) []piece { |
| + for index, line := range lineBreak.Split(text, -1) { |
| + if index > 0 { |
| + pieces = append(pieces, piece{html: "<br>", marks: marks}) |
| + } |
| + if line != "" { |
| + pieces = append(pieces, piece{html: line, marks: marks, |
| + text: true}) |
| + } |
| + } |
| + return pieces |
| +} |
| + |
| +// serialise is DOMSerializer.serializeFragment: the marks a piece shares |
| +// with the ones open stay open, the rest close, its own open in order. |
| +// Then the view's trailing break, when the textblock needs one. |
| +func serialise(pieces []piece) string { |
| + var out strings.Builder |
| + var active []mark |
| + for _, each := range pieces { |
| + keep := 0 |
| + for keep < len(active) && keep < len(each.marks) && |
| + active[keep].key == each.marks[keep].key { |
| + keep++ |
| + } |
| + for len(active) > keep { |
| + out.WriteString(active[len(active)-1].close) |
| + active = active[:len(active)-1] |
| + } |
| + for _, add := range each.marks[keep:] { |
| + out.WriteString(add.open) |
| + active = append(active, add) |
| + } |
| + out.WriteString(each.html) |
| + } |
| + for len(active) > 0 { |
| + out.WriteString(active[len(active)-1].close) |
| + active = active[:len(active)-1] |
| + } |
| + if needsBreak(pieces) { |
| + out.WriteString(trailingBreak) |
| + } |
| + return out.String() |
| +} |
| + |
| +// needsBreak is prosemirror-view's addTextblockHacks rule: an empty |
| +// textblock, or one whose last child is not text or is text ending in a |
| +// newline. |
| +func needsBreak(pieces []piece) bool { |
| + if len(pieces) == 0 { |
| + return true |
| + } |
| + last := pieces[len(pieces)-1] |
| + return !last.text || strings.HasSuffix(last.html, "\n") |
| +} |
diff --git a/internal/mapa/markup/markup_test.go b/internal/mapa/markup/markup_test.go| index a39512c..5989a44 100644 |
| --- a/internal/mapa/markup/markup_test.go |
| +++ b/internal/mapa/markup/markup_test.go |
| @@ -84,7 +84,8 @@ func (fixed) Resolve(target, anchor string) Resolution { |
| case "2026-02-30": |
| return Resolution{Broken: true} |
| } |
| - return Resolution{Href: "/p/1", BlockGone: anchor == "gone"} |
| + return Resolution{Href: "/p/1", Title: target, |
| + BlockGone: anchor == "gone"} |
| } |
| func TestRenderEscapesEverythingAPersonTyped(t *testing.T) { |
| @@ -101,25 +102,71 @@ func TestRenderEscapesEverythingAPersonTyped(t *testing.T) { |
| } |
| func TestRenderDrawsLinksBlocksAndLists(t *testing.T) { |
| - got := string(Render(Parse(sample+"\n\n[[Pending]] [[2026-02-30]] "+ |
| + got := string(RenderPage(Parse(sample+"\n\n[[Pending]] [[2026-02-30]] "+ |
| "[[Acme#^gone]] **bold** *soft* [web](https://example.com)"), |
| fixed{})) |
| for _, want := range []string{ |
| `<h2>Acme</h2>`, |
| - `<a href="/p/1" mapa-link>the robots</a>`, |
| - `<ul><li>first item</li><li>`, |
| - `<ul><li id="b-n1">nested item</li></ul>`, |
| + `<a href="/p/1" mapa-chip="team" aria-label="the robots, team page">`, |
| + `<ul><li><p>first item</p></li><li>`, |
| + `<ul><li id="b-n1"><p>nested item</p></li></ul>`, |
| `<pre><code>fmt.Println("[[Not a link]]")</code></pre>`, |
| `<p id="b-p1">A paragraph</p>`, |
| - `mapa-link="pending">Pending</a>`, |
| - `<span mapa-link="broken">2026-02-30</span>`, |
| - `(block removed)`, `<strong>bold</strong>`, `<em>soft</em>`, |
| + `mapa-chip="pending" aria-label="Pending, page not written yet">`, |
| + `<span mapa-chip="broken">2026-02-30</span>`, |
| + `mapa-chip="block gone"`, `block removed</span></a>`, |
| + `<strong>bold</strong>`, `<em>soft</em>`, |
| `<a href="https://example.com" rel="noopener noreferrer">web</a>`, |
| } { |
| if !strings.Contains(got, want) { |
| t.Errorf("missing %q in:\n%s", want, got) |
| } |
| } |
| + if strings.Contains(got, ">\n<") { |
| + t.Errorf("text between blocks:\n%s", got) |
| + } |
| +} |
| + |
| +// TestEmphasisIsPinned is spec §5's table: the one fix, and three cases |
| +// that keep the HTML they had. |
| +func TestEmphasisIsPinned(t *testing.T) { |
| + for source, want := range map[string]string{ |
| + "*a **b** c*": "<p><em>a <strong>b</strong> c</em></p>", |
| + "***x***": "<p><em><strong>x</strong></em></p>", |
| + "**a** ***b*** **c**": "<p><strong>a</strong> " + |
| + "<em><strong>b</strong></em> <strong>c</strong></p>", |
| + "**a *b* c**": "<p>**a <em>b</em> c**</p>", |
| + } { |
| + if got := string(Render(Parse(source), fixed{})); got != want { |
| + t.Errorf("%q drew %q, want %q", source, got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestListsChangeKindAndKeepNesting(t *testing.T) { |
| + body := "- a\n1. b\n - c\n more\n 2) d\n- [x] e\n- [ ] f" |
| + want := "<ul><li><p>a</p></li></ul><ol><li><p>b</p>" + |
| + "<ul><li><p>c<br>more</p></li></ul><ol><li><p>d</p></li></ol>" + |
| + `</li></ol><ul><li mapa-task="done"><p>e</p></li>` + |
| + `<li mapa-task="open"><p>f</p></li></ul>` + EmptyParagraph |
| + if got := string(RenderPage(Parse(body), fixed{})); got != want { |
| + t.Fatalf("drew\n%s\nwant\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestRenderPageEndsInAParagraph(t *testing.T) { |
| + for body, want := range map[string]string{ |
| + "": EmptyParagraph, |
| + "a": "<p>a</p>", |
| + "---": "<hr>" + EmptyParagraph, |
| + "# A\n\nb": "<h2>A</h2><p>b</p>", |
| + "- ": "<ul><li><p>" + trailingBreak + "</p></li></ul>" + |
| + EmptyParagraph, |
| + } { |
| + if got := string(RenderPage(Parse(body), fixed{})); got != want { |
| + t.Errorf("%q drew %q, want %q", body, got, want) |
| + } |
| + } |
| } |
| func TestDates(t *testing.T) { |
diff --git a/internal/mapa/markup/render.go b/internal/mapa/markup/render.go| index 90c476c..10f1f84 100644 |
| --- a/internal/mapa/markup/render.go |
| +++ b/internal/mapa/markup/render.go |
| @@ -26,6 +26,12 @@ type Resolution struct { |
| // BlockGone is a block link whose block was deleted: it resolves to |
| // the page, with a note (§3.2). |
| BlockGone bool |
| + // Title is the team page's current title, when the link names one. |
| + Title string |
| + // Alias marks a link that reached its page through an old title. |
| + Alias bool |
| + // Day is a date link's day in words, from DayLabel. |
| + Day string |
| } |
| var ( |
| @@ -34,8 +40,9 @@ var ( |
| taskBox = regexp.MustCompile(`^\[([ xX])\][ \t]+`) |
| ) |
| -// Render draws blocks as HTML. Every byte a person typed is escaped; |
| -// nothing they wrote is ever passed through as markup. |
| +// Render draws blocks as HTML: exactly the DOM the editor's schema draws |
| +// for them (spec §5), with no text between blocks. Every byte a person |
| +// typed is escaped; nothing they wrote is ever passed through as markup. |
| func Render(blocks []Block, resolver Resolver) template.HTML { |
| var out strings.Builder |
| for index := 0; index < len(blocks); { |
| @@ -49,16 +56,26 @@ func Render(blocks []Block, resolver Resolver) template.HTML { |
| return template.HTML(out.String()) |
| } |
| +// RenderPage draws a whole body. When its last block is not a paragraph, |
| +// or it has none, it ends with the editor's empty paragraph (§6.2). |
| +func RenderPage(blocks []Block, resolver Resolver) template.HTML { |
| + drawn := Render(blocks, resolver) |
| + if len(blocks) == 0 || blocks[len(blocks)-1].Kind != Paragraph { |
| + drawn += EmptyParagraph |
| + } |
| + return drawn |
| +} |
| + |
| func renderBlock(out *strings.Builder, block Block, resolver Resolver) { |
| id := blockID(block) |
| switch block.Kind { |
| case Heading: |
| - level := strconv.Itoa(min(block.Level+1, 6)) |
| - out.WriteString("<h" + level + id + ">" + |
| - inline(Strip(block), resolver) + "</h" + level + ">") |
| + tag := "h" + strconv.Itoa(min(block.Level+1, 6)) |
| + out.WriteString("<" + tag + id + ">" + |
| + inline(Strip(block), resolver) + "</" + tag + ">") |
| case Code: |
| - out.WriteString("<pre><code>" + html.EscapeString(codeText( |
| - block.Source)) + "</code></pre>") |
| + out.WriteString("<pre><code>" + codeHTML(codeText(block.Source)) + |
| + "</code></pre>") |
| case Rule: |
| out.WriteString("<hr>") |
| case Quote: |
| @@ -68,38 +85,55 @@ func renderBlock(out *strings.Builder, block Block, resolver Resolver) { |
| out.WriteString("<p" + id + ">" + inline(Strip(block), resolver) + |
| "</p>") |
| } |
| - out.WriteString("\n") |
| +} |
| + |
| +// codeHTML is a code block's text, with the view's trailing break when |
| +// it is empty or ends in a newline. |
| +func codeHTML(text string) string { |
| + if text == "" || strings.HasSuffix(text, "\n") { |
| + return html.EscapeString(text) + trailingBreak |
| + } |
| + return html.EscapeString(text) |
| } |
| // renderList draws the run of items starting at start, nesting by level, |
| -// and returns the index after the run. |
| +// and returns the index after the run. A level skip nests one deep, and |
| +// an item of the other kind (bullet or numbered) starts a new list at |
| +// its depth. |
| func renderList(out *strings.Builder, blocks []Block, start int, |
| resolver Resolver) int { |
| - var open []string // the closing tag of each list still open |
| + var open []string // the tag of each list still open, outermost first |
| index := start |
| for ; index < len(blocks) && blocks[index].Kind == Item; index++ { |
| item := blocks[index] |
| depth := min(item.Level, len(open)) |
| - for len(open) > depth+1 { |
| - out.WriteString("</li>" + open[len(open)-1]) |
| - open = open[:len(open)-1] |
| + open = closeLists(out, open, depth+1) |
| + tag := listTag(item) |
| + if len(open) == depth+1 && open[depth] != tag { |
| + open = closeLists(out, open, depth) |
| } |
| if len(open) == depth+1 { |
| out.WriteString("</li>") |
| } else { |
| - tag := listTag(item) |
| out.WriteString("<" + tag + ">") |
| - open = append(open, "</"+tag+">") |
| + open = append(open, tag) |
| } |
| - out.WriteString("<li" + blockID(item) + ">" + itemBody(item, |
| - resolver)) |
| + task, body := itemParts(item, resolver) |
| + out.WriteString("<li" + blockID(item) + task + "><p>" + body + |
| + "</p>") |
| } |
| - for len(open) > 0 { |
| - out.WriteString("</li>" + open[len(open)-1]) |
| + closeLists(out, open, 0) |
| + return index |
| +} |
| + |
| +// closeLists closes open lists, each with its last item, until depth |
| +// remain. |
| +func closeLists(out *strings.Builder, open []string, depth int) []string { |
| + for len(open) > depth { |
| + out.WriteString("</li></" + open[len(open)-1] + ">") |
| open = open[:len(open)-1] |
| } |
| - out.WriteString("\n") |
| - return index |
| + return open |
| } |
| func listTag(item Block) string { |
| @@ -109,17 +143,20 @@ func listTag(item Block) string { |
| return "ul" |
| } |
| -func itemBody(item Block, resolver Resolver) string { |
| +// itemParts is a list item's task attribute, if it is a task, and its |
| +// text. The box is drawn by CSS from the attribute: the item's paragraph |
| +// is the editor's textblock, and holds only the text. |
| +func itemParts(item Block, resolver Resolver) (string, string) { |
| text := Strip(item) |
| match := taskBox.FindStringSubmatch(text) |
| if match == nil { |
| - return inline(text, resolver) |
| + return "", inline(text, resolver) |
| } |
| - box := `<input type="checkbox" disabled` |
| - if match[1] != " " { |
| - box += " checked" |
| + state := ` mapa-task="done"` |
| + if match[1] == " " { |
| + state = ` mapa-task="open"` |
| } |
| - return box + "> " + inline(text[len(match[0]):], resolver) |
| + return state, inline(text[len(match[0]):], resolver) |
| } |
| func blockID(block Block) string { |
| @@ -133,60 +170,6 @@ func blockID(block Block) string { |
| // ^id marker taken off, but its links and emphasis kept. |
| func Strip(block Block) string { return Plain(block) } |
| -func inline(text string, resolver Resolver) string { |
| - var out strings.Builder |
| - for _, span := range Spans(text) { |
| - out.WriteString(renderSpan(span, resolver)) |
| - } |
| - return out.String() |
| -} |
| - |
| -func renderSpan(span Span, resolver Resolver) string { |
| - switch span.Kind { |
| - case InCode: |
| - return "<code>" + html.EscapeString(span.Text) + "</code>" |
| - case WebLink: |
| - return `<a href="` + html.EscapeString(span.URL) + |
| - `" rel="noopener noreferrer">` + html.EscapeString(span.Text) + |
| - "</a>" |
| - case WikiLink: |
| - return renderWiki(span, resolver) |
| - case Embed: |
| - return "<span mapa-embed>" + html.EscapeString(span.Text) + "</span>" |
| - } |
| - return words(span.Text) |
| -} |
| - |
| -// words is escaped text with emphasis and line breaks drawn. |
| -func words(text string) string { |
| - escaped := html.EscapeString(text) |
| - escaped = strong.ReplaceAllString(escaped, "<strong>$1</strong>") |
| - escaped = emphasis.ReplaceAllString(escaped, "<em>$1</em>") |
| - return strings.ReplaceAll(escaped, "\n", "<br>") |
| -} |
| - |
| -func renderWiki(span Span, resolver Resolver) string { |
| - shown := span.Text |
| - if shown == "" { |
| - shown = span.Target |
| - } |
| - answer := resolver.Resolve(span.Target, span.Anchor) |
| - if answer.Broken { |
| - return `<span mapa-link="broken">` + html.EscapeString(shown) + |
| - "</span>" |
| - } |
| - kind := "" |
| - if answer.Pending { |
| - kind = `="pending"` |
| - } |
| - link := `<a href="` + html.EscapeString(answer.Href) + `" mapa-link` + |
| - kind + ">" + html.EscapeString(shown) + "</a>" |
| - if answer.BlockGone { |
| - link += " <small mapa-note>(block removed)</small>" |
| - } |
| - return link |
| -} |
| - |
| // Words is a block's text as a reader sees it: links as their shown |
| // text, code as its text, embeds dropped. Search reads it. |
| func Words(block Block) string { |
diff --git a/internal/mapa/markup/spans.go b/internal/mapa/markup/spans.go| index 9419a99..3d9d7a3 100644 |
| --- a/internal/mapa/markup/spans.go |
| +++ b/internal/mapa/markup/spans.go |
| @@ -164,7 +164,9 @@ func wordStart(text string, index int) bool { |
| // SafeURL is an author link Mapa will make clickable: http, https or |
| // mailto, with a host for the web ones. Everything else (javascript:, |
| -// data: and the rest) stays text. |
| +// data: and the rest) stays text. So does a bracketed IP literal |
| +// (http://[::1]/): editor/markup.mjs ports this rule, and that is the one |
| +// part of net/url it would have to reimplement whole (netip's parser). |
| func SafeURL(raw string) bool { |
| parsed, err := url.Parse(raw) |
| if err != nil { |
| @@ -172,7 +174,7 @@ func SafeURL(raw string) bool { |
| } |
| switch strings.ToLower(parsed.Scheme) { |
| case "http", "https": |
| - return parsed.Host != "" |
| + return parsed.Host != "" && !strings.HasPrefix(parsed.Host, "[") |
| case "mailto": |
| return parsed.Opaque != "" |
| } |
diff --git a/internal/mapa/pages_test.go b/internal/mapa/pages_test.go| index f576e02..8d71d1e 100644 |
| --- a/internal/mapa/pages_test.go |
| +++ b/internal/mapa/pages_test.go |
| @@ -90,9 +90,11 @@ func TestGate3RenamesKeepLinksAndRevisionsHonest(t *testing.T) { |
| t.Fatalf("A's S3 bytes changed: %q", afterRecord.S3(KindPage)) |
| } |
| page := seen(t, index, Scope{}, linker.ID) |
| - if !strings.Contains(string(page.HTML), |
| - `href="`+PagePath(target)+`" mapa-link>Acme</a>`) { |
| - t.Fatalf("[[Acme]] no longer resolves to B:\n%s", page.HTML) |
| + if !strings.Contains(string(page.HTML), `href="`+PagePath(target)+ |
| + `" mapa-chip="team" title="Now called Acme Ltd" `+ |
| + `aria-label="Acme, now called Acme Ltd">`) { |
| + t.Fatalf("[[Acme]] no longer resolves to B through its alias:\n%s", |
| + page.HTML) |
| } |
| renamed := seen(t, index, Scope{}, target.ID) |
| if renamed.Title != "Acme Ltd" || len(renamed.Aliases) != 1 || |
| @@ -129,7 +131,8 @@ func TestFreeingAnAliasMakesItsLinksPendingThenRebinds(t *testing.T) { |
| t.Fatal(err) |
| } |
| html := string(seen(t, index, Scope{}, linker.ID).HTML) |
| - if !strings.Contains(html, `mapa-link="pending">Acme</a>`) { |
| + if !strings.Contains(html, |
| + `mapa-chip="pending" aria-label="Acme, page not written yet">`) { |
| t.Fatalf("a freed alias's link is not pending:\n%s", html) |
| } |
| fresh := mustCreate(t, store, "acme", "") |
diff --git a/internal/mapa/views.go b/internal/mapa/views.go| index 1326ddb..f5097fe 100644 |
| --- a/internal/mapa/views.go |
| +++ b/internal/mapa/views.go |
| @@ -58,7 +58,7 @@ func (index *Index) View(ctx context.Context, scope Scope, |
| } |
| draw := resolver{index: index, scope: scope} |
| view := page.summary() |
| - view.HTML = markup.Render(page.blocks, draw) |
| + view.HTML = markup.RenderPage(page.blocks, draw) |
| view.Backlinks = index.backlinks(scope, page) |
| if page.team() { |
| view.Mentions = index.mentions(scope, page, view.Backlinks) |
| @@ -228,7 +228,7 @@ func (index *Index) Day(ctx context.Context, scope Scope, |
| if pageID, ok := index.days[scope.Member][date]; ok && scope.Member != 0 { |
| page := index.pages[pageID] |
| view := page.summary() |
| - view.HTML = markup.Render(page.blocks, resolver{index, scope}) |
| + view.HTML = markup.RenderPage(page.blocks, resolver{index, scope}) |
| view.Backlinks = index.backlinks(scope, page) |
| return view, true, nil |
| } |
| @@ -269,5 +269,5 @@ func (index *Index) Preview(ctx context.Context, scope Scope, |
| } |
| index.mutex.RLock() |
| defer index.mutex.RUnlock() |
| - return markup.Render(markup.Parse(body), resolver{index, scope}), nil |
| + return markup.RenderPage(markup.Parse(body), resolver{index, scope}), nil |
| } |
diff --git a/internal/mapatest/daily_test.go b/internal/mapatest/daily_test.go| index 6e0e7aa..d573b56 100644 |
| --- a/internal/mapatest/daily_test.go |
| +++ b/internal/mapatest/daily_test.go |
| @@ -56,9 +56,12 @@ func TestADateLinkOpensTheReadersOwnNote(t *testing.T) { |
| rig.post(t, Bo, "/d/2026-11-12", url.Values{"body": {"Bo writes here."}}) |
| plan := rig.createPage(t, Ana, "Plan", "See [[2026-11-12]].") |
| for _, who := range []string{Ana, Bo} { |
| - if !strings.Contains(rig.page(t, who, plan), |
| - `href="/d/2026-11-12" mapa-link>2026-11-12</a>`) { |
| - t.Fatal("the date link does not lead to /d/2026-11-12") |
| + drawn := rig.page(t, who, plan) |
| + if !strings.Contains(drawn, |
| + `href="/d/2026-11-12" mapa-chip="date"`) || |
| + !strings.Contains(drawn, `</svg>Thursday 12 November`) { |
| + t.Fatal("the date link does not lead to /d/2026-11-12, " + |
| + "named in words") |
| } |
| } |
| anaDay := rig.page(t, Ana, "/d/2026-11-12") |
diff --git a/internal/mapatest/pages_test.go b/internal/mapatest/pages_test.go| index 1550089..bf2c678 100644 |
| --- a/internal/mapatest/pages_test.go |
| +++ b/internal/mapatest/pages_test.go |
| @@ -62,7 +62,7 @@ func TestAPendingLinkOffersToCreateItsPage(t *testing.T) { |
| rig := newRig(t) |
| notes := rig.createPage(t, Ana, "Notes", "Ask about [[Halden Robotics]].") |
| body := rig.page(t, Bo, notes) |
| - href := `href="/new?title=Halden+Robotics" mapa-link="pending"` |
| + href := `href="/new?title=Halden+Robotics" mapa-chip="pending"` |
| if !strings.Contains(body, href) { |
| t.Fatalf("no pending link:\n%s", body) |
| } |