Skip to content

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

FEATURE: add mention rich editor extension (#31142)

Renato Atilio5a21f7471628
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js
new file mode 100644
index 00000000000..8c88c81f7a9
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js
@@ -0,0 +1,83 @@
+import { mentionRegex } from "pretty-text/mentions";
+import { isBoundary } from "discourse/static/prosemirror/lib/markdown-it";
+
+/** @type {RichEditorExtension} */
+const extension = {
+ nodeSpec: {
+ mention: {
+ attrs: { name: {} },
+ inline: true,
+ group: "inline",
+ content: "text*",
+ atom: true,
+ draggable: true,
+ selectable: false,
+ parseDOM: [
+ {
+ tag: "a.mention",
+ preserveWhitespace: "full",
+ getAttrs: (dom) => {
+ return { name: dom.getAttribute("data-name") };
+ },
+ },
+ ],
+ toDOM: (node) => {
+ return [
+ "a",
+ { class: "mention", "data-name": node.attrs.name },
+ `@${node.attrs.name}`,
+ ];
+ },
+ },
+ },
+
+ inputRules: [
+ {
+ // TODO(renato): pass unicodeUsernames?
+ match: new RegExp(`(^|\\W)(${mentionRegex().source}) $`),
+ handler: (state, match, start, end) => {
+ if (
+ state.selection.$from.nodeBefore?.type === state.schema.nodes.mention
+ ) {
+ return null;
+ }
+ const mentionStart = start + match[1].length;
+ const name = match[2].slice(1);
+ return state.tr.replaceWith(mentionStart, end, [
+ state.schema.nodes.mention.create({ name }),
+ state.schema.text(" "),
+ ]);
+ },
+ options: { undoable: false },
+ },
+ ],
+
+ parse: {
+ mention: {
+ block: "mention",
+ getAttrs: (token, tokens, i) => ({
+ // this is not ideal, but working around the mention_open/close structure
+ // a text is expected just after the mention_open token
+ name: tokens[i + 1].content.slice(1),
+ }),
+ },
+ },
+
+ serializeNode: {
+ mention: (state, node, parent, index) => {
+ if (!isBoundary(state.out, state.out.length - 1)) {
+ state.write(" ");
+ }
+
+ state.write(`@${node.attrs.name}`);
+
+ const nextSibling =
+ parent.childCount > index + 1 ? parent.child(index + 1) : null;
+ if (nextSibling?.isText && !isBoundary(nextSibling.text, 0)) {
+ state.write(" ");
+ }
+ },
+ },
+};
+
+export default extension;
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
index bf0c5495105..17aabf36086 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
@@ -1,5 +1,6 @@
import { registerRichEditorExtension } from "discourse/lib/composer/rich-editor-extensions";
import hashtag from "./hashtag";
+import mention from "./mention";
import underline from "./underline";
/**
@@ -8,7 +9,7 @@ import underline from "./underline";
*
* @type {RichEditorExtension[]}
*/
-const defaultExtensions = [hashtag, underline];
+const defaultExtensions = [hashtag, mention, underline];
defaultExtensions.forEach(registerRichEditorExtension);
diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/mention-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/mention-test.gjs
new file mode 100644
index 00000000000..99435675347
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/mention-test.gjs
@@ -0,0 +1,35 @@
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
+
+module(
+ "Integration | Component | prosemirror-editor - mention extension",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ const testCases = {
+ hashtag: [
+ [
+ "@hello",
+ '<p><a class="mention" data-name="hello" contenteditable="false" draggable="true">@hello</a></p>',
+ "@hello",
+ ],
+ [
+ "Hello @dude",
+ '<p>Hello <a class="mention" data-name="dude" contenteditable="false" draggable="true">@dude</a></p>',
+ "Hello @dude",
+ ],
+ ],
+ };
+
+ Object.entries(testCases).forEach(([name, tests]) => {
+ tests.forEach(([markdown, expectedHtml, expectedMarkdown]) => {
+ test(name, async function (assert) {
+ this.siteSettings.rich_editor = true;
+
+ await testMarkdown(assert, markdown, expectedHtml, expectedMarkdown);
+ });
+ });
+ });
+ }
+);