Skip to content

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

FEATURE: add hashtag rich editor extension (#31141)

Renato Atiliob43e46566909
diff --git a/app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.js b/app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.js
index 2bc42fb595d..166f8562c80 100644
--- a/app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.js
+++ b/app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.js
@@ -93,10 +93,18 @@ export function registerRichEditorExtension(extension) {
registeredExtensions.push(extension);
}
-export function resetRichEditorExtensions() {
+export function clearRichEditorExtensions() {
registeredExtensions.length = 0;
}
+export async function resetRichEditorExtensions() {
+ const { default: extensions } = await import(
+ "discourse/static/prosemirror/extensions/register-default"
+ );
+ clearRichEditorExtensions();
+ extensions.forEach(registerRichEditorExtension);
+}
+
/**
* Get all extensions registered for the rich editor
*
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/hashtag.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/hashtag.js
new file mode 100644
index 00000000000..1888dd7edcc
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/hashtag.js
@@ -0,0 +1,86 @@
+import { isBoundary } from "discourse/static/prosemirror/lib/markdown-it";
+
+/** @type {RichEditorExtension} */
+const extension = {
+ nodeSpec: {
+ hashtag: {
+ attrs: { name: {} },
+ inline: true,
+ group: "inline",
+ content: "text*",
+ atom: true,
+ draggable: true,
+ selectable: false,
+ parseDOM: [
+ {
+ tag: "a.hashtag-cooked",
+ preserveWhitespace: "full",
+ getAttrs: (dom) => {
+ return { name: dom.getAttribute("data-name") };
+ },
+ },
+ ],
+ toDOM: (node) => {
+ return [
+ "a",
+ { class: "hashtag-cooked", "data-name": node.attrs.name },
+ `#${node.attrs.name}`,
+ ];
+ },
+ },
+ },
+
+ inputRules: [
+ {
+ match: /(^|\W)(#[\u00C0-\u1FFF\u2C00-\uD7FF\w:-]{1,101})\s$/,
+ handler: (state, match, start, end) => {
+ const hashtagStart = start + match[1].length;
+ const name = match[2].slice(1);
+ return (
+ state.selection.$from.nodeBefore?.type !==
+ state.schema.nodes.hashtag &&
+ state.tr.replaceWith(hashtagStart, end, [
+ state.schema.nodes.hashtag.create({ name }),
+ state.schema.text(" "),
+ ])
+ );
+ },
+ options: { undoable: false },
+ },
+ ],
+
+ parse: {
+ span_open(state, token, tokens, i) {
+ if (token.attrGet("class") === "hashtag-raw") {
+ state.openNode(state.schema.nodes.hashtag, {
+ name: tokens[i + 1].content.slice(1),
+ });
+ return true;
+ }
+ },
+ span_close(state) {
+ if (state.top().type.name === "hashtag") {
+ state.closeNode();
+ return true;
+ }
+ },
+ },
+
+ serializeNode: {
+ hashtag(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 bc95ecd9a54..bf0c5495105 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,4 +1,5 @@
import { registerRichEditorExtension } from "discourse/lib/composer/rich-editor-extensions";
+import hashtag from "./hashtag";
import underline from "./underline";
/**
@@ -7,6 +8,8 @@ import underline from "./underline";
*
* @type {RichEditorExtension[]}
*/
-const defaultExtensions = [underline];
+const defaultExtensions = [hashtag, underline];
defaultExtensions.forEach(registerRichEditorExtension);
+
+export default defaultExtensions;
diff --git a/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs b/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs
index 53a6284d008..52900d53bde 100644
--- a/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs
+++ b/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs
@@ -53,7 +53,7 @@ export async function testMarkdown(
.replace('<img class="ProseMirror-separator" alt="">', "")
.replace('<br class="ProseMirror-trailingBreak">', "")
// or artifacts
- .replace('class=""', "");
+ .replace(' class=""', "");
assert.strictEqual(html, expectedHtml, `HTML should match for "${markdown}"`);
assert.strictEqual(
diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/hashtag-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/hashtag-test.gjs
new file mode 100644
index 00000000000..9ce6daab21c
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/hashtag-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 - hashtag extension",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ const testCases = {
+ hashtag: [
+ [
+ "#hello",
+ '<p><a class="hashtag-cooked" data-name="hello" contenteditable="false" draggable="true">#hello</a></p>',
+ "#hello",
+ ],
+ [
+ "Hello #category",
+ '<p>Hello <a class="hashtag-cooked" data-name="category" contenteditable="false" draggable="true">#category</a></p>',
+ "Hello #category",
+ ],
+ ],
+ };
+
+ 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);
+ });
+ });
+ });
+ }
+);
diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/prosemirror-editor-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/prosemirror-editor-test.gjs
index 066b9feec93..91e695e3195 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/prosemirror-editor-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/prosemirror-editor-test.gjs
@@ -1,6 +1,9 @@
import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
-import { resetRichEditorExtensions } from "discourse/lib/composer/rich-editor-extensions";
+import {
+ clearRichEditorExtensions,
+ resetRichEditorExtensions,
+} from "discourse/lib/composer/rich-editor-extensions";
import { withPluginApi } from "discourse/lib/plugin-api";
import ProsemirrorEditor from "discourse/static/prosemirror/components/prosemirror-editor";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
@@ -9,6 +12,10 @@ import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
module("Integration | Component | prosemirror-editor", function (hooks) {
setupRenderingTest(hooks);
+ hooks.beforeEach(function () {
+ clearRichEditorExtensions();
+ });
+
hooks.afterEach(function () {
resetRichEditorExtensions();
});