From 8db9fbb2c22078aa47ae0cbb765bffe882ca06fb Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Thu, 19 Nov 2026 00:08:30 -0500 Subject: [PATCH 1/5] Send mentions and files back bare when rich text is edited Basecamp returns a mention or a file attachment expanded: the avatar or preview as a
inside the , and for a mention its rendered HTML in a content attribute. An agent that edits a post by sending back the content it read wrote that expanded form, which Basecamp stores as content of its own: the figure breaks the paragraph around a mention apart, and every edit adds another copy. richTextToHTML now reduces every that names a record to the bare tag Basecamp documents for writing one, in both formats. In Markdown, attachments written inside code are left as they are. Fixes #905 --- internal/commands/richtext_format.go | 10 +- internal/commands/richtext_format_test.go | 25 +++++ internal/richtext/attachments.go | 121 ++++++++++++++++++++++ internal/richtext/attachments_test.go | 54 ++++++++++ 4 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 internal/richtext/attachments.go create mode 100644 internal/richtext/attachments_test.go diff --git a/internal/commands/richtext_format.go b/internal/commands/richtext_format.go index ea84cdbf4..dd1e89d87 100644 --- a/internal/commands/richtext_format.go +++ b/internal/commands/richtext_format.go @@ -48,8 +48,11 @@ func addRichTextFormatFlag(cmd *cobra.Command) { } // richTextToHTML is the one place a command turns rich-text input into the -// HTML Basecamp stores. With --format html the content is returned exactly as -// given. Otherwise it is read as Markdown, whatever it contains. +// HTML Basecamp stores. With --format html the content is returned as given. +// Otherwise it is read as Markdown, whatever it contains. Either way, a +// mention or file attachment copied from what the API returned is first +// reduced to the bare tag Basecamp expects (richtext.CollapseAttachments): +// sent back expanded, it corrupts the post. // // Content that holds HTML tags was sent as written before Markdown became the // only default, so when --format was left unset and the Markdown keeps raw HTML @@ -61,8 +64,9 @@ func richTextToHTML(cmd *cobra.Command, content string) string { panic(cmd.CommandPath() + " reads rich text but has no --format flag; call addRichTextFormatFlag") } if flag.Value.String() == string(richTextHTML) { - return content + return richtext.CollapseAttachments(content) } + content = richtext.CollapseAttachmentsInMarkdown(content) if !flag.Changed && richtext.HasRichTextHTML(content) { fmt.Fprintln(cmd.ErrOrStderr(), "Warning: content contains HTML tags and was read as Markdown, the default. "+ "Supported tags are kept and the Markdown around them is converted. "+ diff --git a/internal/commands/richtext_format_test.go b/internal/commands/richtext_format_test.go index 849ffbacd..42475b346 100644 --- a/internal/commands/richtext_format_test.go +++ b/internal/commands/richtext_format_test.go @@ -270,3 +270,28 @@ func TestNotesSetFormatHTMLSendsContentExactly(t *testing.T) { require.NoError(t, json.Unmarshal([]byte(transport.last(t).Body), &body)) assert.Equal(t, passthroughHTML, body.Note.Content) } + +// expandedMention is a mention as the API returns it: the avatar figure inside +// the tag and the rendered mention in its content attribute. +const expandedMention = `

Started by

+ Jason Fried +
+ Jason +
+
this week.

` + +// Writing back a mention as the API returned it stored its avatar as content +// of its own, split the paragraph, and added a copy on every edit (#905). +// Basecamp's write form is the bare tag, in either format. +func TestCommentsUpdateCollapsesAMentionCopiedFromTheAPI(t *testing.T) { + want := `

Started by this week.

