Skip to content

A platform for community discussion. Free, open, simple.

FIX: Disallow table cells to be weighted actual articles can be main content (#27508)

Natalie Tay489aac3fddce
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 07af6848537..53466cbcb33 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -163,9 +163,31 @@ class TopicEmbed < ActiveRecord::Base
require "ruby-readability"
opts = {
- tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote figure figcaption],
+ tags: %w[
+ div
+ p
+ code
+ pre
+ h1
+ h2
+ h3
+ b
+ em
+ i
+ strong
+ a
+ img
+ ul
+ li
+ ol
+ blockquote
+ figure
+ figcaption
+ details
+ ],
attributes: %w[href src class],
remove_empty_nodes: false,
+ elements_to_score: %w[p],
}
opts[
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 98861813137..b9888fcdbce 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -23,7 +23,7 @@ RSpec.describe TopicEmbed do
expect(TopicEmbed.count).to eq(0)
end
- it "Allows figure and figcaption HTML tags" do
+ it "Allows figure, figcaption, details HTML tags" do
html = <<~HTML
<html>
<head>
@@ -35,7 +35,10 @@ RSpec.describe TopicEmbed do
<figure>
<img src="/a.png">
<figcaption>Some caption</figcaption>
- <figure>
+ </figure>
+ <details>
+ some details
+ </details>
</div>
</body>
</html>
@@ -51,13 +54,60 @@ RSpec.describe TopicEmbed do
<figure>
<img src="https://blog.discourse.com/a.png">
<figcaption>Some caption</figcaption>
- <figure>
- </figure></figure></div>
+ </figure>
+ <details>
+ some details
+ </details>
+ </div>
</div></div>
HTML
expect(parsed.body.strip).to eq(expected.strip)
end
+ # ideally, articles get a heavier weightage than td elements
+ # so to force that, we do not allow td elements to be scored
+ it "does not score td tags" do
+ html = <<~HTML
+ <html>
+ <head>
+ <title>Some title</title>
+ </head>
+ <body>
+ <article>
+ article content
+ <table>
+ <tr>
+ <td>
+ <p>cats</p>
+ <p>cats</p>
+ </td>
+ </tr>
+ </table>
+ </article>
+ </body>
+ </html>
+ HTML
+
+ parsed = TopicEmbed.parse_html(html, "https://blog.discourse.com/somepost.html")
+
+ expected = <<-HTML
+ <div><div>
+
+ article content
+
+
+
+ cats
+ cats
+
+
+
+
+</div></div>
+ HTML
+ expect(parsed.body.strip).to eq(expected.strip)
+ end
+
context "when creating a post" do
let!(:post) { TopicEmbed.import(user, url, title, contents) }
let(:topic_embed) { TopicEmbed.find_by(post: post) }