A platform for community discussion. Free, open, simple.
FIX: Respect invalidate_oneboxes option for inline oneboxes
Vinoth Kannan24a14af15af6
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6index ee3588f9fdf..f382c11cd8a 100644--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6@@ -100,7 +100,7 @@ function applyOnebox(state, silent) {let options = state.md.options.discourse;if (options.lookupInlineOnebox) {- onebox = options.lookupInlineOnebox(href);+ onebox = options.lookupInlineOnebox(href, options.invalidateOneboxes);}if (onebox && onebox.title) {diff --git a/app/assets/javascripts/pretty-text/pretty-text.js.es6 b/app/assets/javascripts/pretty-text/pretty-text.js.es6index c535ef01ad4..7050e38aa17 100644--- a/app/assets/javascripts/pretty-text/pretty-text.js.es6+++ b/app/assets/javascripts/pretty-text/pretty-text.js.es6@@ -31,7 +31,8 @@ export function buildOptions(state) {previewing,linkify,censoredWords,- mentionLookup+ mentionLookup,+ invalidateOneboxes} = state;let features = {@@ -80,7 +81,8 @@ export function buildOptions(state) {markdownIt: true,injectLineNumbersToPreview:siteSettings.enable_advanced_editor_preview_sync,- previewing+ previewing,+ invalidateOneboxes};// note, this will mutate options due to the way the API is designeddiff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rbindex 7ee57b432e9..1ba26830165 100644--- a/lib/inline_oneboxer.rb+++ b/lib/inline_oneboxer.rb@@ -23,8 +23,9 @@ class InlineOneboxerdef self.lookup(url, opts = nil)opts ||= {}+ opts = opts.with_indifferent_access- unless opts[:skip_cache]+ unless opts[:skip_cache] || opts[:invalidate]cached = cache_lookup(url)return cached if cached.present?enddiff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex cf6a3eefa4b..f43dcdbd934 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -172,6 +172,10 @@ module PrettyTextbuffer << "__optInput.userId = #{opts[:user_id].to_i};\n"end+ if opts[:invalidate_oneboxes]+ buffer << "__optInput.invalidateOneboxes = true;\n"+ end+buffer << "__textOptions = __buildOptions(__optInput);\n"buffer << ("__pt = new __PrettyText(__textOptions);")diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rbindex dcb42d76d47..b8376a731cf 100644--- a/lib/pretty_text/helpers.rb+++ b/lib/pretty_text/helpers.rb@@ -77,8 +77,8 @@ module PrettyTextresultend- def lookup_inline_onebox(url)- InlineOneboxer.lookup(url)+ def lookup_inline_onebox(url, opts = {})+ InlineOneboxer.lookup(url, opts)enddef get_topic_info(topic_id)diff --git a/lib/pretty_text/shims.js b/lib/pretty_text/shims.jsindex 46ce2595873..9631d260f8a 100644--- a/lib/pretty_text/shims.js+++ b/lib/pretty_text/shims.js@@ -49,8 +49,14 @@ function __getURL(url) {return url;}-function __lookupInlineOnebox(url) {- return __helpers.lookup_inline_onebox(url);+function __lookupInlineOnebox(url, invalidate = false) {+ const opts = {};++ if (invalidate) {+ opts["invalidate"] = true;+ }++ return __helpers.lookup_inline_onebox(url, opts);}function __lookupImageUrls(urls) {diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rbindex 4abf96d50bd..fe0a6686a01 100644--- a/spec/components/pretty_text_spec.rb+++ b/spec/components/pretty_text_spec.rb@@ -1195,6 +1195,25 @@ HTMLexpect(PrettyText.cook(raw)).to eq(cooked.strip)end++ it "invalidates the onebox url" do+ topic = Fabricate(:topic)+ url = topic.url+ raw = "Hello #{url}"++ PrettyText.cook(raw)++ topic.title = "Updated: #{topic.title}"+ topic.save++ cooked = <<~HTML+ <p>Hello <a href="#{url}">#{topic.title}</a></p>+ HTML++ expect(PrettyText.cook(raw)).not_to eq(cooked.strip)+ expect(PrettyText.cook(raw, invalidate_oneboxes: true)).to eq(cooked.strip)+ expect(PrettyText.cook(raw)).to eq(cooked.strip)+ endenddescribe "image decoding" do