Skip to content

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

DEV: event node view link validation from getLinkify (#37607)

Renato Atilio936cf2ebc3b6
diff --git a/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs b/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs
index 3e44340e5df..c938cb0d77f 100644
--- a/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs
+++ b/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs
@@ -369,6 +369,7 @@ export default class ProsemirrorEditor extends Component {
@view={{nodeView.view}}
@getPos={{nodeView.getPos}}
@dom={{nodeView.dom}}
+ @pluginParams={{nodeView.pluginParams}}
@onSetup={{nodeView.setComponentInstance}}
/>
{{~/in-element~}}
diff --git a/frontend/discourse/app/static/prosemirror/core/plugin.js b/frontend/discourse/app/static/prosemirror/core/plugin.js
index 98e80ad312f..231cf6b7768 100644
--- a/frontend/discourse/app/static/prosemirror/core/plugin.js
+++ b/frontend/discourse/app/static/prosemirror/core/plugin.js
@@ -2,7 +2,7 @@ import { Plugin } from "prosemirror-state";
import GlimmerNodeView from "../lib/glimmer-node-view";
/*
- There are 4 ways to define a node view:
+ There are 3 ways to define a node view:
Setting a node view class directly (e.g. code-block)
`nodeViews: { nodeName: NodeViewClass }`
@@ -10,11 +10,13 @@ import GlimmerNodeView from "../lib/glimmer-node-view";
Setting a node view class as returned by a function, when plugin params are needed (e.g. footnote)
`nodeViews: { nodeName: (pluginParams) => NodeViewClass }`
- Setting a node view instance as returned by a function, when plugin params are needed (e.g. image)
- `nodeViews: { nodeName: (pluginParams) => (...args) => nodeViewInstance }`
+ Setting a Glimmer component with auto-wrapping (recommended for Glimmer components)
+ `nodeViews: { nodeName: { component: GlimmerComponent } }`
- Setting a Glimmer component with auto-wrapping (new)
- `nodeViews: { nodeName: { component: GlimmerComponent, name: "nodeName" } }`
+ The Glimmer component descriptor supports additional options:
+ - `name: "customName"` - CSS class suffix (defaults to the key)
+ - `hasContent: true` - for nodes with editable content inside
+ - `shouldRender: ({ node, view, getPos, pluginParams }) => boolean` - for conditional rendering
*/
export function extractNodeViews(extensions, pluginParams) {
/** @type {Record<string, import('prosemirror-view').NodeViewConstructor>} */
@@ -25,13 +27,21 @@ export function extractNodeViews(extensions, pluginParams) {
// Check if nodeView is a Glimmer component descriptor
if (nodeView && typeof nodeView === "object" && nodeView.component) {
allNodeViews[name] = (node, view, getPos) => {
+ if (
+ nodeView.shouldRender &&
+ !nodeView.shouldRender({ node, view, getPos, pluginParams })
+ ) {
+ return null;
+ }
+
return new GlimmerNodeView({
node,
view,
getPos,
- getContext: pluginParams.getContext,
+ pluginParams,
component: nodeView.component,
name: nodeView.name || name,
+ hasContent: nodeView.hasContent,
});
};
continue;
diff --git a/frontend/discourse/app/static/prosemirror/extensions/grid.js b/frontend/discourse/app/static/prosemirror/extensions/grid.js
index 00368ac1441..d1d0e46bf73 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/grid.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/grid.js
@@ -1,20 +1,13 @@
import GridNodeView from "../components/grid-node-view";
-import GlimmerNodeView from "../lib/glimmer-node-view";
-
-const createGridNodeView =
- ({ getContext }) =>
- (node, view, getPos) =>
- new GlimmerNodeView({
- node,
- view,
- getPos,
- getContext,
+
+const extension = {
+ nodeViews: {
+ grid: {
component: GridNodeView,
- name: "grid",
hasContent: true,
- });
+ },
+ },
-const extension = {
nodeSpec: {
grid: {
content: "block+",
@@ -54,10 +47,6 @@ const extension = {
},
},
- nodeViews: {
- grid: createGridNodeView,
- },
-
parse: {
bbcode_open(state, token) {
if (token.attrGet("class") === "d-image-grid") {
diff --git a/frontend/discourse/app/static/prosemirror/extensions/image.js b/frontend/discourse/app/static/prosemirror/extensions/image.js
index 0a3aed28e8a..01e3d448536 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/image.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/image.js
@@ -8,7 +8,6 @@ import { authorizesOneOrMoreImageExtensions } from "discourse/lib/uploads";
import { isNumeric } from "discourse/lib/utilities";
import { i18n } from "discourse-i18n";
import ImageNodeView from "../components/image-node-view";
-import GlimmerNodeView from "../lib/glimmer-node-view";
import { getChangedRanges } from "../lib/plugin-utils";
const PLACEHOLDER_IMG = "/images/transparent.png";
@@ -18,30 +17,17 @@ const ALT_TEXT_REGEX =
const UPLOAD_TIMEOUT = 30_000;
-const createImageNodeView =
- ({ getContext }) =>
- (node, view, getPos) => {
- if (
- node.attrs.placeholder ||
- node.attrs.extras === "audio" ||
- node.attrs.extras === "video"
- ) {
- return null;
- }
-
- return new GlimmerNodeView({
- node,
- view,
- getPos,
- getContext,
- component: ImageNodeView,
- name: "image",
- });
- };
-
/** @type {RichEditorExtension} */
const extension = {
- nodeViews: { image: createImageNodeView },
+ nodeViews: {
+ image: {
+ component: ImageNodeView,
+ shouldRender: ({ node }) =>
+ !node.attrs.placeholder &&
+ node.attrs.extras !== "audio" &&
+ node.attrs.extras !== "video",
+ },
+ },
nodeSpec: {
image: {
diff --git a/frontend/discourse/app/static/prosemirror/extensions/wrap.js b/frontend/discourse/app/static/prosemirror/extensions/wrap.js
index 19a054ce3de..629872ddc2d 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/wrap.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/wrap.js
@@ -1,7 +1,6 @@
import { camelCaseToDash } from "discourse/lib/case-converter";
import { i18n } from "discourse-i18n";
import WrapNodeView from "../components/wrap-node-view";
-import GlimmerNodeView from "../lib/glimmer-node-view";
import { parseAttributesString, serializeAttributes } from "../lib/wrap-utils";
const toDataAttrs = (data) => {
@@ -17,24 +16,17 @@ const toDataAttrs = (data) => {
return attrs;
};
-const createWrapNodeView =
- ({ getContext }) =>
- (node, view, getPos) =>
- new GlimmerNodeView({
- node,
- view,
- getPos,
- getContext,
- component: WrapNodeView,
- name: "wrap",
- hasContent: true,
- });
+const wrapNodeViewDescriptor = {
+ component: WrapNodeView,
+ name: "wrap",
+ hasContent: true,
+};
/** @type {import("discourse/lib/composer/rich-editor-extensions").RichEditorExtension} */
const extension = {
nodeViews: {
- wrap_block: createWrapNodeView,
- wrap_inline: createWrapNodeView,
+ wrap_block: wrapNodeViewDescriptor,
+ wrap_inline: wrapNodeViewDescriptor,
},
commands: ({ schema }) => ({
diff --git a/frontend/discourse/app/static/prosemirror/lib/glimmer-node-view.js b/frontend/discourse/app/static/prosemirror/lib/glimmer-node-view.js
index 83d59ee78dc..83aee7183a6 100644
--- a/frontend/discourse/app/static/prosemirror/lib/glimmer-node-view.js
+++ b/frontend/discourse/app/static/prosemirror/lib/glimmer-node-view.js
@@ -7,7 +7,7 @@ import { next } from "@ember/runloop";
* @property {import("prosemirror-model").Node} node
* @property {import("prosemirror-view").EditorView} view
* @property {() => number} getPos
- * @property {() => import("discourse/lib/composer/rich-editor-extensions").PluginContext} getContext
+ * @property {import("discourse/lib/composer/rich-editor-extensions").PluginParams} pluginParams
* @property {any} component
* @property {string} name
*/
@@ -23,6 +23,7 @@ export default class GlimmerNodeView {
node,
view,
getPos,
+ pluginParams,
getContext,
component,
name,
@@ -31,10 +32,10 @@ export default class GlimmerNodeView {
this.node = node;
this.view = view;
this.getPos = getPos;
- this.getContext = getContext;
+ this.pluginParams = pluginParams ?? { getContext };
this.component = component;
- getContext().addGlimmerNodeView(this);
+ this.pluginParams.getContext().addGlimmerNodeView(this);
this.dom = document.createElement(node.isInline ? "span" : "div");
this.dom.classList.add(`composer-${name}-node`);
@@ -83,6 +84,6 @@ export default class GlimmerNodeView {
this.#componentInstance?.destroy?.();
this.#componentInstance = null;
- this.getContext().removeGlimmerNodeView(this);
+ this.pluginParams.getContext().removeGlimmerNodeView(this);
}
}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/more-menu.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/more-menu.gjs
index f2fd3f50221..f918f050381 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/more-menu.gjs
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/more-menu.gjs
@@ -13,7 +13,11 @@ import { exportEntity } from "discourse/lib/export-csv";
import { cook } from "discourse/lib/text";
import { applyValueTransformer } from "discourse/lib/transformer";
import { i18n } from "discourse-i18n";
-import { buildParams, replaceRaw } from "../../lib/raw-event-helper";
+import {
+ buildParams,
+ removeEvent,
+ replaceRaw,
+} from "../../lib/raw-event-helper";
import PostEventBuilder from "../modal/post-event-builder";
import PostEventBulkInvite from "../modal/post-event-bulk-invite";
import PostEventInviteUserOrGroup from "../modal/post-event-invite-user-or-group";
@@ -193,12 +197,7 @@ export default class DiscoursePostEventMoreMenu extends Component {
onDelete: async (event) => {
const post = await this.store.find("post", event.id);
const raw = post.raw;
-
- const eventRegex = new RegExp(
- `\\[event\\s(.*?)\\]\\n\\[\\/event\\]`,
- "m"
- );
- const newRaw = raw.replace(eventRegex, "");
+ const newRaw = removeEvent(raw);
const props = {
raw: newRaw,
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs
index 333df9a0789..8d74a95261a 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs
@@ -228,13 +228,9 @@ export default class EventNodeView extends Component {
if (!this.hasLocation) {
return false;
}
- const location = this.eventData.location.trim();
- return (
- location.includes("://") ||
- location.includes("mailto:") ||
- location.startsWith("www.") ||
- /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/.test(location)
- );
+ return this.args.pluginParams.utils
+ .getLinkify()
+ .test(this.eventData.location);
}
get locationIcon() {
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js b/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js
index e8ed47d5d3f..c543810e653 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js
@@ -134,3 +134,7 @@ export function camelCase(input) {
return group1.toUpperCase();
});
}
+
+export function removeEvent(raw) {
+ return raw.replace(/\[event (.*?)\](.*?)\[\/event\]/s, "");
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js b/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js
index 4570b6a1273..521a465d6e5 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js
@@ -27,7 +27,6 @@ const extension = {
nodeViews: {
event: {
component: EventNodeView,
- name: "event",
},
},
diff --git a/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js b/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js
index adfbcacfb07..84100db2673 100644
--- a/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js
+++ b/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js
@@ -1,7 +1,32 @@
import { module, test } from "qunit";
-import { replaceRaw } from "discourse/plugins/discourse-calendar/discourse/lib/raw-event-helper";
+import {
+ removeEvent,
+ replaceRaw,
+} from "discourse/plugins/discourse-calendar/discourse/lib/raw-event-helper";
module("Unit | Lib | raw-event-helper", function () {
+ test("removeEvent", function (assert) {
+ assert.strictEqual(
+ removeEvent('[event start="2024-01-01"]\nDescription\n[/event]'),
+ "",
+ "removes event with content"
+ );
+
+ assert.strictEqual(
+ removeEvent(
+ 'Before\n[event start="2024-01-01"]\nContent\n[/event]\nAfter'
+ ),
+ "Before\n\nAfter",
+ "preserves surrounding text"
+ );
+
+ assert.strictEqual(
+ removeEvent('[event start="2024-01-01"]\n[/event]'),
+ "",
+ "removes event without content"
+ );
+ });
+
test("replaceRaw", function (assert) {
const raw = 'Some text \n[event param1="va]lue1"]\n[/event]\n more text';
const params = {