FEATURE: Replace arrows when the markdown typographer is enabled. (#11638)

By inserting this rule before markdown-it's replacement rule, we can replace "-->" with "&rarr;", and "<--" with "&larr;".
This commit is contained in:
Roman Rizzi 2021-01-06 12:05:38 -03:00 committed by GitHub
parent 53f9a0883e
commit 118ea89372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -1554,4 +1554,35 @@ var bar = 'bar';
</div></p>` </div></p>`
); );
}); });
test("typographer arrows", function (assert) {
const enabledTypographer = {
siteSettings: { enable_markdown_typographer: true },
};
// Replace arrows
assert.cookedOptions(
"--> <--",
enabledTypographer,
"<p> \u2192 \u2190 </p>"
);
assert.cookedOptions("a --> b", enabledTypographer, "<p>a \u2192 b</p>");
assert.cookedOptions("-->", enabledTypographer, "<p> \u2192 </p>");
assert.cookedOptions("<--", enabledTypographer, "<p> \u2190 </p>");
// Don't replace arrows
assert.cookedOptions("<!-- an html comment -->", enabledTypographer, "");
assert.cookedOptions(
"(<--not an arrow)",
enabledTypographer,
"<p>(&lt;not an arrow)</p>"
);
assert.cookedOptions("<-->", enabledTypographer, "<p>&lt;&gt;</p>");
assert.cookedOptions("asd-->", enabledTypographer, "<p>asd&gt;</p>");
assert.cookedOptions(" asd--> ", enabledTypographer, "<p>asd&gt;</p>");
assert.cookedOptions(" asd-->", enabledTypographer, "<p>asd&gt;</p>");
assert.cookedOptions("-->asd", enabledTypographer, "<p>&gt;asd</p>");
assert.cookedOptions(" -->asd ", enabledTypographer, "<p>&gt;asd</p>");
assert.cookedOptions(" -->asd", enabledTypographer, "<p>&gt;asd</p>");
});
}); });

View File

@ -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);
}
});
}