llm browser fixes
This commit is contained in:
1 parent
a4dbb75195
commit
f8526bfb4e
8 files changed
+33
-15
No files matched your search
@@ -221,8 +221,11 @@ func (r *mdRenderer) element(b *strings.Builder, n *html.Node) {
|
||||
case atom.Dl:
|
||||
r.defList(b, n)
|
||||
case atom.Button, atom.A, atom.Span, atom.Strong, atom.Em, atom.Code, atom.Small, atom.Label:
|
||||
// phrasing content at block position: keep it as its own paragraph
|
||||
if s := strings.TrimSpace(r.inline(n)); s != "" {
|
||||
// phrasing content at block position (nav links, card wrappers):
|
||||
// render with full inline semantics so anchors keep their href
|
||||
var ib strings.Builder
|
||||
r.inlineNode(&ib, n)
|
||||
if s := strings.TrimSpace(ib.String()); s != "" {
|
||||
b.WriteString(s + "\n\n")
|
||||
}
|
||||
default:
|
||||
@@ -230,6 +233,19 @@ func (r *mdRenderer) element(b *strings.Builder, n *html.Node) {
|
||||
}
|
||||
}
|
||||
|
||||
// inlineNode renders one child node with inline semantics — the single
|
||||
// dispatch point for list items and block-position phrasing elements, so
|
||||
// anchors keep their href wherever they appear.
|
||||
func (r *mdRenderer) inlineNode(b *strings.Builder, n *html.Node) {
|
||||
if n.Type == html.TextNode {
|
||||
b.WriteString(collapse(n.Data))
|
||||
return
|
||||
}
|
||||
if n.Type == html.ElementNode {
|
||||
r.inlineElement(b, n)
|
||||
}
|
||||
}
|
||||
|
||||
// list renders ul/ol items; nested lists indent under their item.
|
||||
func (r *mdRenderer) list(b *strings.Builder, n *html.Node, indent string) {
|
||||
ordered := n.DataAtom == atom.Ol
|
||||
@@ -258,11 +274,7 @@ func (r *mdRenderer) list(b *strings.Builder, n *html.Node, indent string) {
|
||||
nested = append(nested, c)
|
||||
continue
|
||||
}
|
||||
if c.Type == html.TextNode {
|
||||
text.WriteString(collapse(c.Data))
|
||||
} else {
|
||||
text.WriteString(r.inline(c))
|
||||
}
|
||||
r.inlineNode(&text, c)
|
||||
}
|
||||
if item := strings.TrimSpace(text.String()); item != "" {
|
||||
b.WriteString(indent + marker + item + "\n")
|
||||
|
||||
@@ -62,6 +62,11 @@ func TestHTMLToMarkdown(t *testing.T) {
|
||||
html: `<body><p><img src="/img/logo.png" alt="Logo"> and <img src="/img/x.png"></p></body>`,
|
||||
want: " and ",
|
||||
},
|
||||
{
|
||||
name: "anchors at block position keep their href",
|
||||
html: `<body><nav><a href="/policy">Policy</a></nav><ul><li><a href="/docs">Docs</a></li></ul></body>`,
|
||||
want: "[Policy](https://page.test/policy)\n\n- [Docs](https://page.test/docs)",
|
||||
},
|
||||
{
|
||||
name: "consecutive blocks collapse to one blank line",
|
||||
html: `<body><div><p>a</p></div><div><p>b</p></div></body>`,
|
||||
|
||||
Reference in new issue
Block a user