first
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
\nsecond
"}, + {name: "no excludes", want: "first
second
first
\nsecond
"}, - {name: "one exclude", excludes: []string{".t-meta"}, want: "first
\nsecond
"}, - {name: "every match loses its excludes", excludes: []string{".t-meta", "footer", ""}, want: "first
\nsecond
"}, + want: "first
second
first
second
first
second
first
\nsecond
"}, + want: "first
second
first
\nsecond
"}, // removing .t-meta first would also expose the first footer + want: "first
second