A platform for community discussion. Free, open, simple.
FEATURE: switch to markdown when unsupported tokens are found on rich editor (#35044)
Renato Atiliob6772cf5c555
diff --git a/app/assets/javascripts/discourse/app/components/d-editor.gjs b/app/assets/javascripts/discourse/app/components/d-editor.gjsindex 84744184b28..25721d40dfe 100644--- a/app/assets/javascripts/discourse/app/components/d-editor.gjs+++ b/app/assets/javascripts/discourse/app/components/d-editor.gjs@@ -832,6 +832,7 @@ export default class DEditor extends Component {@topicId={{@topicId}}@id={{this.textAreaId}}@replaceToolbar={{this.replaceToolbar}}+ @toggleRichEditor={{this.toggleRichEditor}}/><PopupInputTip @validation={{this.validation}} /><PluginOutletdiff --git a/app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.js b/app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.jsindex 2a6f31a408a..4155df1b1a1 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@@ -17,14 +17,15 @@*//**- * @typedef {Object} MarkdownConversionHelpers+ * @typedef {Object} EditorInstanceUtils* @property {(markdown: string) => import("prosemirror-model").Node} convertFromMarkdown* @property {(doc: import("prosemirror-model").Node) => string} convertToMarkdown+ * @property {() => void} toggleRichEditor*//*** @typedef PluginParams- * @property {typeof import("discourse/static/prosemirror/lib/plugin-utils") & MarkdownConversionHelpers} utils+ * @property {typeof import("discourse/static/prosemirror/lib/plugin-utils") & EditorInstanceUtils} utils* @property {typeof import('prosemirror-model')} pmModel* @property {typeof import('prosemirror-view')} pmView* @property {typeof import('prosemirror-state')} pmStatediff --git a/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs b/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjsindex 54740fb6483..37c0c2fca67 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs+++ b/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs@@ -24,10 +24,11 @@ import * as ProsemirrorView from "prosemirror-view";import { EditorView } from "prosemirror-view";import { getExtensions } from "discourse/lib/composer/rich-editor-extensions";import { bind } from "discourse/lib/decorators";+import { i18n } from "discourse-i18n";import { buildCommands, buildCustomState } from "../core/commands";import { buildInputRules } from "../core/inputrules";import { buildKeymap } from "../core/keymap";-import Parser from "../core/parser";+import Parser, { UnsupportedTokenError } from "../core/parser";import { extractNodeViews, extractPlugins } from "../core/plugin";import { createSchema } from "../core/schema";import Serializer from "../core/serializer";@@ -53,6 +54,7 @@ const AUTOCOMPLETE_KEY_DOWN_SUPPRESS = ["Enter", "Tab"];* @property {boolean} [includeDefault] If default node and mark spec/parse/serialize/inputRules definitions from ProseMirror should be included* @property {import("discourse/lib/composer/rich-editor-extensions").RichEditorExtension[]} [extensions] A list of extensions to be used with the editor INSTEAD of the ones registered through the API* @property {(toolbar: import("discourse/lib/composer/toolbar").ToolbarBase) => void} [replaceToolbar] A function that replaces the default toolbar in a container with a custom/temporary one+ * @property {() => void} [toggleRichEditor] A callback to toggle the rich editor on and off if in such a context*//**@@ -92,6 +94,7 @@ export default class ProsemirrorEditor extends Component {...utils,convertFromMarkdown: this.convertFromMarkdown,convertToMarkdown: this.convertToMarkdown,+ toggleRichEditor: this.args.toggleRichEditor,},schema: this.schema,pmState: ProsemirrorState,@@ -112,6 +115,7 @@ export default class ProsemirrorEditor extends Component {site: this.site,siteSettings: this.siteSettings,appEvents: this.appEvents,+ dialog: this.dialog,replaceToolbar: this.args.replaceToolbar,addGlimmerNodeView: (nodeView) => this.glimmerNodeViews.push(nodeView),removeGlimmerNodeView: (nodeView) =>@@ -238,12 +242,7 @@ export default class ProsemirrorEditor extends Component {@bindconvertFromMarkdown(markdown) {- try {- return this.parser.convert(this.schema, markdown);- } catch (e) {- next(() => this.dialog.alert(e.message));- throw e;- }+ return this.parser.convert(this.schema, markdown);}@bind@@ -255,15 +254,27 @@ export default class ProsemirrorEditor extends Component {return;}- const doc = this.convertFromMarkdown(value);+ try {+ const doc = this.convertFromMarkdown(value);- const tr = this.view.state.tr;- tr.replaceWith(0, this.view.state.doc.content.size, doc.content).setMeta(- "addToHistory",- false- );+ const tr = this.view.state.tr;+ tr.replaceWith(0, this.view.state.doc.content.size, doc.content).setMeta(+ "addToHistory",+ false+ );- this.view.updateState(this.view.state.apply(tr));+ this.view.updateState(this.view.state.apply(tr));+ } catch (e) {+ if (e instanceof UnsupportedTokenError) {+ this.dialog.alert({+ message: i18n("composer.unsupported_token"),+ didConfirm: this.args.toggleRichEditor,+ didCancel: this.args.toggleRichEditor,+ });+ } else {+ throw e;+ }+ }}@binddiff --git a/app/assets/javascripts/discourse/app/static/prosemirror/core/parser.js b/app/assets/javascripts/discourse/app/static/prosemirror/core/parser.jsindex f3e453d3a99..7582a8c4f0d 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/core/parser.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/core/parser.js@@ -40,7 +40,14 @@ export default class Parser {// Adding function parse handlers directlyObject.assign(parser.tokenHandlers, this.postParseTokens);- return parser.parse(text);+ try {+ return parser.parse(text);+ } catch (e) {+ if (e.message?.startsWith("Token type")) {+ throw new UnsupportedTokenError();+ }+ throw e;+ }}#extractParsers(extensions) {@@ -80,11 +87,9 @@ export default class Parser {}}- throw new Error(- `No parser processed ${tokenName} token for tag: ${- token.tag- }, attrs: ${JSON.stringify(token.attrs)}`- );+ throw new UnsupportedTokenError();};}}++export class UnsupportedTokenError extends Error {}diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/markdown-paste.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/markdown-paste.jsindex 9469ff58823..811843d2492 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/markdown-paste.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/markdown-paste.js@@ -1,16 +1,37 @@+import { UnsupportedTokenError } from "discourse/static/prosemirror/core/parser";+import { i18n } from "discourse-i18n";+/** @type {RichEditorExtension} */const extension = {plugins({pmState: { Plugin },pmModel: { Fragment, Slice },- utils: { convertFromMarkdown },+ utils: { convertFromMarkdown, toggleRichEditor },+ schema,+ getContext,}) {return new Plugin({props: {clipboardTextParser(text) {- const { content } = convertFromMarkdown(text);+ try {+ const { content } = convertFromMarkdown(text);++ return Slice.maxOpen(Fragment.from(content));+ } catch (e) {+ if (e instanceof UnsupportedTokenError) {+ getContext().dialog.alert({+ message: i18n("composer.unsupported_token"),+ didConfirm: toggleRichEditor,+ didCancel: toggleRichEditor,+ });++ return new Fragment([+ schema.nodes.html_block.create({}, schema.text(text)),+ ]);+ }- return Slice.maxOpen(Fragment.from(content));+ throw e;+ }},},});diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex c211a0593ee..a56e68d61ea 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -3120,6 +3120,8 @@ en:visit: "Open link in external tab"link_copied: "Link copied!"+ unsupported_token: "The rich text editor doesn’t support all features used in this post; switching you to the Markdown editor."+notifications:tooltip:regular: