Skip to content

Commit cb57e6d

Browse files
committed
Blank lines between blocks vanish when CLI content is edited
The Markdown pipeline separated top-level blocks with a bare <br>, and the raw-HTML passthrough inserted one between adjacent paragraphs. A bare top-level <br> is not a block in Basecamp's editor document model, so the editor discards it on import: the content displays correctly until someone opens it, makes any edit and saves, at which point every blank line is gone for good. Emit the editor's own separator instead — an empty paragraph — which survives the round trip untouched. Inside a blockquote the break stays a <br>: there it is inline content, which the editor keeps. Bare <br> separators arriving through the HTML passthrough are rewritten to the durable form for the same reason. Verified against lexxy v0.9.29 (the editor bc3 ships): a document with four separators comes back with zero after an edit before this change, and with all four after it.
1 parent 742a9c2 commit cb57e6d

2 files changed

Lines changed: 83 additions & 43 deletions

File tree

‎internal/richtext/richtext.go‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ var mdConverter = goldmark.New(
134134
),
135135
)
136136

137-
// TrixBreak is a custom block node that renders as <br>\n for Trix paragraph spacing.
137+
// paragraphSeparator is the blank line Basecamp's editor itself stores between
138+
// two blocks. A bare top-level <br> is not a block in the editor's document
139+
// model, so it is discarded the first time someone edits the content and the
140+
// spacing disappears; an empty paragraph survives the round trip.
141+
const paragraphSeparator = "<p><br></p>"
142+
143+
// TrixBreak is a custom block node that renders the blank line between blocks:
144+
// an empty paragraph at the top level, a <br> inside a block (see
145+
// renderTrixBreak).
138146
type TrixBreak struct{ ast.BaseBlock }
139147

140148
// KindTrixBreak is the node kind for TrixBreak.
@@ -329,11 +337,18 @@ func (r *trixRenderer) renderFencedCodeBlock(w util.BufWriter, source []byte, no
329337
return ast.WalkContinue, nil
330338
}
331339

