From 118ea893725f61ea76c62f207a82310194b2edcd Mon Sep 17 00:00:00 2001
From: Roman Rizzi
\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); + } + }); +}