A platform for community discussion. Free, open, simple.
UX: Disable typographer replacements in the rich editor (#40550)
Renato Atilio5bd66b45b44d
diff --git a/frontend/discourse/app/static/prosemirror/core/inputrules.js b/frontend/discourse/app/static/prosemirror/core/inputrules.jsindex 23f571332fa..ef336e6470e 100644--- a/frontend/discourse/app/static/prosemirror/core/inputrules.js+++ b/frontend/discourse/app/static/prosemirror/core/inputrules.js@@ -35,8 +35,11 @@ export function buildInputRules(extensions, params, includeDefault = true) {markInputRule(/(^|\s)__([^_]+)__$/, schema.marks.strong, getAttrs),markInputRule(/(^|[^*])\*([^*]+)\*$/, schema.marks.em, getAttrs),markInputRule(/(^|\s)_([^_]+)_$/, schema.marks.em, getAttrs),+ // `---`, `___ `, `***`. The en/em-dash variants are kept for content+ // that already contains those characters (e.g. pasted), since the editor+ // no longer converts `--`/`---` to dashes as you type.new InputRule(- /^(\u2013-|\u2014-|___\s|\*\*\*\s)$/,+ /^(---|\u2013-|\u2014-|___\s|\*\*\*\s)$/,horizontalRuleHandler,{ inCodeMark: false }),diff --git a/frontend/discourse/app/static/prosemirror/core/serializer.js b/frontend/discourse/app/static/prosemirror/core/serializer.jsindex 7cd960646f8..10f2cf762dd 100644--- a/frontend/discourse/app/static/prosemirror/core/serializer.js+++ b/frontend/discourse/app/static/prosemirror/core/serializer.js@@ -2,7 +2,6 @@ import {defaultMarkdownSerializer,MarkdownSerializerState,} from "prosemirror-markdown";-import { buildTypographyReverseMap } from "discourse/static/prosemirror/extensions/typographer-replacements";export default class Serializer {#afterSerializers;@@ -12,13 +11,8 @@ 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);+ state.text(node.text, !state.inAutolink);};this.marks = includeDefault ? { ...defaultMarkdownSerializer.marks } : {};@@ -71,18 +65,3 @@ 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/register-default.js b/frontend/discourse/app/static/prosemirror/extensions/register-default.jsindex 1a1875b33bd..2d86af0affc 100644--- a/frontend/discourse/app/static/prosemirror/extensions/register-default.js+++ b/frontend/discourse/app/static/prosemirror/extensions/register-default.js@@ -28,7 +28,6 @@ import strikethrough from "./strikethrough";import table from "./table";import trailingInlineSpace from "./trailing-inline-space";import trailingParagraph from "./trailing-paragraph";-import typographerReplacements from "./typographer-replacements";import underline from "./underline";import uploadPlaceholder from "./upload-placeholder";import wordPaste from "./word-paste";@@ -58,7 +57,6 @@ const defaultExtensions = [htmlInline,htmlBlock,trailingParagraph,- typographerReplacements,table,markdownPaste,wordPaste,diff --git a/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.js b/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.jsdeleted file mode 100644index 469183b1c5d..00000000000--- a/frontend/discourse/app/static/prosemirror/extensions/typographer-replacements.js+++ /dev/null@@ -1,102 +0,0 @@-// @ts-check--import { InputRule } from "prosemirror-inputrules";-import {- RARE_RE,- replaceRareStr,- replaceScopedStr,- SCOPED_ABBR_RE,-} from "discourse-markdown-it/features/custom-typographer-replacements";--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: ({ 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- 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/app/static/prosemirror/lib/markdown-it.js b/frontend/discourse/app/static/prosemirror/lib/markdown-it.jsindex da7561c9000..d6746ec0b96 100644--- a/frontend/discourse/app/static/prosemirror/lib/markdown-it.js+++ b/frontend/discourse/app/static/prosemirror/lib/markdown-it.js@@ -1,9 +1,21 @@+import { helperContext } from "discourse/lib/helpers";import { buildEngine } from "discourse/static/markdown-it";let engine;function getEngine() {- engine ??= buildEngine(null, ["onebox", "watched-words", "censored"]);+ if (!engine) {+ // Typographer replacements (smart quotes, dashes, ellipsis, etc.) are a+ // rendering concern. We disable them in the rich editor so the document+ // mirrors the raw markdown source and round-trips without baking+ // typographic characters into the stored content.+ const { siteSettings } = helperContext();++ engine = buildEngine(+ { siteSettings: { ...siteSettings, enable_markdown_typographer: false } },+ ["onebox", "watched-words", "censored"]+ );+ }return engine;}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.jsindex 4330e66623f..ee069c7836a 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,13 +144,20 @@ 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)"],+ // Typographer replacements are always disabled in the rich editor,+ // regardless of the `enable_markdown_typographer` site setting (which+ // defaults to true, as it is here). The document mirrors the raw markdown+ // source (no smart quotes, dashes, ellipsis, etc.) and round-trips back+ // to the same source on save.+ "typographer disabled": [+ ["hello -- world", "<p>hello -- world</p>", "hello -- world"],+ ["hello --- world", "<p>hello --- world</p>", "hello --- world"],+ ["hello... world", "<p>hello... world</p>", "hello... world"],+ ["+-5", "<p>+-5</p>", "+-5"],+ ["(tm)", "<p>(tm)</p>", "(tm)"],+ ["(pa)", "<p>(pa)</p>", "(pa)"],+ ['"hello" world', '<p>"hello" world</p>', '"hello" world'],+ ["it's here", "<p>it's here</p>", "it's here"],["`hello -- world`","<p><code>hello -- world</code></p>",diff --git a/spec/system/composer/prosemirror_input_rules_spec.rb b/spec/system/composer/prosemirror_input_rules_spec.rbindex 18985b706da..6a916c4cb86 100644--- a/spec/system/composer/prosemirror_input_rules_spec.rb+++ b/spec/system/composer/prosemirror_input_rules_spec.rb@@ -100,16 +100,14 @@ describe "Composer - ProseMirror - Input rules" doexpect(rich).to have_css("code", text: "This is code")end- it "supports typographer replacements" do+ it "does not apply typographer replacements" doopen_composer- composer.type_content(- "foo +- bar... test???? wow!!!! x,, y-- --- a--> b<-- c-> d<- e<-> f<--> (tm) (pa)",- )+ text = "foo +- bar... test???? wow!!!! x,, y-- --- a--> b<-- c-> d<- e<-> f<--> (tm) (pa)"+ composer.type_content(text)- expect(rich).to have_css(- "p",- text: "foo ± bar… test??? wow!!! x, y– — a–> b←- c→ d← e←> f←→ ™ ¶",- )+ # Typographer replacements are disabled in the rich editor, so the text is+ # kept verbatim instead of being converted to ±, …, smart quotes, etc.+ expect(rich).to have_css("p", text:)endit "supports ---, ***, ___, en-dash+hyphen, em-dash+hyphen to create a horizontal rule" do