Skip to content

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

FIX: Honor `max_oneboxes_per_post` in the rich editor

Régis Hanol195dd1fcc5f2
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 5106ea2928f..d68b594a33d 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3929,6 +3929,7 @@ en:
link_copied: "Link copied!"
load_preview: "Load preview"
preview_failed: "Could not load preview"
+ preview_limit_reached: "This post has reached the maximum number of previews"
onebox_toolbar:
copy: "Copy link"
diff --git a/frontend/discourse/app/static/prosemirror/extensions/link-toolbar.js b/frontend/discourse/app/static/prosemirror/extensions/link-toolbar.js
index 3bd6080b09c..665202085f2 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/link-toolbar.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/link-toolbar.js
@@ -8,7 +8,11 @@ import { rovingButtonBar } from "discourse/lib/roving-button-bar";
import { clipboardCopy } from "discourse/lib/utilities";
import { i18n } from "discourse-i18n";
import { isTopLevel } from "discourse-markdown-it/features/onebox";
-import { oneboxPluginKey, oneboxTypeAtPos } from "./onebox.js";
+import {
+ isPastOneboxLimit,
+ oneboxPluginKey,
+ oneboxTypeAtPos,
+} from "./onebox.js";
const AUTO_LINKS = ["autolink", "linkify"];
const MENU_OFFSET = 12;
@@ -311,6 +315,21 @@ class LinkToolbarPluginView {
return;
}
+ const { siteSettings, toasts } = this.#getContext();
+ if (
+ isPastOneboxLimit(
+ this.#view.state.doc,
+ range.from,
+ siteSettings.max_oneboxes_per_post
+ )
+ ) {
+ toasts.default({
+ duration: "short",
+ data: { message: i18n("composer.link_toolbar.preview_limit_reached") },
+ });
+ return;
+ }
+
this.#resetToolbar();
const { state, dispatch } = this.#view;
diff --git a/frontend/discourse/app/static/prosemirror/extensions/onebox.js b/frontend/discourse/app/static/prosemirror/extensions/onebox.js
index d95a0880262..38ef28e9e06 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/onebox.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/onebox.js
@@ -11,7 +11,7 @@ import {
isWhiteSpace,
} from "discourse/static/prosemirror/lib/markdown-it";
import { i18n } from "discourse-i18n";
-import { isTopLevel } from "discourse-markdown-it/features/onebox";
+import { isTopLevel, ONEBOX_HREF } from "discourse-markdown-it/features/onebox";
export const oneboxPluginKey = new PluginKey("onebox");
@@ -30,7 +30,85 @@ export function oneboxTypeAtPos(doc, pos) {
(!next ||
next.type.name === "hard_break" ||
hasTrailingWhitespaceOnly(doc, pos));
- return $pos.depth === 1 && isAlone ? "full" : "inline";
+ return $pos.depth === 1 && parent.type.name === "paragraph" && isAlone
+ ? "full"
+ : "inline";
+}
+
+const ONEBOX_NODE_TYPES = new Set(["onebox", "onebox_inline"]);
+
+const oneboxSlotsCache = new WeakMap();
+
+function oneboxSlots(doc) {
+ let slots = oneboxSlotsCache.get(doc);
+ if (slots) {
+ return slots;
+ }
+
+ slots = [];
+ doc.descendants((node, pos) => {
+ if (takesOneboxSlot(doc, node, pos)) {
+ slots.push({ pos, order: cookedOrder(doc, pos) });
+ }
+ });
+ slots.sort((a, b) => a.order - b.order);
+
+ oneboxSlotsCache.set(doc, slots);
+ return slots;
+}
+
+function cookedOrder(doc, pos) {
+ const $pos = doc.resolve(pos);
+ for (let depth = $pos.depth; depth > 0; depth--) {
+ const node = $pos.node(depth);
+ if (node.isInline && !node.isLeaf && !node.inlineContent) {
+ return doc.content.size + pos;
+ }
+ }
+ return pos;
+}
+
+function takesOneboxSlot(doc, node, pos) {
+ if (ONEBOX_NODE_TYPES.has(node.type.name)) {
+ return true;
+ }
+
+ const href = node.marks.find(
+ (mark) => mark.type.name === "link" && mark.attrs.markup === "linkify"
+ )?.attrs.href;
+
+ if (!href || !ONEBOX_HREF.test(href)) {
+ return false;
+ }
+
+ return oneboxTypeAtPos(doc, pos) === "full" || !isTopLevel(href);
+}
+
+function oneboxesPastLimit(doc, maxOneboxes) {
+ return new Set(
+ oneboxSlots(doc)
+ .slice(maxOneboxes)
+ .map((slot) => slot.pos)
+ );
+}
+
+function oneboxToLink(state, tr, node, pos, marks) {
+ const { url } = node.attrs;
+ const link = state.schema.marks.link.create({ href: url, markup: "linkify" });
+ const text = state.schema.text(url, link.addToSet(marks));
+
+ tr.replaceWith(
+ tr.mapping.map(pos),
+ tr.mapping.map(pos + node.nodeSize),
+ node.isBlock ? state.schema.nodes.paragraph.create(null, text) : text
+ );
+}
+
+export function isPastOneboxLimit(doc, pos, maxOneboxes) {
+ const order = cookedOrder(doc, pos);
+ return (
+ oneboxSlots(doc).filter((slot) => slot.order < order).length >= maxOneboxes
+ );
}
/** @type {RichEditorExtension} */
@@ -117,17 +195,20 @@ const extension = {
getContext,
}) {
const failedUrls = { full: new Set(), inline: new Set() };
+ const maxOneboxes = () => getContext().siteSettings.max_oneboxes_per_post;
// Scans for linkified URLs and creates onebox loading decorations.
// `forceUrl` bypasses selection/topLevel/failed checks for that URL.
function scanForOneboxLinks(doc, decoSet, tr, forceUrl) {
const decorations = [];
+ const pastLimit = oneboxesPastLimit(doc, maxOneboxes());
doc.descendants((node, pos) => {
const link = node.marks.find((mark) => mark.type.name === "link");
if (
link?.attrs.markup === "linkify" &&
getLinkify().test(node.text) &&
+ !pastLimit.has(pos) &&
decoSet.find(pos, pos + node.nodeSize).length === 0
) {
const isForced = forceUrl === link.attrs.href;
@@ -478,6 +559,7 @@ const extension = {
const tr = state.tr;
const inspected = new Set();
+ const pastLimit = oneboxesPastLimit(state.doc, maxOneboxes());
for (const position of [state.selection.from, leftPos]) {
const { from, to } = topBlockRange(state.doc, position);
@@ -487,7 +569,7 @@ const extension = {
inspected.add(from);
state.doc.nodesBetween(from, to, (node, nodePos) => {
- if (node.type.name !== "onebox_inline") {
+ if (node.type.name !== "onebox_inline" || pastLimit.has(nodePos)) {
return;
}
@@ -499,20 +581,19 @@ const extension = {
return;
}
- const { url } = node.attrs;
- const mark = state.schema.marks.link.create({
- href: url,
- markup: "linkify",
- });
-
- tr.replaceWith(
- tr.mapping.map(nodePos),
- tr.mapping.map(nodePos + node.nodeSize),
- state.schema.text(url, [mark])
- );
+ oneboxToLink(state, tr, node, nodePos, []);
});
}
+ for (const pos of pastLimit) {
+ const node = state.doc.nodeAt(pos);
+ if (!ONEBOX_NODE_TYPES.has(node.type.name)) {
+ continue;
+ }
+
+ oneboxToLink(state, tr, node, pos, node.marks);
+ }
+
return tr.steps.length ? tr.setMeta("addToHistory", false) : null;
},
});
diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js
index f33631242dc..580fd945ebe 100644
--- a/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js
+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js
@@ -1,4 +1,4 @@
-import { settled } from "@ember/test-helpers";
+import { click, settled } from "@ember/test-helpers";
import { setLocalCache } from "pretty-text/oneboxer-cache";
import { NodeSelection, TextSelection } from "prosemirror-state";
import { module, test } from "qunit";
@@ -9,6 +9,7 @@ import { setupRichEditor } from "discourse/tests/helpers/rich-editor-helper";
// Mocked by create-pretender for both /inline-onebox and /onebox.
const URL = "http://www.example.com/has-title.html";
+const OTHER_URL = "http://www.example.com/no-title.html";
function lastParagraphStart(doc) {
let pos = null;
@@ -51,6 +52,109 @@ module(
setLocalCache(testUrl, null);
});
+ test("respects max_oneboxes_per_post site setting", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 0;
+
+ const [editor] = await setupRichEditor(assert, "a\n\nb");
+ const { view } = editor;
+
+ view.dispatch(view.state.tr.insertText(URL, 1, 2));
+ moveCursorTo(view, lastParagraphStart(view.state.doc));
+ await settled();
+
+ assert.dom(".onebox-wrapper").doesNotExist("does not render a onebox");
+ assert
+ .dom("a.inline-onebox")
+ .doesNotExist("does not render an inline onebox");
+ });
+
+ test("each copy of a repeated onebox takes a slot", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 1;
+
+ const [editor] = await setupRichEditor(assert, `${URL}\n\n${URL}\n\nb`);
+
+ moveCursorTo(editor.view, lastParagraphStart(editor.view.state.doc));
+ await settled();
+
+ assert.dom(".onebox-wrapper").exists({ count: 1 });
+ assert.dom(`a[href="${URL}"]`).hasText(URL);
+ });
+
+ test("demotes a onebox pushed past max_oneboxes_per_post by an earlier link", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 1;
+
+ const [editor] = await setupRichEditor(assert, `a\n\n${OTHER_URL}\n\nb`);
+ const { view } = editor;
+
+ moveCursorTo(view, lastParagraphStart(view.state.doc));
+ await settled();
+ assert.dom(".onebox-wrapper").hasText("No title");
+
+ const tr = view.state.tr.insertText(URL, 1, 2);
+ tr.setSelection(TextSelection.atEnd(tr.doc));
+ view.dispatch(tr);
+ await settled();
+
+ assert.dom(".onebox-wrapper").exists({ count: 1 });
+ assert.dom(".onebox-wrapper").includesText("An interesting article");
+ assert.dom(`a[href="${OTHER_URL}"]`).hasText(OTHER_URL);
+ assert.strictEqual(editor.value, `${URL}\n\n${OTHER_URL}\n\nb`);
+ });
+
+ test("links that never cook to a onebox do not take a slot", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 1;
+
+ const [editor] = await setupRichEditor(
+ assert,
+ `# http://www.example.com\n\[email protected]\n\n${URL}\n\nb`
+ );
+
+ moveCursorTo(editor.view, lastParagraphStart(editor.view.state.doc));
+ await settled();
+
+ assert.dom(".onebox-wrapper").exists({ count: 1 });
+ assert.dom(".onebox-wrapper").includesText("An interesting article");
+ });
+
+ test("keeps marks when demoting an inline onebox", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 0;
+
+ const [editor] = await setupRichEditor(assert, "see x here");
+ const { view } = editor;
+ const { schema } = view.state;
+
+ view.dispatch(
+ view.state.tr.replaceWith(5, 6, [
+ schema.nodes.onebox_inline.create(
+ { url: URL, title: "Title" },
+ null,
+ [schema.marks.strong.create()]
+ ),
+ ])
+ );
+ await settled();
+
+ assert.dom("a.inline-onebox").doesNotExist();
+ assert.strictEqual(editor.value, `see **${URL}** here`);
+ });
+
+ test("show preview past max_oneboxes_per_post keeps the link and shows a toast", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 0;
+ const toasts = this.owner.lookup("service:toasts");
+
+ const [editor] = await setupRichEditor(assert, `a\n\n<${URL}>`, {
+ withMenus: true,
+ });
+ moveCursorTo(editor.view, lastParagraphStart(editor.view.state.doc) + 5);
+ await settled();
+
+ await click(".composer-link-toolbar__show-preview");
+
+ assert.dom(".onebox-wrapper").doesNotExist();
+ assert.strictEqual(editor.value, `a\n\n<${URL}>`);
+ assert.strictEqual(toasts.activeToasts.length, 1);
+ });
+
test("holds a lone link inline while the cursor is on its line", async function (assert) {
const [editor] = await setupRichEditor(assert, "a\n\nb");
const { view } = editor;
diff --git a/plugins/footnote/test/javascripts/integration/rich-editor-onebox-test.js b/plugins/footnote/test/javascripts/integration/rich-editor-onebox-test.js
new file mode 100644
index 00000000000..658900aadb4
--- /dev/null
+++ b/plugins/footnote/test/javascripts/integration/rich-editor-onebox-test.js
@@ -0,0 +1,68 @@
+import { settled } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import {
+ registerRichEditorExtension,
+ resetRichEditorExtensions,
+} from "discourse/lib/composer/rich-editor-extensions";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { setupRichEditor } from "discourse/tests/helpers/rich-editor-helper";
+import richEditorExtension from "discourse/plugins/footnote/lib/rich-editor-extension";
+
+const URL = "http://www.example.com/has-title.html";
+const OTHER_URL = "http://www.example.com/no-title.html";
+
+function inlineOneboxTitles(doc) {
+ const titles = [];
+ doc.descendants((node) => {
+ if (node.type.name === "onebox_inline") {
+ titles.push(node.attrs.title);
+ }
+ });
+ return titles;
+}
+
+async function setupEditorWithFootnoteLink(assert) {
+ let textManipulation;
+ await setupRichEditor(assert, `note^[${URL}]\n\n${OTHER_URL}\n\nb`, {
+ onSetup: (value) => (textManipulation = value),
+ });
+ const { view } = textManipulation;
+
+ textManipulation.selectText(view.state.doc.content.size - 1, 0);
+ await settled();
+
+ return view;
+}
+
+module(
+ "Integration | Component | ProsemirrorEditor | Footnote oneboxes",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(async function () {
+ await resetRichEditorExtensions();
+ registerRichEditorExtension(richEditorExtension);
+ });
+
+ test("counts footnote links after the main body for max_oneboxes_per_post", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 1;
+
+ const view = await setupEditorWithFootnoteLink(assert);
+
+ assert.dom(".onebox-wrapper").exists({ count: 1 });
+ assert.dom(".onebox-wrapper").hasText("No title");
+ assert.deepEqual(inlineOneboxTitles(view.state.doc), []);
+ });
+
+ test("previews footnote links within max_oneboxes_per_post", async function (assert) {
+ this.siteSettings.max_oneboxes_per_post = 2;
+
+ const view = await setupEditorWithFootnoteLink(assert);
+
+ assert.dom(".onebox-wrapper").hasText("No title");
+ assert.deepEqual(inlineOneboxTitles(view.state.doc), [
+ "This is a great title",
+ ]);
+ });
+ }
+);