Skip to content

Commit

Permalink
Keep other table rows and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-walker authored and fguillot committed Apr 3, 2023
1 parent 49d2596 commit 8b6dd3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
29 changes: 15 additions & 14 deletions reader/rewrite/rewrite_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,25 +342,26 @@ func removeTables(entryContent string) string {
return entryContent
}

var table *goquery.Selection
selectors := []string{"table", "tbody", "thead", "td", "th", "td"}

for {
table = doc.Find("table").First()
var loopElement *goquery.Selection

if table.Length() == 0 {
break
}

td := table.Find("td").First()
for _, selector := range selectors {
for {
loopElement = doc.Find(selector).First()

if td.Length() == 0 {
break
}
if loopElement.Length() == 0 {
break
}

tdHtml, _ := td.Html()
innerHtml, err := loopElement.Html()
if err != nil {
break
}

table.Parent().AppendHtml(tdHtml)
table.Remove()
loopElement.Parent().AppendHtml(innerHtml)
loopElement.Remove()
}
}

output, _ := doc.Find("body").First().Html()
Expand Down
4 changes: 2 additions & 2 deletions reader/rewrite/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ func TestRewriteBase64DecodeArgs(t *testing.T) {
}

func TestRewriteRemoveTables(t *testing.T) {
content := `<table class="container"><tbody><tr><td><p>Test</p><table class="row"><tbody><tr><td>Hello World!</td></tr></tbody></table></td></tr></tbody></table>`
expected := `<p>Test</p>Hello World!`
content := `<table class="container"><tbody><tr><td><p>Test</p><table class="row"><tbody><tr><td><p>Hello World!</p></td><td><p>Test</p></td></tr></tbody></table></td></tr></tbody></table>`
expected := `<p>Test</p><p>Hello World!</p><p>Test</p>`
output := Rewriter("https://example.org/article", content, `remove_tables`)

if expected != output {
Expand Down

0 comments on commit 8b6dd3e

Please sign in to comment.