Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/commands/richtext_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func TestCommentsCreateRejectsUnknownFormat(t *testing.T) {
// Content that used to be sent as written because it looked like HTML is now
// read as Markdown. The caller is told once, on stderr, how to keep the old
// result — unless the format was chosen explicitly, or the only tags are the
// mention and attachment markup Markdown content carries routinely.
// mention and attachment markup Markdown content carries routinely, or a fold's
// <details> and <summary>, which the basecamp skill tells agents to write in
// Markdown. Warning on a fold pointed agents at --format html, where every block
// is stored touching.
func TestRichTextHTMLInMarkdownWarns(t *testing.T) {
tests := []struct {
name string
Expand All @@ -139,6 +142,13 @@ func TestRichTextHTMLInMarkdownWarns(t *testing.T) {
{"html in a tilde fence", []string{"create", "789", "~~~\n<p>x</p>\n~~~"}, false},
{"html in indented code", []string{"create", "789", "Example:\n\n <p>x</p>"}, false},
{"angle brackets in prose", []string{"create", "789", "A Vec<String> here"}, false},
{"a fold in markdown", []string{"create", "789", "The fix landed.\n\n<details><summary>▶ How it was measured</summary>\n\n- Five **runs**\n\n</details>"}, false},
{"a fold written inline", []string{"create", "789", "Done. <details><summary>Steps</summary>ran it twice</details>"}, false},
{"a fold written as html", []string{"create", "789", "<details><summary>Steps</summary><p>ran it twice</p></details>"}, true},
{"html beside a fold", []string{"create", "789", "<p>A</p>\n\n<details><summary>Steps</summary>\n\nran it\n\n</details>"}, true},
{"a summary holding other tags", []string{"create", "789", "<details><summary><strong>Steps</strong></summary>\n\nran it\n\n</details>"}, true},
{"a fold holding a comment", []string{"create", "789", "<details><summary>Steps</summary><!-- a note -->\n\nran it\n\n</details>"}, true},
{"a fold tag with an attribute", []string{"create", "789", "<details data-x=\"<p>\"><summary>Steps</summary>\n\nran it\n\n</details>"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
23 changes: 19 additions & 4 deletions internal/richtext/richtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,11 @@ func MarkdownToHTML(md string) string {
}

// HasRichTextHTML reports whether MarkdownToHTML would keep any raw HTML from
// md other than <bc-attachment> markup: whether the Markdown parser finds an
// inline tag or an HTML block whose tag is in richTextTags. HTML inside code is
// not raw HTML to the parser, so it never counts.
// md other than <bc-attachment> markup and a fold's own tags: whether the
// Markdown parser finds an inline tag or an HTML block whose tag is in
// richTextTags. HTML inside code is not raw HTML to the parser, so it never
// counts. A fold written the way the basecamp skill says, <details> and
// <summary> around Markdown, is Markdown content, so it never counts either.
func HasRichTextHTML(md string) bool {
if md == "" {
return false
Expand All @@ -507,7 +509,7 @@ func HasRichTextHTML(md string) bool {
default:
return ast.WalkContinue, nil
}
if isRichTextTag(raw) && !reLeadingAttachmentTag.Match(raw) {
if isRichTextTag(raw) && !reLeadingAttachmentTag.Match(raw) && !isFoldMarkup(raw) {
found = true
return ast.WalkStop, nil
}
Expand All @@ -516,6 +518,19 @@ func HasRichTextHTML(md string) bool {
return found
}

// reFoldMarkup matches raw HTML made only of a fold's own bare tags, <details>,
// <summary> and their closing tags, with text between them. Every "<" in it
// must open one of those four tags, so a tag with attributes, any other tag, a
// comment or a declaration does not match.
var reFoldMarkup = regexp.MustCompile(`(?i)^(?:[^<]|</?(?:details|summary)>)*$`)

// isFoldMarkup reports whether raw HTML holds only a fold's own tags, <details>
// and <summary>, with nothing but text between them. An HTML block that also
// holds anything else, such as a whole fold written as HTML, is not.
func isFoldMarkup(raw []byte) bool {
return reFoldMarkup.Match(raw)
}

// reLeadingAttachmentTag matches a <bc-attachment> open or close tag at the
// start of raw HTML: the mention and attachment markup Markdown content carries
// routinely.
Expand Down
Loading