` + for _, format := range []string{"html", "markdown"} { + t.Run(format, func(t *testing.T) { + transport := &mockCommentWriteTransport{} + _, err := runCommentWithStderr(t, transport, expandedMention+"\n", "update", "1234", "-", "--format", format) + require.NoError(t, err) + assert.Equal(t, want, sentContent(t, transport)) + }) + } +} diff --git a/internal/richtext/attachments.go b/internal/richtext/attachments.go new file mode 100644 index 000000000..529ad1350 --- /dev/null +++ b/internal/richtext/attachments.go @@ -0,0 +1,121 @@ +package richtext + +import ( + "regexp" + "sort" + "strings" + + "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/text" +) + +// reAttachmentElement matches a element that has a closing tag, +// capturing its opening tag's attributes. Attribute values are HTML-escaped in +// what the API returns, so none holds a literal '>'. +var reAttachmentElement = regexp.MustCompile(`(?is)]*)?>.*?`) + +// reTagAttribute matches one attribute of a tag: its name, and its value if it +// has one, quoted or not. +var reTagAttribute = regexp.MustCompile(`([^\s"'=<>/]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>` + "`" + `]+))?`) + +// CollapseAttachments returns html with every that names a +// record (it has an sgid) reduced to the form Basecamp documents for writing +// one: the tag with nothing inside it and without its content attribute. +// +// Basecamp returns a mention or a file expanded: the person's avatar or the +// file's preview as a
inside the tag, and for a mention its rendered +// HTML in content. Written back as returned, the figure is stored as content +// of its own: it breaks the paragraph around a mention apart, and each edit +// adds another copy. Basecamp expands the bare tag again when it is read, so +// collapsing it loses nothing. An attachment without an sgid carries its +// content itself (a horizontal rule, say) and is left as written. +func CollapseAttachments(html string) string { + if !strings.Contains(strings.ToLower(html), " element to its write form, +// or returns it unchanged when it names no record. +func collapseAttachment(element string) string { + open := element[:strings.IndexByte(element, '>')] + attrs := reTagAttribute.FindAllString(open[len("" +} + +// markdownCodeSpans returns the byte ranges of md that the Markdown parser +// reads as code: code spans, and fenced and indented code blocks. +func markdownCodeSpans(md string) textSpanIndex { + source := []byte(md) + doc := mdConverter.Parser().Parse(text.NewReader(source)) + + var index textSpanIndex + add := func(lines *text.Segments) { + for i := 0; i < lines.Len(); i++ { + seg := lines.At(i) + index = append(index, textSpan{start: seg.Start, end: seg.Stop}) + } + } + _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { + if !entering { + return ast.WalkContinue, nil + } + switch node := n.(type) { + case *ast.FencedCodeBlock, *ast.CodeBlock: + add(node.Lines()) + return ast.WalkSkipChildren, nil + case *ast.CodeSpan: + for c := node.FirstChild(); c != nil; c = c.NextSibling() { + if t, ok := c.(*ast.Text); ok { + index = append(index, textSpan{start: t.Segment.Start, end: t.Segment.Stop}) + } + } + return ast.WalkSkipChildren, nil + } + return ast.WalkContinue, nil + }) + sort.Slice(index, func(i, j int) bool { return index[i].start < index[j].start }) + return index +} diff --git a/internal/richtext/attachments_test.go b/internal/richtext/attachments_test.go new file mode 100644 index 000000000..3bea33465 --- /dev/null +++ b/internal/richtext/attachments_test.go @@ -0,0 +1,54 @@ +package richtext + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCollapseAttachmentsReducesAnExpandedMention(t *testing.T) { + input := `

Hi

+ Jo +
Jo
+
there.

` + + assert.Equal(t, + `

Hi there.

`, + CollapseAttachments(input)) +} + +func TestCollapseAttachmentsKeepsAFileAttachmentsOwnAttributes(t *testing.T) { + input := `
The bug
` + + assert.Equal(t, + ``, + CollapseAttachments(input)) +} + +func TestCollapseAttachmentsLeavesWhatNamesNoRecord(t *testing.T) { + for _, input := range []string{ + `

`, + ``, + ``, + `

no attachments