332-
func (r *trixRenderer) renderTrixBreak(w util.BufWriter, _ []byte, _ ast.Node, entering bool) (ast.WalkStatus, error) {
340+
// renderTrixBreak emits an empty paragraph for a top-level break and a <br> for
341+
// one inside a block. Only the top level needs a block-level separator:
342+
// a <br> nested in a blockquote is inline content, which survives editing.
343+
func (r *trixRenderer) renderTrixBreak(w util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
333344
if !entering {
334345
return ast.WalkContinue, nil
335346
}
336-
_, _ = w.WriteString("<br>\n")
347+
if parent := node.Parent(); parent != nil && parent.Kind() == ast.KindDocument {
348+
_, _ = w.WriteString(paragraphSeparator + "\n")
349+
} else {
350+
_, _ = w.WriteString("<br>\n")
351+
}
337352
return ast.WalkContinue, nil
338353
}
339354

@@ -348,8 +363,8 @@ func (r *trixRenderer) renderEscapedAt(w util.BufWriter, _ []byte, _ ast.Node, e
348363
// MarkdownToHTML converts Markdown text to HTML suitable for Basecamp's rich text fields.
349364
// It uses goldmark with custom AST transformations for Trix editor compatibility.
350365
// If the input already appears to be HTML, it is passed through with existing
351-
// formatting preserved, except that a <br> separator is inserted between
352-
// directly adjacent paragraph blocks (see insertParagraphSeparators).
366+
// formatting preserved, except that a separator is inserted between directly
367+
// adjacent paragraph blocks (see insertParagraphSeparators).
353368
func MarkdownToHTML(md string) string {
354369
if md == "" {
355370
return ""
@@ -370,9 +385,9 @@ func MarkdownToHTML(md string) string {
370385
return strings.TrimSpace(buf.String())
371386
}
372387

373-
// insertParagraphSeparators inserts a <br> between directly adjacent, non-empty
374-
// paragraph blocks so that HTML supplied to the CLI renders with visible
375-
// paragraph spacing.
388+
// insertParagraphSeparators puts an empty separator paragraph between directly
389+
// adjacent, non-empty paragraph blocks so that HTML supplied to the CLI renders
390+
// with visible paragraph spacing.
376391
//
377392
// Basecamp's rich text relies on explicit separator nodes for paragraph
378393
// spacing, not CSS margins: contiguous <p>A</p><p>B</p> renders squished. The
@@ -383,14 +398,14 @@ func MarkdownToHTML(md string) string {
383398
// distinction), so contiguous paragraphs from HTML input are treated as
384399
// separate paragraphs.
385400
//
386-
// The transform is byte-preserving apart from the inserted separators and is
387-
// idempotent: a boundary that already carries a separator — a bare <br> between
388-
// the paragraphs, or an empty separator paragraph (<p><br></p> or <p></p>) on
389-
// either side — is left untouched, so running it on already-separated content
390-
// (including Basecamp editor output) is a no-op. Only directly adjacent <p>
391-
// blocks are separated; anything between them (whitespace excepted), such as a
392-
// heading, list, or attachment, already provides its own break and is left
393-
// alone.
401+
// A boundary already carrying an empty separator paragraph (<p><br></p> or
402+
// <p></p>) on either side is left untouched, so running this on Basecamp editor
403+
// output — or on its own output — is a no-op. A boundary separated only by bare
404+
// <br> tags is rewritten to the separator paragraph: the editor drops those on
405+
// the first edit, so leaving them would keep the spacing they express fragile.
406+
// Only directly adjacent <p> blocks are separated; anything else between them,
407+
// such as a heading, list, or attachment, already provides its own break and is
408+
// left alone.
394409
func insertParagraphSeparators(s string) string {
395410
locs := reP.FindAllStringIndex(s, -1)
396411
if len(locs) < 2 {
@@ -416,21 +431,28 @@ func insertParagraphSeparators(s string) string {
416431

417432
nextStart := locs[i+1][0]
418433
gap := s[end:nextStart]
419-
if !empty[i] && !empty[i+1] && strings.TrimSpace(gap) == "" {
420-
b.WriteString(gap)
421-
b.WriteString("<br>")
434+
if !empty[i] && !empty[i+1] && isSeparatorGap(gap) {
435+
b.WriteString(reBR.ReplaceAllString(gap, ""))
436+
b.WriteString(paragraphSeparator)
422437
cursor = nextStart
423438
}
424439
}
425440
b.WriteString(s[cursor:])
426441
return b.String()
427442
}
428443

444+
// isSeparatorGap reports whether the markup between two paragraph blocks holds
445+
// nothing but whitespace and bare <br> tags — i.e. whatever spacing it expresses
446+
// can be replaced by a separator paragraph.
447+
func isSeparatorGap(gap string) bool {
448+
return strings.TrimSpace(reBR.ReplaceAllString(gap, "")) == ""
449+
}
450+
429451
// isEmptyParagraph reports whether a <p>...</p> block has no visible content —
430452
// i.e. it is empty or contains only <br> tags and whitespace, including
431453
// non-breaking-space entities (&nbsp;, &#160;, &#xa0;) that rich text editors
432454
// commonly use for blank separator lines. Such paragraphs act as separators, so
433-
// no additional <br> is inserted adjacent to them.
455+
// no additional one is inserted adjacent to them.
434456
func isEmptyParagraph(block string) bool {
435457
m := reP.FindStringSubmatch(block)
436458
if m == nil {

‎internal/richtext/richtext_test.go‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestMarkdownToHTML(t *testing.T) {
8585
{
8686
name: "list followed by blank line then paragraph",
8787
input: "- Item 1\n- Item 2\n\nFollowing paragraph.",
88-
expected: "<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\n<br>\n<p>Following paragraph.</p>",
88+
expected: "<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\n<p><br></p>\n<p>Following paragraph.</p>",
8989
},
9090
{
9191
// CommonMark §5.4: "After" is a lazy continuation of the second list item.
@@ -128,7 +128,7 @@ func TestMarkdownToHTML(t *testing.T) {
128128
{
129129
name: "mixed formatting",
130130
input: "# Title\n\nThis is **bold** and *italic* and `code`.",
131-
expected: "<h1>Title</h1>\n<br>\n<p>This is <strong>bold</strong> and <em>italic</em> and <code>code</code>.</p>",
131+
expected: "<h1>Title</h1>\n<p><br></p>\n<p>This is <strong>bold</strong> and <em>italic</em> and <code>code</code>.</p>",
132132
},
133133
{
134134
name: "escapes HTML",
@@ -143,12 +143,12 @@ func TestMarkdownToHTML(t *testing.T) {
143143
{
144144
name: "paragraph spacing with blank line",
145145
input: "First paragraph\n\nSecond paragraph",
146-
expected: "<p>First paragraph</p>\n<br>\n<p>Second paragraph</p>",
146+
expected: "<p>First paragraph</p>\n<p><br></p>\n<p>Second paragraph</p>",
147147
},
148148
{
149149
name: "multiple blank lines collapse to one break",
150150
input: "First\n\n\n\nSecond",
151-
expected: "<p>First</p>\n<br>\n<p>Second</p>",
151+
expected: "<p>First</p>\n<p><br></p>\n<p>Second</p>",
152152
},
153153
{
154154
name: "consecutive lines join into one paragraph",
@@ -158,12 +158,12 @@ func TestMarkdownToHTML(t *testing.T) {
158158
{
159159
name: "blank line before list",
160160
input: "Intro\n\n- Item 1\n- Item 2",
161-
expected: "<p>Intro</p>\n<br>\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>",
161+
expected: "<p>Intro</p>\n<p><br></p>\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>",
162162
},
163163
{
164164
name: "blank line before code block",
165165
input: "Intro\n\n```\ncode\n```",
166-
expected: "<p>Intro</p>\n<br>\n<pre><code>code\n</code></pre>",
166+
expected: "<p>Intro</p>\n<p><br></p>\n<pre><code>code\n</code></pre>",
167167
},
168168
{
169169
name: "leading blank lines ignored",
@@ -173,12 +173,12 @@ func TestMarkdownToHTML(t *testing.T) {
173173
{
174174
name: "blank line before blockquote",
175175
input: "Intro\n\n> A quote",
176-
expected: "<p>Intro</p>\n<br>\n<blockquote>A quote</blockquote>",
176+
expected: "<p>Intro</p>\n<p><br></p>\n<blockquote>A quote</blockquote>",
177177
},
178178
{
179179
name: "blank line before horizontal rule",
180180
input: "Intro\n\n---",
181-
expected: "<p>Intro</p>\n<br>\n<hr>",
181+
expected: "<p>Intro</p>\n<p><br></p>\n<hr>",
182182
},
183183
{
184184
name: "heading flushes accumulated paragraph",
@@ -214,7 +214,7 @@ func TestMarkdownToHTML(t *testing.T) {
214214
{
215215
name: "fenced code block containing HTML tags is converted",
216216
input: "intro\n\n```\n<div>hello</div>\n```",
217-
expected: "<p>intro</p>\n<br>\n<pre><code>&lt;div&gt;hello&lt;/div&gt;\n</code></pre>",
217+
expected: "<p>intro</p>\n<p><br></p>\n<pre><code>&lt;div&gt;hello&lt;/div&gt;\n</code></pre>",
218218
},
219219
}
220220

@@ -2223,30 +2223,30 @@ func TestMarkdownToHTMLInsertsParagraphSeparators(t *testing.T) {
22232223
{
22242224
name: "two contiguous paragraphs get a separator",
22252225
input: "<p>Line 1</p><p>Line 2</p>",
2226-
expected: "<p>Line 1</p><br><p>Line 2</p>",
2226+
expected: "<p>Line 1</p><p><br></p><p>Line 2</p>",
22272227
},
22282228
{
22292229
name: "three contiguous paragraphs get separators between each",
22302230
input: "<p>A</p><p>B</p><p>C</p>",
2231-
expected: "<p>A</p><br><p>B</p><br><p>C</p>",
2231+
expected: "<p>A</p><p><br></p><p>B</p><p><br></p><p>C</p>",
22322232
},
22332233
{
22342234
name: "paragraphs with attributes are separated",
22352235
input: `<p dir="auto">A</p><p dir="auto">B</p>`,
2236-
expected: `<p dir="auto">A</p><br><p dir="auto">B</p>`,
2236+
expected: `<p dir="auto">A</p><p><br></p><p dir="auto">B</p>`,
22372237
},
22382238
{
22392239
name: "whitespace-only gap is preserved and separator added",
22402240
input: "<p>A</p>\n<p>B</p>",
2241-
expected: "<p>A</p>\n<br><p>B</p>",
2241+
expected: "<p>A</p>\n<p><br></p><p>B</p>",
22422242
},
22432243
{
2244-
name: "existing bare br separator is left untouched (idempotent)",
2244+
name: "bare br separator becomes an editor-durable separator paragraph",
22452245
input: "<p>A</p><br><p>B</p>",
2246-
expected: "<p>A</p><br><p>B</p>",
2246+
expected: "<p>A</p><p><br></p><p>B</p>",
22472247
},
22482248
{
2249-
name: "empty separator paragraph is left untouched (Lexxy canonical)",
2249+
name: "separator paragraph is left untouched (editor canonical)",
22502250
input: "<p>A</p><p><br></p><p>B</p>",
22512251
expected: "<p>A</p><p><br></p><p>B</p>",
22522252
},
@@ -2293,17 +2293,17 @@ func TestMarkdownToHTMLInsertsParagraphSeparators(t *testing.T) {
22932293
{
22942294
name: "paragraph with inline br is still non-empty and separated",
22952295
input: "<p>A<br>C</p><p>B</p>",
2296-
expected: "<p>A<br>C</p><br><p>B</p>",
2296+
expected: "<p>A<br>C</p><p><br></p><p>B</p>",
22972297
},
22982298
{
22992299
name: "leading empty paragraph is left alone",
23002300
input: "<p></p><p>A</p>",
23012301
expected: "<p></p><p>A</p>",
23022302
},
23032303
{
2304-
name: "mix of separated and contiguous only fills the gap that lacks a separator",
2304+
name: "mix of contiguous and bare-br boundaries both get separator paragraphs",
23052305
input: "<p>A</p><p>B</p><br><p>C</p>",
2306-
expected: "<p>A</p><br><p>B</p><br><p>C</p>",
2306+
expected: "<p>A</p><p><br></p><p>B</p><p><br></p><p>C</p>",
23072307
},
23082308
}
23092309

@@ -2325,6 +2325,7 @@ func TestMarkdownToHTMLParagraphSeparatorsIdempotent(t *testing.T) {
23252325
`<p dir="auto">A</p><p dir="auto">B</p>`,
23262326
"<p>A</p>\n<p>B</p>",
23272327
"<p>A</p><p><br></p><p>B</p>",
2328+
"<p>A</p><br><p>B</p>",
23282329
"<p>A</p><h3>H</h3><p>B</p>",
23292330
}
23302331

@@ -2345,10 +2346,27 @@ func TestMarkdownToHTMLParagraphSeparatorsMatchMarkdownPath(t *testing.T) {
23452346
fromMarkdown := MarkdownToHTML("Line 1\n\nLine 2")
23462347
fromHTML := MarkdownToHTML("<p>Line 1</p><p>Line 2</p>")
23472348

2348-
if !strings.Contains(fromMarkdown, "<br>") {
2349-
t.Fatalf("markdown path unexpectedly produced no <br>: %q", fromMarkdown)
2349+
if fromMarkdown != "<p>Line 1</p>\n<p><br></p>\n<p>Line 2</p>" {
2350+
t.Errorf("markdown path = %q", fromMarkdown)
2351+
}
2352+
if fromHTML != "<p>Line 1</p><p><br></p><p>Line 2</p>" {
2353+
t.Errorf("HTML path = %q, want %q", fromHTML, "<p>Line 1</p><p><br></p><p>Line 2</p>")
2354+
}
2355+
}
2356+
2357+
// Basecamp's editor discards a bare top-level <br> the first time the content is
2358+
// edited, collapsing the spacing. No blank line between blocks may rely on one.
2359+
func TestMarkdownToHTMLEmitsNoBareTopLevelBreaks(t *testing.T) {
2360+
markdown := "Para one.\n\nPara two.\n\n## Heading\n\n- a\n- b\n\n> A quote\n\n```\ncode\n```\n\n---\n\nClosing."
2361+
2362+
html := MarkdownToHTML(markdown)
2363+
2364+
for _, block := range []string{"<p>", "<h2>", "<ul>", "<blockquote>", "<pre>", "<hr>"} {
2365+
if strings.Contains(html, "<br>\n"+block) {
2366+
t.Errorf("bare <br> separator before %s in %q", block, html)
2367+
}
23502368
}
2351-
if fromHTML != "<p>Line 1</p><br><p>Line 2</p>" {
2352-
t.Errorf("HTML path = %q, want %q", fromHTML, "<p>Line 1</p><br><p>Line 2</p>")
2369+
if want := strings.Count(markdown, "\n\n"); strings.Count(html, paragraphSeparator) != want {
2370+
t.Errorf("got %d separator paragraphs, want %d in %q", strings.Count(html, paragraphSeparator), want, html)
23532371
}
23542372
}

0 commit comments

Comments
 (0)