diff --git a/internal/stringutil/htmlchunker.go b/internal/stringutil/htmlchunker.go index 9efd76b6..178e83fc 100644 --- a/internal/stringutil/htmlchunker.go +++ b/internal/stringutil/htmlchunker.go @@ -11,7 +11,7 @@ import ( ) // Bump whenever ChunkHTMLContent changes the text it emits for unchanged input; it feeds reindex fingerprints. -const ChunkerVersion = 2 +const ChunkerVersion = 3 var ( sentenceRegex = regexp.MustCompile(`[.!?]+[\s]+`) diff --git a/internal/stringutil/htmlembedprep.go b/internal/stringutil/htmlembedprep.go index e96d2c5d..f39838bb 100644 --- a/internal/stringutil/htmlembedprep.go +++ b/internal/stringutil/htmlembedprep.go @@ -12,7 +12,7 @@ func prepareHTMLForEmbedding(htmlContent string) string { if err != nil { return htmlContent } - inlineLinkHrefs(doc) + inlineAnchorMarkdown(doc) flattenTables(doc) var b strings.Builder @@ -22,20 +22,6 @@ func prepareHTMLForEmbedding(htmlContent string) string { return b.String() } -// inlineLinkHrefs appends each link's destination after its text so the flattened text keeps the URL. -func inlineLinkHrefs(n *html.Node) { - if n.Type == html.ElementNode && n.Data == "a" { - href := strings.TrimSpace(attrValue(n, "href")) - text := strings.TrimSpace(nodeText(n)) - if href != "" && !strings.HasPrefix(href, "#") && !strings.HasPrefix(href, "cid:") && text != href { - n.AppendChild(&html.Node{Type: html.TextNode, Data: " (" + href + ")"}) - } - } - for c := n.FirstChild; c != nil; c = c.NextSibling { - inlineLinkHrefs(c) - } -} - // flattenTables turns each table into a
of one header-labelled line per row, which also keeps it unsplittable.
func flattenTables(n *html.Node) {
var tables []*html.Node
diff --git a/internal/stringutil/htmlembedprep_test.go b/internal/stringutil/htmlembedprep_test.go
new file mode 100644
index 00000000..5366eeb1
--- /dev/null
+++ b/internal/stringutil/htmlembedprep_test.go
@@ -0,0 +1,87 @@
+package stringutil
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestPrepareHTMLForEmbedding(t *testing.T) {
+ tests := []struct {
+ name string
+ in string
+ contains []string
+ notContains []string
+ }{
+ {
+ name: "link becomes markdown",
+ in: `See the guide for steps.
`,
+ contains: []string{"See [the guide](https://example.com/guide) for steps."},
+ notContains: []string{"https://example.com`,
+ contains: []string{">https://example.com<"},
+ notContains: []string{
+ "[https://example.com](https://example.com)",
+ "Setup inline`,
+ contains: []string{`Setup`, `inline`},
+ notContains: []string{"[Setup]"},
+ },
+ {
+ name: "table flattens to header labelled lines",
+ in: `| Plan | Price |
|---|---|
| Basic | $5 |
| Pro | $10 |
", "Plan: Basic | Price: $5", "Plan: Pro | Price: $10"},
+ notContains: []string{"| Plan | Price |
|---|
| Basic | $5 |
| Doc |
|---|
| Pay now |
{{ .L.T "helpCenter.notFoundText" }}
- + {{ template "hc-search-icon" }} {{ .L.T "helpCenter.searchPlaceholder" }} diff --git a/static/public/web-templates/help/docs/search.html b/static/public/web-templates/help/docs/search.html index 0936b05b..6845a96e 100644 --- a/static/public/web-templates/help/docs/search.html +++ b/static/public/web-templates/help/docs/search.html @@ -2,7 +2,7 @@ {{ template "docs-header" . }} diff --git a/static/public/web-templates/help/shared/partials.html b/static/public/web-templates/help/shared/partials.html index 2d52ba58..df049362 100644 --- a/static/public/web-templates/help/shared/partials.html +++ b/static/public/web-templates/help/shared/partials.html @@ -26,6 +26,29 @@