`, + } { + assert.Equal(t, input, CollapseAttachments(input)) + } +} + +func TestCollapseAttachmentsInMarkdownLeavesCode(t *testing.T) { + expanded := `
Jo
` + bare := `` + input := "Hi " + expanded + " there.\n\n" + + "`" + expanded + "`\n\n" + + "```html\n" + expanded + "\n```\n\n" + + " " + expanded + "\n" + + assert.Equal(t, + "Hi "+bare+" there.\n\n"+ + "`"+expanded+"`\n\n"+ + "```html\n"+expanded+"\n```\n\n"+ + " "+expanded+"\n", + CollapseAttachmentsInMarkdown(input)) +} From c203354b67ffa9334855408d95a7e5f1b11072fd Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Fri, 20 Nov 2026 11:47:00 -0500 Subject: [PATCH 2/5] Read attachment tags by their quotes, and pass self-closing ones by A self-closing was taken as the opening tag of the next attachment, which dropped everything up to that one's closing tag. And the first '>' was taken to end an opening tag even inside a quoted value. Walk the tags instead of matching them with a pattern: skip quoted values, leave self-closing and unclosed tags as written, and search a copy lowered only in ASCII so its indexes stay those of the original. --- internal/richtext/attachments.go | 144 ++++++++++++++++++++++---- internal/richtext/attachments_test.go | 43 ++++++++ 2 files changed, 165 insertions(+), 22 deletions(-) diff --git a/internal/richtext/attachments.go b/internal/richtext/attachments.go index 529ad1350..a9bc8f2ca 100644 --- a/internal/richtext/attachments.go +++ b/internal/richtext/attachments.go @@ -9,11 +9,6 @@ import ( "github.com/yuin/goldmark/text" ) -// reAttachmentElement matches a element that has a closing tag, -// capturing its opening tag's attributes. Attribute values are HTML-escaped in -// what the API returns, so none holds a literal '>'. -var reAttachmentElement = regexp.MustCompile(`(?is)]*)?>.*?`) - // reTagAttribute matches one attribute of a tag: its name, and its value if it // has one, quoted or not. var reTagAttribute = regexp.MustCompile(`([^\s"'=<>/]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>` + "`" + `]+))?`) @@ -28,41 +23,146 @@ var reTagAttribute = regexp.MustCompile(`([^\s"'=<>/]+)(?:\s*=\s*(?:"[^"]*"|'[^' // of its own: it breaks the paragraph around a mention apart, and each edit // adds another copy. Basecamp expands the bare tag again when it is read, so // collapsing it loses nothing. An attachment without an sgid carries its -// content itself (a horizontal rule, say) and is left as written. +// content itself (a horizontal rule, say) and is left as written, and so is +// one that is self-closing or never closed. func CollapseAttachments(html string) string { - if !strings.Contains(strings.ToLower(html), " in a string: where it starts, +// where its opening tag ends, and where the element ends. A self-closing tag +// ends where its opening tag does. +type attachmentElement struct { + start, openEnd, end int + selfClosing bool +} + +const ( + attachmentOpen = " element at or after +// from. The opening tag is read with its quoted attribute values, so a '>' +// inside one does not end it. An element that is opened and never closed, or +// is followed by another before its closing tag, is not one: the search moves +// past its opening tag. +// lower is s with its ASCII letters lowered, byte for byte. +func nextAttachmentElement(s, lower string, from int) (attachmentElement, bool) { + for from < len(s) { + i := strings.Index(lower[from:], attachmentOpen) + if i < 0 { + return attachmentElement{}, false + } + start := from + i + after := start + len(attachmentOpen) + if after < len(s) && !isTagNameEnd(s[after]) { + from = after + continue + } + openEnd, ok := openingTagEnd(s, after) + if !ok { + return attachmentElement{}, false + } + if strings.HasSuffix(strings.TrimRight(s[after:openEnd-1], " \t\n\r"), "/") { + return attachmentElement{start: start, openEnd: openEnd, end: openEnd, selfClosing: true}, true + } + c := strings.Index(lower[openEnd:], attachmentClose) + if c < 0 || strings.Contains(lower[openEnd:openEnd+c], attachmentOpen) { + from = openEnd + continue + } + closeStart := openEnd + c + closeEnd := strings.IndexByte(s[closeStart:], '>') + if closeEnd < 0 { + return attachmentElement{}, false + } + return attachmentElement{start: start, openEnd: openEnd, end: closeStart + closeEnd + 1}, true + } + return attachmentElement{}, false +} + +// asciiLower lowers only ASCII letters, so every index into the result is the +// same index into s. strings.ToLower can change a string's length: 'İ' grows +// by a byte. +func asciiLower(s string) string { + b := []byte(s) + for i, c := range b { + if 'A' <= c && c <= 'Z' { + b[i] = c + ('a' - 'A') + } + } + return string(b) +} + +func isTagNameEnd(c byte) bool { + return c == '>' || c == '/' || c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' +} + +// openingTagEnd returns the index just past the '>' that ends the tag whose +// attributes start at i, skipping quoted attribute values. +func openingTagEnd(s string, i int) (int, bool) { + var quote byte + for ; i < len(s); i++ { + c := s[i] + switch { + case quote != 0: + if c == quote { + quote = 0 + } + case c == '"' || c == '\'': + quote = c + case c == '>': + return i + 1, true + } + } + return 0, false +} + // collapseAttachment reduces one element to its write form, // or returns it unchanged when it names no record. -func collapseAttachment(element string) string { - open := element[:strings.IndexByte(element, '>')] - attrs := reTagAttribute.FindAllString(open[len("KEEP THIS TEXT
Jo
` + + assert.Equal(t, + `KEEP THIS TEXT`, + CollapseAttachments(input)) + assert.Equal(t, + `KEEP THIS TEXT`, + CollapseAttachmentsInMarkdown(input)) +} + +func TestCollapseAttachmentsReadsAQuotedGreaterThan(t *testing.T) { + input := `
a > b
` + + assert.Equal(t, + ``, + CollapseAttachments(input)) +} + +func TestCollapseAttachmentsLeavesAnUnclosedAttachment(t *testing.T) { + for _, input := range []string{ + `

Hi

Jo

`, + `

Hi

Jo
`, + } { + assert.Equal(t, input, CollapseAttachments(input)) + } + + assert.Equal(t, + `KEEP `, + CollapseAttachments(`KEEP
Jo
`)) +} + +// Lowering a string for a case-blind search must not move its bytes: 'İ' +// lowers to three bytes from two. +func TestCollapseAttachmentsAfterTextThatLowersLonger(t *testing.T) { + input := `

İİ

Jo
ok

` + + assert.Equal(t, `

İİ ok

`, CollapseAttachments(input)) +} From 8c49be3474ed611d47207236face2b80a5f47500 Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Fri, 20 Nov 2026 11:59:09 -0500 Subject: [PATCH 3/5] Leave attachment tags in HTML comments and Markdown escapes alone A tag inside an HTML comment, or a Markdown tag escaped with a backslash, is text rather than markup, but was read as an attachment, and collapsing it deleted what lay between it and its closing tag. Pass both by when looking for opening and closing tags. --- internal/richtext/attachments.go | 128 +++++++++++++++++++------- internal/richtext/attachments_test.go | 28 ++++++ 2 files changed, 125 insertions(+), 31 deletions(-) diff --git a/internal/richtext/attachments.go b/internal/richtext/attachments.go index a9bc8f2ca..b4dc5b038 100644 --- a/internal/richtext/attachments.go +++ b/internal/richtext/attachments.go @@ -26,39 +26,60 @@ var reTagAttribute = regexp.MustCompile(`([^\s"'=<>/]+)(?:\s*=\s*(?:"[^"]*"|'[^' // content itself (a horizontal rule, say) and is left as written, and so is // one that is self-closing or never closed. func CollapseAttachments(html string) string { - return collapseAttachmentsOutside(html, nil) + return collapseAttachments(html, nil) } // CollapseAttachmentsInMarkdown is CollapseAttachments for Markdown: it -// leaves alone an attachment written inside a code span or code block, which -// shows the markup as text rather than writing it. +// leaves alone an attachment written inside a code span or code block, or +// escaped with a backslash, which shows the markup as text rather than +// writing it. func CollapseAttachmentsInMarkdown(md string) string { if !hasAttachmentTag(md) { return md } - return collapseAttachmentsOutside(md, markdownCodeSpans(md)) + code := markdownCodeSpans(md) + return collapseAttachments(md, func(i int) bool { + return code.contains(i) || escapedInMarkdown(md, i) + }) } func hasAttachmentTag(s string) bool { return strings.Contains(strings.ToLower(s), "= 0 && s[j] == '\\'; j-- { + n++ + } + return n%2 == 1 +} + +// collapseAttachments collapses every attachment element in s. A tag that +// starts where literal says is text, or inside an HTML comment, is not one. +func collapseAttachments(s string, literal func(int) bool) string { if !hasAttachmentTag(s) { return s } - lower := asciiLower(s) + if literal == nil { + literal = func(int) bool { return false } + } + comments := htmlComments(s, literal) + tags := attachmentTags{s: s, lower: asciiLower(s), literal: func(i int) bool { + return literal(i) || comments.contains(i) + }} + var b strings.Builder last, pos := 0, 0 for { - el, ok := nextAttachmentElement(s, lower, pos) + el, ok := tags.nextElement(pos) if !ok { break } pos = el.end - if el.selfClosing || code.contains(el.start) { + if el.selfClosing { continue } b.WriteString(s[last:el.start]) @@ -69,6 +90,30 @@ func collapseAttachmentsOutside(s string, code textSpanIndex) string { return b.String() } +// htmlComments returns the byte ranges of s inside HTML comments, an +// unterminated one running to the end. A ""); j >= 0 { + end = start + 4 + j + 3 + } + index = append(index, textSpan{start: start, end: end}) + from = end + } +} + // attachmentElement locates one in a string: where it starts, // where its opening tag ends, and where the element ends. A self-closing tag // ends where its opening tag does. @@ -82,44 +127,65 @@ const ( attachmentClose = "
element at or after -// from. The opening tag is read with its quoted attribute values, so a '>' -// inside one does not end it. An element that is opened and never closed, or -// is followed by another before its closing tag, is not one: the search moves -// past its opening tag. -// lower is s with its ASCII letters lowered, byte for byte. -func nextAttachmentElement(s, lower string, from int) (attachmentElement, bool) { - for from < len(s) { - i := strings.Index(lower[from:], attachmentOpen) +// attachmentTags finds attachment tags in s. lower is s with its ASCII +// letters lowered, byte for byte, and a tag starting where literal says is +// text is passed by. +type attachmentTags struct { + s, lower string + literal func(int) bool +} + +// next returns the start of the first tag beginning with prefix at or after +// from, followed by the end of its name or the string. +func (t attachmentTags) next(prefix string, from int) (int, bool) { + for from < len(t.s) { + i := strings.Index(t.lower[from:], prefix) if i < 0 { - return attachmentElement{}, false + return 0, false } start := from + i - after := start + len(attachmentOpen) - if after < len(s) && !isTagNameEnd(s[after]) { - from = after - continue + after := start + len(prefix) + if (after == len(t.s) || isTagNameEnd(t.s[after])) && !t.literal(start) { + return start, true + } + from = after + } + return 0, false +} + +// nextElement finds the first element at or after from. The +// opening tag is read with its quoted attribute values, so a '>' inside one +// does not end it. An element that is opened and never closed, or is followed +// by another before its closing tag, is not one: the search moves past its +// opening tag. +func (t attachmentTags) nextElement(from int) (attachmentElement, bool) { + for { + start, ok := t.next(attachmentOpen, from) + if !ok { + return attachmentElement{}, false } - openEnd, ok := openingTagEnd(s, after) + after := start + len(attachmentOpen) + openEnd, ok := openingTagEnd(t.s, after) if !ok { return attachmentElement{}, false } - if strings.HasSuffix(strings.TrimRight(s[after:openEnd-1], " \t\n\r"), "/") { + if strings.HasSuffix(strings.TrimRight(t.s[after:openEnd-1], " \t\n\r"), "/") { return attachmentElement{start: start, openEnd: openEnd, end: openEnd, selfClosing: true}, true } - c := strings.Index(lower[openEnd:], attachmentClose) - if c < 0 || strings.Contains(lower[openEnd:openEnd+c], attachmentOpen) { + closeStart, ok := t.next(attachmentClose, openEnd) + if !ok { + return attachmentElement{}, false + } + if nested, ok := t.next(attachmentOpen, openEnd); ok && nested < closeStart { from = openEnd continue } - closeStart := openEnd + c - closeEnd := strings.IndexByte(s[closeStart:], '>') + closeEnd := strings.IndexByte(t.s[closeStart:], '>') if closeEnd < 0 { return attachmentElement{}, false } return attachmentElement{start: start, openEnd: openEnd, end: closeStart + closeEnd + 1}, true } - return attachmentElement{}, false } // asciiLower lowers only ASCII letters, so every index into the result is the diff --git a/internal/richtext/attachments_test.go b/internal/richtext/attachments_test.go index 75d08e6e6..5ff2d9d13 100644 --- a/internal/richtext/attachments_test.go +++ b/internal/richtext/attachments_test.go @@ -95,3 +95,31 @@ func TestCollapseAttachmentsAfterTextThatLowersLonger(t *testing.T) { assert.Equal(t, `

İİ ok

`, CollapseAttachments(input)) } + +// A backslash makes a tag literal Markdown text, so neither an escaped +// opening tag nor an escaped closing tag is markup. +func TestCollapseAttachmentsInMarkdownLeavesEscapedTags(t *testing.T) { + for _, input := range []string{ + `Example: \KEEP THIS TEXT\`, + `Example: KEEP THIS TEXT\`, + } { + assert.Equal(t, input, CollapseAttachmentsInMarkdown(input)) + } + + assert.Equal(t, + `Example: \\`, + CollapseAttachmentsInMarkdown(`Example: \\
Jo
`)) +} + +// Markup inside an HTML comment is not markup: reading it as an attachment +// deleted the visible content between two comments. +func TestCollapseAttachmentsLeavesTagsInComments(t *testing.T) { + input := `

KEEP THIS TEXT

` + + assert.Equal(t, input, CollapseAttachments(input)) + assert.Equal(t, input, CollapseAttachmentsInMarkdown(input)) + + assert.Equal(t, + `
Jo
"); j >= 0 { - end = start + 4 + j + 3 - } - index = append(index, textSpan{start: start, end: end}) - from = end +// attachmentTagsIn reads s[start:end] as HTML and returns its attachment +// tags. A tokenizer reads it, so markup inside a comment or inside another +// tag's attribute value is not taken for a tag. +func attachmentTagsIn(s string, start, end int) []attachmentTag { + var tags []attachmentTag + z := html.NewTokenizer(strings.NewReader(s[start:end])) + for pos := start; ; { + kind := z.Next() + if kind == html.ErrorToken { + return tags + } + size := len(z.Raw()) + isTag := kind == html.StartTagToken || kind == html.EndTagToken || kind == html.SelfClosingTagToken + if name, _ := z.TagName(); isTag && bytes.Equal(name, []byte("bc-attachment")) { + tags = append(tags, attachmentTag{start: pos, end: pos + size, kind: kind}) + } + pos += size } } -// attachmentElement locates one in a string: where it starts, -// where its opening tag ends, and where the element ends. A self-closing tag -// ends where its opening tag does. -type attachmentElement struct { - start, openEnd, end int - selfClosing bool -} - -const ( - attachmentOpen = " element at or after from. The -// opening tag is read with its quoted attribute values, so a '>' inside one -// does not end it. An element that is opened and never closed, or is followed -// by another before its closing tag, is not one: the search moves past its -// opening tag. -func (t attachmentTags) nextElement(from int) (attachmentElement, bool) { - for { - start, ok := t.next(attachmentOpen, from) - if !ok { - return attachmentElement{}, false - } - after := start + len(attachmentOpen) - openEnd, ok := openingTagEnd(t.s, after) - if !ok { - return attachmentElement{}, false - } - if strings.HasSuffix(strings.TrimRight(t.s[after:openEnd-1], " \t\n\r"), "/") { - return attachmentElement{start: start, openEnd: openEnd, end: openEnd, selfClosing: true}, true - } - closeStart, ok := t.next(attachmentClose, openEnd) - if !ok { - return attachmentElement{}, false + var tags []attachmentTag + read := func(segments []text.Segment) { + if len(segments) == 0 { + return } - if nested, ok := t.next(attachmentOpen, openEnd); ok && nested < closeStart { - from = openEnd - continue - } - closeEnd := strings.IndexByte(t.s[closeStart:], '>') - if closeEnd < 0 { - return attachmentElement{}, false - } - return attachmentElement{start: start, openEnd: openEnd, end: closeStart + closeEnd + 1}, true + tags = append(tags, attachmentTagsIn(md, segments[0].Start, segments[len(segments)-1].Stop)...) } -} - -// asciiLower lowers only ASCII letters, so every index into the result is the -// same index into s. strings.ToLower can change a string's length: 'İ' grows -// by a byte. -func asciiLower(s string) string { - b := []byte(s) - for i, c := range b { - if 'A' <= c && c <= 'Z' { - b[i] = c + ('a' - 'A') + _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { + if !entering { + return ast.WalkContinue, nil } - } - return string(b) -} - -func isTagNameEnd(c byte) bool { - return c == '>' || c == '/' || c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' -} - -// openingTagEnd returns the index just past the '>' that ends the tag whose -// attributes start at i, skipping quoted attribute values. -func openingTagEnd(s string, i int) (int, bool) { - var quote byte - for ; i < len(s); i++ { - c := s[i] - switch { - case quote != 0: - if c == quote { - quote = 0 + switch node := n.(type) { + case *ast.RawHTML: + read(node.Segments.Sliced(0, node.Segments.Len())) + case *ast.HTMLBlock: + lines := node.Lines() + if isRichTextTag(lines.Value(source)) { + segments := lines.Sliced(0, lines.Len()) + if node.HasClosure() { + segments = append(segments, node.ClosureLine) + } + read(segments) } - case c == '"' || c == '\'': - quote = c - case c == '>': - return i + 1, true + return ast.WalkSkipChildren, nil } - } - return 0, false + return ast.WalkContinue, nil + }) + sort.Slice(tags, func(i, j int) bool { return tags[i].start < tags[j].start }) + return tags } // collapseAttachment reduces one element to its write form, // or returns it unchanged when it names no record. func collapseAttachment(element, open string) string { - attrs := reTagAttribute.FindAllString(open[len(attachmentOpen):len(open)-1], -1) + attrs := reTagAttribute.FindAllString(open[len("" } - -// markdownCodeSpans returns the byte ranges of md that the Markdown parser -// reads as code: code spans, and fenced and indented code blocks. -func markdownCodeSpans(md string) textSpanIndex { - source := []byte(md) - doc := mdConverter.Parser().Parse(text.NewReader(source)) - - var index textSpanIndex - add := func(lines *text.Segments) { - for i := 0; i < lines.Len(); i++ { - seg := lines.At(i) - index = append(index, textSpan{start: seg.Start, end: seg.Stop}) - } - } - _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { - if !entering { - return ast.WalkContinue, nil - } - switch node := n.(type) { - case *ast.FencedCodeBlock, *ast.CodeBlock: - add(node.Lines()) - return ast.WalkSkipChildren, nil - case *ast.CodeSpan: - for c := node.FirstChild(); c != nil; c = c.NextSibling() { - if t, ok := c.(*ast.Text); ok { - index = append(index, textSpan{start: t.Segment.Start, end: t.Segment.Stop}) - } - } - return ast.WalkSkipChildren, nil - } - return ast.WalkContinue, nil - }) - sort.Slice(index, func(i, j int) bool { return index[i].start < index[j].start }) - return index -} diff --git a/internal/richtext/attachments_test.go b/internal/richtext/attachments_test.go index 5ff2d9d13..15c0f8383 100644 --- a/internal/richtext/attachments_test.go +++ b/internal/richtext/attachments_test.go @@ -123,3 +123,24 @@ func TestCollapseAttachmentsLeavesTagsInComments(t *testing.T) { `
Jo