Skip to content

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

FEATURE: reverse rich editor typography when serializing and respect site settings (#38908)

Renato Atilio50455fc7a7f4
diff --git a/frontend/discourse/app/lib/composer/rich-editor-extensions.js b/frontend/discourse/app/lib/composer/rich-editor-extensions.js
index 199c9d3041d..9c1006dc55d 100644
--- a/frontend/discourse/app/lib/composer/rich-editor-extensions.js
+++ b/frontend/discourse/app/lib/composer/rich-editor-extensions.js
@@ -13,6 +13,7 @@ import { waitForPromise } from "@ember/test-waiters";
* @property {import("discourse/services/modal").default} modal
* @property {import("discourse/float-kit/services/toasts").default} toasts
* @property {import("discourse/models/site").default} site
+ * @property {Record<string, unknown>} siteSettings
* @property {(toolbar: import("discourse/lib/composer/toolbar").ToolbarBase) => void} replaceToolbar
* @property {(nodeView: import("discourse/static/prosemirror/lib/glimmer-node-view").default) => void} addGlimmerNodeView
* @property {(nodeView: import("discourse/static/prosemirror/lib/glimmer-node-view").default) => void} removeGlimmerNodeView
diff --git a/frontend/discourse/app/static/prosemirror/core/inputrules.js b/frontend/discourse/app/static/prosemirror/core/inputrules.js
index 3efb2133e24..23f571332fa 100644
--- a/frontend/discourse/app/static/prosemirror/core/inputrules.js
+++ b/frontend/discourse/app/static/prosemirror/core/inputrules.js
@@ -1,7 +1,6 @@
import {
InputRule,
inputRules,
- smartQuotes,
textblockTypeInputRule,
wrappingInputRule,
} from "prosemirror-inputrules";
@@ -21,8 +20,6 @@ export function buildInputRules(extensions, params, includeDefault = true) {
});
const defaultRules = [
- // TODO(renato) smartQuotes should respect `markdown_typographer_quotation_marks`
- ...smartQuotes,
...[
wrappingInputRule(/^\s*>\s$/, schema.nodes.blockquote),
orderedListRule(schema.nodes.ordered_list),
diff --git a/frontend/discourse/app/static/prosemirror/core/serializer.js b/frontend/discourse/app/static/prosemirror/core/serializer.js
index 8d747e3a433..7cd960646f8 100644
--- a/frontend/discourse/app/static/prosemirror/core/serializer.js
+++ b/frontend/discourse/app/static/prosemirror/core/serializer.js
@@ -2,6 +2,7 @@ import {
defaultMarkdownSerializer,
MarkdownSerializerState,
} from "prosemirror-markdown";
+import { buildTypographyReverseMap } from "discourse/static/prosemirror/extensions/typographer-replacements";
export default class Serializer {
#afterSerializers;
@@ -11,6 +12,15 @@ export default class Serializer {
this.nodes.hard_break = (state) =>
state.write(state.inTable ? "<br>" : "\n");
+ const siteSettings = pluginParams.getContext?.().siteSettings;
+ const reverseTypography = buildReverseTypography(
+ buildTypographyReverseMap(siteSettings)
+ );
+
+ this.nodes.text = (state, node) => {
+ state.text(reverseTypography(node.text), !state.inAutolink);
+ };
+
this.marks = includeDefault ? { ...defaultMarkdownSerializer.marks } : {};
this.#extractNodeSerializers(extensions, pluginParams);
@@ -61,3 +71,18 @@ export default class Serializer {
}
}
}
+
+function buildReverseTypography(map) {
+ if (!Object.keys(map).length) {
+ return (text) => text;
+ }
+
+ const re = new RegExp(
+ Object.keys(map)
+ .map((k) => k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
+ .join("|"),
+ "g"
+ );
+
+ return (text) => text.replace(re, (char) => map[char]);
+}
diff --git a/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.js b/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.js
index 034ee509e2f..469183b1c5d 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.js
@@ -1,5 +1,6 @@
// @ts-check
+import { InputRule } from "prosemirror-inputrules";
import {
RARE_RE,
replaceRareStr,
@@ -7,35 +8,95 @@ import {
SCOPED_ABBR_RE,
} from "discourse-markdown-it/features/custom-typographer-replacements";
-// TODO(renato): should respect `enable_markdown_typographer`
+const BASE_TEXT_REPLACEMENTS = {
+ "\u2014": "---", // em-dash
+ "\u2013": "--", // en-dash
+ "\u2026": "...", // ellipsis
+ "\u00b1": "+-", // plus-minus
+ "\u2192": "->", // right arrow
+ "\u2190": "<-", // left arrow
+ "\u2194": "<->", // left-right arrow
+ "\u2122": "(tm)", // trademark
+ "\u00b6": "(pa)", // pilcrow
+};
+
+// Smart quote patterns from prosemirror-inputrules
+const SMART_QUOTE_PATTERNS = [
+ /(?:^|[\s{[(<'"\u2018\u201C])(")$/, // open double
+ /"$/, // close double
+ /(?:^|[\s{[(<'"\u2018\u201C])(')$/, // open single
+ /'$/, // close single
+];
+
+export function buildTypographyReverseMap(siteSettings) {
+ if (!siteSettings?.enable_markdown_typographer) {
+ return {};
+ }
+
+ const map = { ...BASE_TEXT_REPLACEMENTS };
+ const quotes =
+ /** @type {string} */
+ (siteSettings.markdown_typographer_quotation_marks)?.split("|");
+
+ if (quotes?.length === 4) {
+ map[quotes[0]] = '"';
+ map[quotes[1]] = '"';
+ map[quotes[2]] = "'";
+ map[quotes[3]] = "'";
+ }
+
+ return map;
+}
/** @type {import("discourse/lib/composer/rich-editor-extensions").RichEditorExtension} */
const extension = {
- inputRules: [
- {
- match: new RegExp(`(${RARE_RE.source})$`),
- handler: (state, match, start, end) => {
- return state.tr.replaceWith(
- start,
- end,
- state.schema.text(replaceRareStr(match[0]).trim())
- );
- },
- },
- {
+ inputRules: ({ getContext }) => {
+ const siteSettings = getContext()?.siteSettings;
+ if (!siteSettings?.enable_markdown_typographer) {
+ return [];
+ }
+
+ const rules = [
+ new InputRule(
+ new RegExp(`(${RARE_RE.source})$`),
+ (state, match, start, end) => {
+ return state.tr.replaceWith(
+ start,
+ end,
+ state.schema.text(replaceRareStr(match[0]).trim())
+ );
+ }
+ ),
// existing for markdown-it, plus: en-dash + hyphen -> em-dash
- match: new RegExp(`(${SCOPED_ABBR_RE.source}|\u2013-)$`, "i"),
- handler: (state, match, start, end) => {
- return state.tr.replaceWith(
- start,
- end,
- state.schema.text(
- replaceScopedStr(match[0]).replace(/\u2013-$/, "\u2014")
- )
- );
- },
- },
- ],
+ new InputRule(
+ new RegExp(`(${SCOPED_ABBR_RE.source}|\u2013-)$`, "i"),
+ (state, match, start, end) => {
+ return state.tr.replaceWith(
+ start,
+ end,
+ state.schema.text(
+ replaceScopedStr(match[0]).replace(/\u2013-$/, "\u2014")
+ )
+ );
+ }
+ ),
+ ];
+
+ const quotes =
+ /** @type {string} */
+ (siteSettings.markdown_typographer_quotation_marks)?.split("|");
+
+ if (quotes?.length !== 4) {
+ return rules;
+ }
+
+ return [
+ ...rules,
+ ...SMART_QUOTE_PATTERNS.map(
+ (pattern, i) => new InputRule(pattern, quotes[i], { inCodeMark: false })
+ ),
+ ];
+ },
};
export default extension;
diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/prosemirror-markdown-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/prosemirror-markdown-test.js
index d828493e176..4330e66623f 100644
--- a/frontend/discourse/tests/integration/components/prosemirror-editor/prosemirror-markdown-test.js
+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/prosemirror-markdown-test.js
@@ -144,6 +144,19 @@ module(
"***[`Hello`](https://example.com)***",
],
],
+ "typography reverse": [
+ ["hello -- world", "<p>hello \u2013 world</p>", "hello -- world"],
+ ["hello --- world", "<p>hello \u2014 world</p>", "hello --- world"],
+ ["hello... world", "<p>hello\u2026 world</p>", "hello... world"],
+ ["+-5", "<p>\u00b15</p>", "+-5"],
+ ["(tm)", "<p>\u2122</p>", "(tm)"],
+ ["(pa)", "<p>\u00b6</p>", "(pa)"],
+ [
+ "`hello -- world`",
+ "<p><code>hello -- world</code></p>",
+ "`hello -- world`",
+ ],
+ ],
};
Object.entries(testCases).forEach(([name, tests]) => {