A platform for community discussion. Free, open, simple.
FIX: Honor `max_oneboxes_per_post` in the markdown composer preview
Régis Hanolff8b95a26c19
diff --git a/frontend/discourse-markdown-it/src/features/onebox.js b/frontend/discourse-markdown-it/src/features/onebox.jsindex c623b0542d9..309ec0a936c 100644--- a/frontend/discourse-markdown-it/src/features/onebox.js+++ b/frontend/discourse-markdown-it/src/features/onebox.js@@ -4,6 +4,10 @@ import { lookupCache } from "pretty-text/oneboxer-cache";const ONEBOX = 1;const INLINE = 2;+const NO_ONEBOX_LIMIT = "none";++export const ONEBOX_HREF = /^http|^\/\//i;+export function isTopLevel(href) {let split = href.split(/https?:\/\/[^\/]+[\/?]/i);let hasExtra = split && split[1] && split[1].length > 0;@@ -15,6 +19,9 @@ function applyOnebox(state, silent) {return;}+ const { maxOneboxes } = state.md.options.discourse;+ let oneboxes = 0;+for (let i = 1; i < state.tokens.length; i++) {let token = state.tokens[i];let prev = state.tokens[i - 1];@@ -63,7 +70,7 @@ function applyOnebox(state, silent) {let href = attrs[0][1];// edge case ... what if this is not http or protocolless?- if (!/^http|^\/\//i.test(href)) {+ if (!ONEBOX_HREF.test(href)) {continue;}@@ -72,6 +79,16 @@ function applyOnebox(state, silent) {continue;}+ if (mode === INLINE && isTopLevel(href)) {+ continue;+ }++ if (oneboxes >= maxOneboxes) {+ continue;+ }++ oneboxes++;+if (mode === ONEBOX) {// we already determined earlier that 0 0 was hreflet cached = lookupCache(attrs[0][1]);@@ -95,7 +112,7 @@ function applyOnebox(state, silent) {attrs.push(["class", "onebox"]);attrs.push(["target", "_blank"]);}- } else if (mode === INLINE && !isTopLevel(href)) {+ } else {const onebox = cachedInlineOnebox(href);if (onebox && onebox.title) {@@ -115,6 +132,13 @@ function applyOnebox(state, silent) {}export function setup(helper) {+ helper.registerOptions((opts, siteSettings, state) => {+ opts.maxOneboxes =+ state.oneboxLimit === NO_ONEBOX_LIMIT+ ? Infinity+ : siteSettings.max_oneboxes_per_post;+ });+helper.registerPlugin((md) => {md.core.ruler.after("linkify", "onebox", applyOnebox);});diff --git a/frontend/discourse/app/lib/load-oneboxes.js b/frontend/discourse/app/lib/load-oneboxes.jsindex be810aa801d..706828e3b4a 100644--- a/frontend/discourse/app/lib/load-oneboxes.js+++ b/frontend/discourse/app/lib/load-oneboxes.js@@ -1,5 +1,5 @@import { applyInlineOneboxes } from "pretty-text/inline-oneboxer";-import { load, LOADING_ONEBOX_CSS_CLASS } from "pretty-text/oneboxer";+import { load } from "pretty-text/oneboxer";export function loadOneboxes(container,@@ -17,9 +17,7 @@ export function loadOneboxes(// Oneboxes = `a.onebox` -> `a.onebox-loading` -> `aside.onebox`// Inline Oneboxes = `a.inline-onebox-loading` -> `a.inline-onebox`- let loadedOneboxes = container.querySelectorAll(- `aside.onebox, a.${LOADING_ONEBOX_CSS_CLASS}, a.inline-onebox`- ).length;+ let loadedOneboxes = 0;container.querySelectorAll("a.onebox, a.inline-onebox-loading")diff --git a/frontend/discourse/tests/acceptance/composer-onebox-test.js b/frontend/discourse/tests/acceptance/composer-onebox-test.jsindex ec5bcf5b199..81a3d9abcea 100644--- a/frontend/discourse/tests/acceptance/composer-onebox-test.js+++ b/frontend/discourse/tests/acceptance/composer-onebox-test.js@@ -1,9 +1,18 @@import { click, fillIn, visit } from "@ember/test-helpers";+import { applyCachedInlineOnebox } from "pretty-text/inline-oneboxer";+import { setLocalCache } from "pretty-text/oneboxer-cache";import { test } from "qunit";import { withPluginApi } from "discourse/lib/plugin-api";import pretender from "discourse/tests/helpers/create-pretender";import { acceptance } from "discourse/tests/helpers/qunit-helpers";+function cacheOnebox(name, text) {+ const onebox = document.createElement("aside");+ onebox.className = "onebox";+ onebox.textContent = text;+ setLocalCache(`http://www.example.com/${name}.html`, onebox);+}+acceptance("Composer - Onebox", function (needs) {needs.user();needs.settings({@@ -34,10 +43,101 @@ http://www.example.com/has-title.htmlassert.dom(".d-editor-preview").hasHtml(`<p></p><aside class=\"onebox\"><article class=\"onebox-body\"><h3><a href=\"http://www.example.com/article.html\" tabindex=\"-1\">An interesting article</a></h3></article></aside><br>This is another test <a href=\"http://www.example.com/has-title.html\" class=\"inline-onebox\" tabindex=\"-1\">This is a great title</a><p></p>-<p><a href=\"http://www.example.com/no-title.html\" class=\"onebox\" target=\"_blank\" tabindex=\"-1\">http://www.example.com/no-title.html</a></p>-<p>This is another test <a href=\"http://www.example.com/no-title.html\" class=\"\" tabindex=\"-1\">http://www.example.com/no-title.html</a><br>-This is another test <a href=\"http://www.example.com/has-title.html\" class=\"inline-onebox\" tabindex=\"-1\">This is a great title</a></p>-<p></p><aside class=\"onebox\"><article class=\"onebox-body\"><h3><a href=\"http://www.example.com/article.html\" tabindex=\"-1\">An interesting article</a></h3></article></aside><p></p>`);+<p><a href=\"http://www.example.com/no-title.html\" tabindex=\"-1\">http://www.example.com/no-title.html</a></p>+<p>This is another test <a href=\"http://www.example.com/no-title.html\" tabindex=\"-1\">http://www.example.com/no-title.html</a><br>+This is another test <a href=\"http://www.example.com/has-title.html\" tabindex=\"-1\">http://www.example.com/has-title.html</a></p>+<p><a href=\"http://www.example.com/has-title.html\" tabindex=\"-1\">http://www.example.com/has-title.html</a></p>`);+ });++ test("cached oneboxes respect max_oneboxes_per_post site setting", async function (assert) {+ ["first", "second", "third"].forEach((name) => cacheOnebox(name, name));+ applyCachedInlineOnebox("http://www.example.com/inline.html", {+ title: "Inline title",+ });++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await fillIn(+ ".d-editor-input",+ `http://www.example.com/first.html++http://www.example.com/second.html++http://www.example.com/third.html++See http://www.example.com/inline.html`+ );++ assert.dom(".d-editor-preview aside.onebox").exists({ count: 2 });+ assert.dom(".d-editor-preview a.inline-onebox").doesNotExist();+ });++ test("cached oneboxes do not take the slots of earlier oneboxes", async function (assert) {+ ["first", "second"].forEach((name) => cacheOnebox(name, `Cached ${name}`));++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await fillIn(+ ".d-editor-input",+ `http://www.example.com/has-title.html++http://www.example.com/first.html++http://www.example.com/second.html`+ );++ assert.dom(".d-editor-preview aside.onebox").exists({ count: 2 });+ assert.dom(".d-editor-preview").includesText("An interesting article");+ assert.dom(".d-editor-preview").includesText("Cached first");+ assert.dom(".d-editor-preview").doesNotIncludeText("Cached second");+ });++ test("repeated oneboxes each take a slot", async function (assert) {+ cacheOnebox("first", "Cached first");++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await fillIn(+ ".d-editor-input",+ `http://www.example.com/first.html++http://www.example.com/first.html++http://www.example.com/has-title.html`+ );++ assert.dom(".d-editor-preview aside.onebox").exists({ count: 2 });+ assert+ .dom(".d-editor-preview a[href='http://www.example.com/has-title.html']")+ .doesNotHaveClass("onebox");+ assert+ .dom(".d-editor-preview")+ .doesNotIncludeText("An interesting article");+ });++ test("cached oneboxes that are not asides take a slot", async function (assert) {+ const video = document.createElement("div");+ video.className = "youtube-onebox";+ setLocalCache("http://www.example.com/first.html", video);++ cacheOnebox("second", "");++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await fillIn(+ ".d-editor-input",+ `http://www.example.com/first.html++http://www.example.com/second.html++http://www.example.com/has-title.html`+ );++ assert.dom(".d-editor-preview .youtube-onebox").exists();+ assert.dom(".d-editor-preview aside.onebox").exists({ count: 1 });+ assert+ .dom(".d-editor-preview")+ .doesNotIncludeText("An interesting article");});test("decorates oneboxes once they load", async function (assert) {diff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex 6aea62899de..01414b88dc5 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -19,6 +19,7 @@ module PrettyTextBLOCKED_HOTLINKED_SRC_ATTR = "data-blocked-hotlinked-src"BLOCKED_HOTLINKED_SRCSET_ATTR = "data-blocked-hotlinked-srcset"+ NO_ONEBOX_LIMIT = "none"VIMEO_PLAYER_HOST = "player.vimeo.com"VIMEO_PLAYER_PATH = %r{\A/video/(?<id>\d+)/?\z}@@ -208,6 +209,7 @@ module PrettyTextopt_input[:topicId] = opts[:topic_id].to_i if opts[:topic_id]opt_input[:postId] = opts[:post_id].to_i if opts[:post_id]opt_input[:forceQuoteLink] = opts[:force_quote_link] if opts[:force_quote_link]+ opt_input[:oneboxLimit] = opts[:onebox_limit] if opts[:onebox_limit]opt_input[:userId] = opts[:user_id].to_i if opts[:user_id]# Be careful disabling sanitization. We allow for custom emailsopt_input[:disableSanitizer] = true if opts[:sanitize] == falsediff --git a/migrations/converters/lib/migrations/converters/markdown_engine/config.rb b/migrations/converters/lib/migrations/converters/markdown_engine/config.rbindex a98371e77a7..c77ab1cbcab 100644--- a/migrations/converters/lib/migrations/converters/markdown_engine/config.rb+++ b/migrations/converters/lib/migrations/converters/markdown_engine/config.rb@@ -40,6 +40,7 @@ module Migrationsexternal_emoji_urlmarkdown_linkify_tldsmarkdown_typographer_quotation_marks+ max_oneboxes_per_postpolicy_enabledpoll_enabledpoll_maximum_options