From 98251dc128cdbdff3541579d778404d740102040 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Tue, 1 Dec 2026 08:59:12 +0100 Subject: [PATCH] Keep each rule match's own tags in the extracted content The rule path joined the inner HTML of every match, so a rule that selects headings and paragraph blocks, such as Apple's newsroom rule with .pagebody-header and .pagebody-copy, lost the h2 and div around them: section titles became bare bold text and the paragraphs ran together. Each match now keeps its own element. --- extractor/readability.go | 6 +++++- extractor/selector_test.go | 27 +++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/extractor/readability.go b/extractor/readability.go index c4a8a7f7..baae1375 100644 --- a/extractor/readability.go +++ b/extractor/readability.go @@ -260,9 +260,13 @@ func extractWithSelector(body, selector string, excludes []string) (string, erro }) excluded.Remove() + // each match keeps its own tags, so a heading stays a heading and a paragraph block stays a block var parts []string kept.Each(func(_ int, s *goquery.Selection) { - if h, err := s.Html(); err == nil && h != "" { + if inner, err := s.Html(); err != nil || inner == "" { + return + } + if h, err := goquery.OuterHtml(s); err == nil { parts = append(parts, h) } }) diff --git a/extractor/selector_test.go b/extractor/selector_test.go index 6ee84586..d7c676fc 100644 --- a/extractor/selector_test.go +++ b/extractor/selector_test.go @@ -20,15 +20,15 @@ func TestExtractWithSelectorExcludes(t *testing.T) { excludes []string want string }{ - {name: "no excludes", want: "

first

by someone
\n

second

"}, + {name: "no excludes", want: "

first

by someone
share
\n

second

share again
"}, {name: "blank excludes change nothing", excludes: []string{"", " "}, - want: "

first

by someone
\n

second

"}, - {name: "one exclude", excludes: []string{".t-meta"}, want: "

first

\n

second

"}, - {name: "every match loses its excludes", excludes: []string{".t-meta", "footer", ""}, want: "

first

\n

second

"}, + want: "

first

by someone
share
\n

second

share again
"}, + {name: "one exclude", excludes: []string{".t-meta"}, want: "

first

share
\n

second

share again
"}, + {name: "every match loses its excludes", excludes: []string{".t-meta", "footer", ""}, want: "

first

\n

second

"}, {name: "exclude padded with spaces the selector parser keeps", excludes: []string{"\u00a0footer\v"}, - want: "

first

by someone
\n

second

"}, + want: "

first

by someone
\n

second

"}, {name: "excludes match the page as it is", excludes: []string{".t-meta", "p:first-child + footer"}, - want: "

first

\n

second

"}, // removing .t-meta first would also expose the first footer + want: "

first

share
\n

second

"}, // removing .t-meta first would also expose the first footer } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -44,7 +44,7 @@ func TestExtractWithSelectorNestedMatchInExcluded(t *testing.T) { `` got, err := extractWithSelector(page, ".post", []string{".related"}) require.NoError(t, err) - assert.Equal(t, "

Main

", got) + assert.Equal(t, `

Main

`, got) } func TestGetContentCustomExcludes(t *testing.T) { @@ -54,7 +54,18 @@ func TestGetContentCustomExcludes(t *testing.T) { rule := &datastore.Rule{Content: ".post", Excludes: []string{".wp-embedded-content"}} content, rich := lr.getContent(context.Background(), page, "https://techcrunch.com/post", rule) - assert.Equal(t, "

The article text.

", rich) + assert.Equal(t, `

The article text.

`, rich) assert.Equal(t, rich, content) assert.Equal(t, "The article text.", lr.getText(content, "Post")) } + +func TestExtractWithSelectorKeepsTags(t *testing.T) { + // each match keeps its own tags: a heading stays a heading and a paragraph block stays a block + const page = `
First paragraph.

` + + `A section

Second paragraph.
` + + `
` + got, err := extractWithSelector(page, ".pagebody-header, .pagebody-copy", nil) + require.NoError(t, err) + assert.Equal(t, `
First paragraph.
`+"\n"+`

A section

`+ + "\n"+`
Second paragraph.
`, got) +}