History M4: one empty paragraph, at the end of a version's diff
Unchanged runs were drawn through Preview, the page renderer, which ends every body whose last block is not a paragraph with the editor's empty paragraph: so a stray <p><br class="ProseMirror-trailingBreak"> </p> appeared after each kept list, mid-text. Kept runs are now drawn with markup.Render in the viewer's scope (Index drawRun), and the empty paragraph is added once, at the end of the whole diff, when the last block drawn is not a paragraph: RenderPage's rule. A whole version (the first, or one whose predecessor aged out) is drawn the same way. With nothing drawn there is no empty paragraph, so an empty version reads "This version is empty.", as the template intends.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+129
−24
internal/mapa/diff.go+47 −15internal/mapa/diff_test.go+41 −3internal/mapa/handlers_history.go+5 −2internal/mapa/history_view.go+16 −4internal/mapatest/history_test.go+20 −0
diff --git a/internal/mapa/diff.go b/internal/mapa/diff.go| index 7dc71b7..0d301db 100644 |
| --- a/internal/mapa/diff.go |
| +++ b/internal/mapa/diff.go |
| @@ -203,20 +203,26 @@ func lcs(rows, columns int, same func(row, column int) bool) []int { |
| return matched |
| } |
| +// drawRun draws a run of a version's blocks as the page draws them, in |
| +// the viewer's scope (markup.Render), with no empty paragraph after. |
| +type drawRun func(blocks []markup.Block) (template.HTML, error) |
| + |
| // diffHTML draws this version, to, with what changed from the version |
| -// before it marked. Runs of unchanged blocks go through draw, the page's |
| -// own renderer with the viewer's scope; changed blocks are drawn here. |
| -func diffHTML(from, to []markup.Block, |
| - draw func(string) (template.HTML, error)) (template.HTML, error) { |
| +// before it marked. Runs of unchanged blocks go through draw; changed |
| +// blocks are drawn here. The editor's empty paragraph comes once, at the |
| +// end, as the page's renderer decides it (pageEnd). |
| +func diffHTML(from, to []markup.Block, draw drawRun) (template.HTML, |
| + error) { |
| var out strings.Builder |
| var kept []markup.Block |
| + var last *markup.Block |
| flush := func() error { |
| if len(kept) == 0 { |
| return nil |
| } |
| - drawn, err := draw(markup.Join(kept)) |
| + drawn, err := draw(kept) |
| out.WriteString(string(drawn)) |
| - kept = nil |
| + last, kept = &kept[len(kept)-1], nil |
| return err |
| } |
| for _, change := range diffBlocks(from, to) { |
| @@ -227,27 +233,53 @@ func diffHTML(from, to []markup.Block, |
| if err := flush(); err != nil { |
| return "", err |
| } |
| - out.WriteString(changedBlock(change, from, to)) |
| + block := blockOf(change, from, to) |
| + out.WriteString(changedBlock(change, block, from)) |
| + last = &block |
| } |
| - if err := flush(); err != nil { |
| - return "", err |
| + err := flush() |
| + return template.HTML(out.String() + pageEnd(last)), err |
| +} |
| + |
| +// pageEnd is the editor's empty paragraph after the last block drawn, |
| +// when it is not a paragraph, as markup.RenderPage ends a body. With |
| +// nothing drawn there is none, so the version reads as empty. |
| +func pageEnd(last *markup.Block) string { |
| + if last == nil || last.Kind == markup.Paragraph { |
| + return "" |
| + } |
| + return markup.EmptyParagraph |
| +} |
| + |
| +// drawPage is a whole body drawn as its page, with its end. |
| +func drawPage(body string, draw drawRun) (template.HTML, error) { |
| + blocks := markup.Parse(body) |
| + if len(blocks) == 0 { |
| + return "", nil |
| + } |
| + drawn, err := draw(blocks) |
| + return drawn + template.HTML(pageEnd(&blocks[len(blocks)-1])), err |
| +} |
| + |
| +// blockOf is the block a change draws: the one removed, or this |
| +// version's. |
| +func blockOf(change blockChange, from, to []markup.Block) markup.Block { |
| + if change.Kind == blockRemoved { |
| + return from[change.From] |
| } |
| - return template.HTML(out.String()), nil |
| + return to[change.To] |
| } |
| // changedBlock is one added, removed or changed block, as its kind. |
| -func changedBlock(change blockChange, from, to []markup.Block) string { |
| - var block markup.Block |
| +func changedBlock(change blockChange, block markup.Block, |
| + from []markup.Block) string { |
| var inner string |
| switch change.Kind { |
| case blockAdded: |
| - block = to[change.To] |
| inner = drawRuns([]wordRun{{Tag: "ins", Tokens: tokensOf(block)}}) |
| case blockRemoved: |
| - block = from[change.From] |
| inner = drawRuns([]wordRun{{Tag: "del", Tokens: tokensOf(block)}}) |
| default: |
| - block = to[change.To] |
| inner = drawRuns(diffWords(tokensOf(from[change.From]), |
| tokensOf(block))) |
| } |
diff --git a/internal/mapa/diff_test.go b/internal/mapa/diff_test.go| index 8462323..278717a 100644 |
| --- a/internal/mapa/diff_test.go |
| +++ b/internal/mapa/diff_test.go |
| @@ -32,8 +32,20 @@ func TestEditSummaries(t *testing.T) { |
| } |
| } |
| -func plainDraw(body string) (template.HTML, error) { |
| - return template.HTML("[" + template.HTMLEscapeString(body) + "]"), nil |
| +func plainDraw(blocks []markup.Block) (template.HTML, error) { |
| + return template.HTML("[" + template.HTMLEscapeString( |
| + markup.Join(blocks)) + "]"), nil |
| +} |
| + |
| +// noLinks resolves every link nowhere: enough to draw with Render. |
| +type noLinks struct{} |
| + |
| +func (noLinks) Resolve(string, string) markup.Resolution { |
| + return markup.Resolution{Href: "#"} |
| +} |
| + |
| +func renderDraw(blocks []markup.Block) (template.HTML, error) { |
| + return markup.Render(blocks, noLinks{}), nil |
| } |
| func diffOf(t *testing.T, old, body string) string { |
| @@ -50,7 +62,7 @@ func TestTheWordDiffMarksOnlyWhatChanged(t *testing.T) { |
| "Same.\n\n- The window opens in March.") |
| want := `[Same.]<div mapa-diff="changed"><ul mapa-diff-level="0"><li>` + |
| `The window opens in <del>February.</del><ins>March.</ins>` + |
| - "</li></ul></div>\n" |
| + "</li></ul></div>\n" + markup.EmptyParagraph |
| if got != want { |
| t.Fatalf("got %s\nwant %s", got, want) |
| } |
| @@ -137,3 +149,29 @@ func TestRemovedBlocksAreDrawnBeforeAddedOnes(t *testing.T) { |
| t.Fatalf("removed is not drawn before added:\n%s", got) |
| } |
| } |
| + |
| +// Review M4: the editor's empty paragraph is drawn once, at the end of |
| +// the whole diff and only when its last block is not a paragraph; never |
| +// after a kept list in the middle of the text. |
| +func TestTheEmptyParagraphEndsTheDiffOnce(t *testing.T) { |
| + for _, check := range []struct{ old, body string }{ |
| + {"- a\n- b\n\nOld end.", "- a\n- b\n\nA new end entirely."}, |
| + {"- a\n- b\n\nMiddle.\n\n- c", "- a\n- b\n\nMiddle, now.\n\n- c"}, |
| + {"Start.\n\n- a", "Start, now.\n\n- a"}, |
| + } { |
| + drawn, err := diffHTML(markup.Parse(check.old), |
| + markup.Parse(check.body), renderDraw) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + got := string(drawn) |
| + count := strings.Count(got, markup.EmptyParagraph) |
| + ends := strings.HasSuffix(got, markup.EmptyParagraph) |
| + last := markup.Parse(check.body) |
| + wantEnd := last[len(last)-1].Kind != markup.Paragraph |
| + if count > 1 || (count == 1) != ends || ends != wantEnd { |
| + t.Errorf("%q: %d empty paragraphs in\n%s", check.body, count, |
| + got) |
| + } |
| + } |
| +} |
diff --git a/internal/mapa/handlers_history.go b/internal/mapa/handlers_history.go| index 43699da..73fab13 100644 |
| --- a/internal/mapa/handlers_history.go |
| +++ b/internal/mapa/handlers_history.go |
| @@ -6,6 +6,8 @@ import ( |
| "strconv" |
| "github.com/go-chi/chi/v5" |
| + |
| + "amadan.net/oficina/mapa/internal/mapa/markup" |
| ) |
| // History and block links: the page's revisions in the window, one |
| @@ -96,8 +98,9 @@ func (app *app) showVersion(writer http.ResponseWriter, |
| app.historyFragment(writer, request, panel) |
| return |
| } |
| - draw := func(body string) (template.HTML, error) { |
| - return app.index.Preview(request.Context(), ScopeOf(viewer), body) |
| + draw := func(blocks []markup.Block) (template.HTML, error) { |
| + return app.index.drawRun(request.Context(), ScopeOf(viewer), |
| + blocks) |
| } |
| data, err := versionOf(page, chosen, draw) |
| if err != nil { |
diff --git a/internal/mapa/history_view.go b/internal/mapa/history_view.go| index 6641227..a4a90f8 100644 |
| --- a/internal/mapa/history_view.go |
| +++ b/internal/mapa/history_view.go |
| @@ -135,8 +135,8 @@ type versionView struct { |
| // versionOf draws a chosen version: the banner, its title, and its text |
| // with what changed from the version before marked. draw is the page's |
| // own renderer, in the viewer's scope. |
| -func versionOf(page PageView, chosen Chosen, |
| - draw func(string) (template.HTML, error)) (versionView, error) { |
| +func versionOf(page PageView, chosen Chosen, draw drawRun) (versionView, |
| + error) { |
| view := versionView{Page: page, Banner: possessive(chosen.Version.Who) + |
| " version, " + chosen.Version.At.Format("Monday 2 January at 15:04") + |
| ".", Title: versionTitle(page, chosen)} |
| @@ -148,11 +148,11 @@ func versionOf(page PageView, chosen Chosen, |
| markup.Parse(chosen.Record.Body), draw) |
| case chosen.Verb == VerbCreated || chosen.Verb == VerbRestored: |
| view.Note = "The first version." |
| - view.HTML, err = draw(chosen.Record.Body) |
| + view.HTML, err = drawPage(chosen.Record.Body, draw) |
| default: |
| view.Note = "The version before it has aged out, so nothing " + |
| "is marked." |
| - view.HTML, err = draw(chosen.Record.Body) |
| + view.HTML, err = drawPage(chosen.Record.Body, draw) |
| } |
| return view, err |
| } |
| @@ -180,3 +180,15 @@ func versionTitle(page PageView, chosen Chosen) template.HTML { |
| html.EscapeString(chosen.Before.Title) + "</del>", |
| "<ins>" + escaped + "</ins>"}, " ")) |
| } |
| + |
| +// drawRun draws blocks for scope as the page does, with no empty |
| +// paragraph after them: a version's view adds that once, at its end. |
| +func (index *Index) drawRun(ctx context.Context, scope Scope, |
| + blocks []markup.Block) (template.HTML, error) { |
| + if err := index.Sync(ctx); err != nil { |
| + return "", err |
| + } |
| + index.mutex.RLock() |
| + defer index.mutex.RUnlock() |
| + return markup.Render(blocks, resolver{index, scope}), nil |
| +} |
diff --git a/internal/mapatest/history_test.go b/internal/mapatest/history_test.go| index 4582074..26fe220 100644 |
| --- a/internal/mapatest/history_test.go |
| +++ b/internal/mapatest/history_test.go |
| @@ -71,3 +71,23 @@ func TestLinkToThisBlockMintsAMarkerAsAnEdit(t *testing.T) { |
| t.Fatal("a private page offers block markers") |
| } |
| } |
| + |
| +// Review M4: a chosen version's diff draws the editor's empty paragraph |
| +// at most once, at its end; never after a kept list mid-text. |
| +func TestAVersionDiffEndsInOneEmptyParagraph(t *testing.T) { |
| + rig := newRig(t) |
| + moving := rig.clocked(t) |
| + acme := rig.createPage(t, Ana, "Acme", "- One\n- Two\n\nOld end.\n\n"+ |
| + "- Three") |
| + first := revisionOf(t, rig, acme) |
| + moving.now = moving.now.Add(time.Hour) |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"- One\n- Two\n\nA new end entirely.\n\n- Three"}}) |
| + version := rig.page(t, Ana, acme+"/history/"+revisionOf(t, rig, acme)) |
| + empty := `<p><br class="ProseMirror-trailingBreak"></p>` |
| + if strings.Count(version, empty) != 1 || !strings.Contains(version, |
| + "<li><p>Three</p></li></ul>"+empty) { |
| + t.Fatalf("the empty paragraph is not drawn once, at the end:\n%s", |
| + version) |
| + } |
| +} |