FEATURE: Ability to scrub titles when importing embeddable content

This commit is contained in:
Robin Ward 2016-08-22 12:43:02 -04:00
parent aff1efc1f8
commit 884bdf7240
8 changed files with 45 additions and 6 deletions

View File

@ -38,9 +38,7 @@ export default Ember.Controller.extend({
const updates = embedding.getProperties(embedding.get('fields'));
this.set('saved', false);
this.get('embedding').update(updates).then(() => {
this.set('saved', true);
}).catch(popupAjaxError);
this.get('embedding').update(updates).then(() => this.set('saved', true)).catch(popupAjaxError);
},
addHost() {

View File

@ -30,6 +30,9 @@
{{embedding-setting field="embed_by_username" value=embedding.embed_by_username}}
{{embedding-setting field="embed_post_limit" value=embedding.embed_post_limit}}
{{embedding-setting field="embed_title_scrubber"
value=embedding.embed_title_scrubber
placeholder="- site.com$"}}
{{embedding-setting field="embed_truncate" value=embedding.embed_truncate type="checkbox"}}
</div>

View File

@ -6,6 +6,7 @@ class Embedding < OpenStruct
def self.settings
%i(embed_by_username
embed_post_limit
embed_title_scrubber
embed_truncate
embed_whitelist_selector
embed_blacklist_selector

View File

@ -79,7 +79,14 @@ class TopicEmbed < ActiveRecord::Base
doc = Readability::Document.new(open(url).read, opts)
tags = {'img' => 'src', 'script' => 'src', 'a' => 'href'}
title = doc.title
title = doc.title || ''
title.strip!
if SiteSetting.embed_title_scrubber.present?
title.sub!(Regexp.new(SiteSetting.embed_title_scrubber), '')
title.strip!
end
doc = Nokogiri::HTML(doc.content)
doc.search(tags.keys.join(',')).each do |node|
url_param = tags[node.name]

View File

@ -3120,6 +3120,7 @@ en:
embed_by_username: "Username for topic creation"
embed_post_limit: "Maximum number of posts to embed"
embed_username_key_from_feed: "Key to pull discourse username from feed"
embed_title_scrubber: "Regular expression used to scrub the title of posts"
embed_truncate: "Truncate the embedded posts"
embed_whitelist_selector: "CSS selector for elements that are allowed in embeds"
embed_blacklist_selector: "CSS selector for elements that are removed from embeds"

View File

@ -1317,6 +1317,7 @@ en:
feed_polling_url: "EMBEDDING ONLY: URL of RSS/ATOM feed to embed."
embed_by_username: "Discourse username of the user who creates the embedded topics."
embed_username_key_from_feed: "Key to pull discourse username from feed."
embed_title_scrubber: "Regular expression for scrubbing embeddable titles."
embed_truncate: "Truncate the embedded posts."
embed_post_limit: "Maximum number of posts to embed."
embed_username_required: "The username for topic creation is required."

View File

@ -973,6 +973,9 @@ embedding:
embed_post_limit:
default: 100
hidden: true
embed_title_scrubber:
default: ''
hidden: true
embed_truncate:
default: true
hidden: true

View File

@ -62,6 +62,31 @@ describe TopicEmbed do
describe '.find_remote' do
context ".title_scrub" do
let(:url) { 'http://eviltrout.com/123' }
let(:contents) { "<title>Through the Looking Glass - Classic Books</title><body>some content here</body>" }
let!(:embeddable_host) { Fabricate(:embeddable_host) }
let!(:file) { StringIO.new }
before do
file.stubs(:read).returns contents
TopicEmbed.stubs(:open).returns file
end
it "doesn't scrub the title by default" do
title, _ = TopicEmbed.find_remote(url)
expect(title).to eq("Through the Looking Glass - Classic Books")
end
it "scrubs the title when the option is enabled" do
SiteSetting.embed_title_scrubber = " - Classic Books$"
title, _ = TopicEmbed.find_remote(url)
expect(title).to eq("Through the Looking Glass")
end
end
context 'post with allowed classes "foo" and "emoji"' do
let(:user) { Fabricate(:user) }
@ -76,7 +101,7 @@ describe TopicEmbed do
SiteSetting.stubs(:embed_classname_whitelist).returns 'emoji , foo'
file.stubs(:read).returns contents
TopicEmbed.stubs(:open).returns file
title, content = TopicEmbed.find_remote(url)
_, content = TopicEmbed.find_remote(url)
end
it 'img node has emoji class' do
@ -111,7 +136,7 @@ describe TopicEmbed do
SiteSetting.stubs(:embed_classname_whitelist).returns ' '
file.stubs(:read).returns contents
TopicEmbed.stubs(:open).returns file
title, content = TopicEmbed.find_remote(url)
_, content = TopicEmbed.find_remote(url)
end
it 'img node doesn\'t have emoji class' do