From 118ea893725f61ea76c62f207a82310194b2edcd Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Wed, 6 Jan 2021 12:05:38 -0300 Subject: [PATCH] FEATURE: Replace arrows when the markdown typographer is enabled. (#11638) By inserting this rule before markdown-it's replacement rule, we can replace "-->" with "→", and "<--" with "←". --- .../tests/unit/lib/pretty-text-test.js | 31 ++++++++++++++++++ .../discourse-markdown/typographer-arrows.js | 32 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 app/assets/javascripts/pretty-text/engines/discourse-markdown/typographer-arrows.js diff --git a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js index 8f232ae8551..3c7718eef72 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js @@ -1554,4 +1554,35 @@ var bar = 'bar';

` ); }); + + test("typographer arrows", function (assert) { + const enabledTypographer = { + siteSettings: { enable_markdown_typographer: true }, + }; + + // Replace arrows + assert.cookedOptions( + "--> <--", + enabledTypographer, + "

\u2192 \u2190

" + ); + assert.cookedOptions("a --> b", enabledTypographer, "

a \u2192 b

"); + assert.cookedOptions("-->", enabledTypographer, "

\u2192

"); + assert.cookedOptions("<--", enabledTypographer, "

\u2190

"); + + // Don't replace arrows + assert.cookedOptions("", enabledTypographer, ""); + assert.cookedOptions( + "(<--not an arrow)", + enabledTypographer, + "

(<–not an arrow)

" + ); + assert.cookedOptions("<-->", enabledTypographer, "

<–>

"); + assert.cookedOptions("asd-->", enabledTypographer, "

asd–>

"); + assert.cookedOptions(" asd--> ", enabledTypographer, "

asd–>

"); + assert.cookedOptions(" asd-->", enabledTypographer, "

asd–>

"); + assert.cookedOptions("-->asd", enabledTypographer, "

–>asd

"); + assert.cookedOptions(" -->asd ", enabledTypographer, "

–>asd

"); + assert.cookedOptions(" -->asd", enabledTypographer, "

–>asd

"); + }); }); diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/typographer-arrows.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/typographer-arrows.js new file mode 100644 index 00000000000..cccea6d47ca --- /dev/null +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/typographer-arrows.js @@ -0,0 +1,32 @@ +function replaceArrows(state) { + for (let i = 0; i < state.tokens.length; i++) { + let token = state.tokens[i]; + + if (token.type !== "inline") { + continue; + } + + const arrowsRegexp = /-->|<--/; + if (arrowsRegexp.test(token.content)) { + for (let ci = 0; ci < token.children.length; ci++) { + let child = token.children[ci]; + + if (child.type === "text") { + if (arrowsRegexp.test(child.content)) { + child.content = child.content + .replace(/(^|\s)-->(\s|$)/gm, "\u0020\u2192\u0020") + .replace(/(^|\s)<--(\s|$)/gm, "\u0020\u2190\u0020"); + } + } + } + } + } +} + +export function setup(helper) { + helper.registerPlugin((md) => { + if (md.options.typographer) { + md.core.ruler.before("replacements", "typographer-arrow", replaceArrows); + } + }); +}