High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(text): markdown maxChars rune-cuts the overrunning line instead of dropping it, never splitting a table row or link (PIN-389)
Luigi Agosti1feb2df3ff42
diff --git a/docs/endpoints.md b/docs/endpoints.mdindex 814c9ea8..8fe32961 100644--- a/docs/endpoints.md+++ b/docs/endpoints.md@@ -331,7 +331,8 @@ envelope carrying the converter's `title` and `description` alongside `text`.It reads the current frame scope's rendered HTML (the document `/html` returns),so a `/frame`-selected iframe converts that frame. `format=text` returns the rawMarkdown body with `Content-Type: text/markdown; charset=utf-8`, and `maxChars`-truncates on a line boundary so a cut never splits a table row or link. When the+keeps whole lines and rune-cuts the final overrunning one, never splitting a+table row or link (those are dropped whole instead). When theconverter yields nothing the response falls back to the raw page text and echoes`extraction: "markdown_fallback"`.diff --git a/internal/handlers/text.go b/internal/handlers/text.goindex cacae21d..55df813d 100644--- a/internal/handlers/text.go+++ b/internal/handlers/text.go@@ -4,6 +4,7 @@ import ("context""fmt""net/http"+ "regexp""sort""strconv""strings"@@ -310,11 +311,13 @@ func truncateChars(s string, limit int) (string, bool) {return s, false}-// truncateCharsLine cuts s to at most limit characters on a line boundary, so a-// cut never lands inside a Markdown table row or link. It keeps whole lines-// while they fit and drops the rest; when the first line alone exceeds the limit-// the result is empty. Reports whether it cut. This differs from truncateChars,-// which cuts at the exact rune.+// truncateCharsLine cuts s to at most limit characters, keeping whole lines+// while they fit and then rune-cutting the first line that overruns. The partial+// line is dropped instead of cut only for the two shapes a mid-line cut would+// corrupt: a Markdown table row (starts with '|') or a cut that would land inside+// a [..](..) link — so a cut never splits a table row or link. Reports whether it+// cut. This differs from truncateChars, which cuts at the exact rune regardless+// of line structure.func truncateCharsLine(s string, limit int) (string, bool) {if limit < 0 || utf8.RuneCountInString(s) <= limit {return s, false@@ -322,22 +325,60 @@ func truncateCharsLine(s string, limit int) (string, bool) {var b strings.Buildercount := 0for i, line := range strings.Split(s, "\n") {- add := utf8.RuneCountInString(line)+ sep := 0if i > 0 {- add++ // the newline joining this line to the previous+ sep = 1 // the newline joining this line to the previous}- if count+add > limit {- break+ lineLen := utf8.RuneCountInString(line)+ if count+sep+lineLen <= limit {+ if i > 0 {+ b.WriteByte('\n')+ }+ b.WriteString(line)+ count += sep + lineLen+ continue}- if i > 0 {- b.WriteByte('\n')+ // This line overruns. Keep a rune-cut of it unless its shape forbids+ // splitting, in which case the whole line is dropped.+ budget := limit - count - sep+ if budget > 0 && !strings.HasPrefix(line, "|") {+ if partial, ok := cutLineForMarkdown(line, budget); ok {+ if i > 0 {+ b.WriteByte('\n')+ }+ b.WriteString(partial)+ }}- b.WriteString(line)- count += add+ break}return b.String(), true}+// markdownLinkRe matches a whole [text](url) link so a truncation can refuse to+// cut through one.+var markdownLinkRe = regexp.MustCompile(`\[[^\]]*\]\([^)]*\)`)++// cutLineForMarkdown returns the first budget runes of line for use as the final+// partial line of a truncation. ok is false when that cut would fall inside a+// [..](..) link, which must not be split; the caller then drops the line whole.+func cutLineForMarkdown(line string, budget int) (string, bool) {+ cut := len(line)+ count := 0+ for off := range line {+ if count == budget {+ cut = off+ break+ }+ count+++ }+ for _, loc := range markdownLinkRe.FindAllStringIndex(line, -1) {+ if loc[0] < cut && cut < loc[1] {+ return "", false+ }+ }+ return line[:cut], true+}+// writeTextResponse truncates, IDPI-scans, and writes the document text as// plain text (format text/plain) or the JSON envelope.func (h *Handlers) writeTextResponse(w http.ResponseWriter, r *http.Request, tCtx context.Context, extraction textExtraction, maxChars int, format string, route *browserops.RouteMetadata, scope *frameDisclosure) {diff --git a/internal/handlers/text_test.go b/internal/handlers/text_test.goindex 70b901ef..cd240f35 100644--- a/internal/handlers/text_test.go+++ b/internal/handlers/text_test.go@@ -480,30 +480,24 @@ func TestExtractDocumentMarkdown_EmptyConversionFallsBackToRaw(t *testing.T) {}}-// The markdown table proves the guarantee: a cut inside a row would split a-// pipe-delimited line, so every returned line must be whole.-func TestTruncateCharsLine_NeverCutsMidLine(t *testing.T) {+// A table row that overruns is dropped, never split: every returned line must be+// a whole source line, and the result must not be empty (the header rows fit).+func TestTruncateCharsLine_NeverSplitsTableRow(t *testing.T) {body := "| Col A | Col B | Col C |\n|-------|-------|-------|\n| a1 | b1 | c1 |\n| a2 | b2 | c2 |\n| a3 | b3 | c3 |"cut, truncated := truncateCharsLine(body, 60)if !truncated {t.Fatalf("expected a cut at 60 chars of a %d-char body", utf8.RuneCountInString(body))}- for _, line := range strings.Split(body, "\n") {- if strings.Contains(cut, line) {- continue- }- if strings.HasPrefix(line, cut) && cut != "" && !strings.HasSuffix(cut, line) {- t.Fatalf("a partial line survived the cut:\n%q", cut)- }+ if cut == "" {+ t.Fatalf("result is empty; the header rows fit within 60 chars and must survive")}- // Every line in the result must appear whole in the source.sourceLines := map[string]bool{}for _, line := range strings.Split(body, "\n") {sourceLines[line] = true}for _, line := range strings.Split(cut, "\n") {if !sourceLines[line] {- t.Fatalf("returned line %q is not a whole source line; the cut landed mid-line:\n%s", line, cut)+ t.Fatalf("returned line %q is not a whole source line; a table row was split:\n%s", line, cut)}}if utf8.RuneCountInString(cut) > 60 {@@ -511,6 +505,46 @@ func TestTruncateCharsLine_NeverCutsMidLine(t *testing.T) {}}+// seaportal emits each paragraph as ONE line, so a page opening with a long+// paragraph must still return a rune-cut of it — not an empty body. This fails on+// the whole-lines-only helper, which dropped the first overrunning line.+func TestTruncateCharsLine_RuneCutsLongFirstLine(t *testing.T) {+ body := strings.Repeat("word ", 60) + "end" // one ~303-char paragraph, no newlines+ if utf8.RuneCountInString(body) <= 200 {+ t.Fatalf("fixture must exceed the limit; got %d chars", utf8.RuneCountInString(body))+ }+ cut, truncated := truncateCharsLine(body, 200)+ if !truncated {+ t.Fatal("expected a cut")+ }+ if cut == "" {+ t.Fatal("a long first paragraph must be rune-cut, not dropped to empty")+ }+ if n := utf8.RuneCountInString(cut); n == 0 || n > 200 {+ t.Fatalf("cut kept %d chars, want 1..200", n)+ }+ if !strings.HasPrefix(body, cut) {+ t.Fatalf("the partial line must be a prefix of the source paragraph:\n%q", cut)+ }+}++// A cut that would land inside a [..](..) link drops the whole line rather than+// emit a half-open link; the fitting heading before it survives.+func TestTruncateCharsLine_NeverSplitsLink(t *testing.T) {+ line2 := "Read [the annual report](https://example.com/reports/2026/annual.pdf) today"+ body := "# Title\n" + line2+ cut, truncated := truncateCharsLine(body, 30) // budget lands inside the link text+ if !truncated {+ t.Fatal("expected a cut")+ }+ if cut != "# Title" {+ t.Fatalf("a mid-link cut must drop the line, keeping only the heading; got %q", cut)+ }+ if strings.Contains(cut, "](") || strings.Count(cut, "[") != strings.Count(cut, "]") {+ t.Fatalf("a partial link survived: %q", cut)+ }+}+func TestTruncateCharsLine_NoCutWhenUnderLimit(t *testing.T) {body := "one\ntwo\nthree"got, truncated := truncateCharsLine(body, 100)