Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.3 KiB
130 lines
3.3 KiB
// Simple typographic replacements
|
|
//
|
|
// (c) (C) → ©
|
|
// (tm) (TM) → ™
|
|
// (r) (R) → ®
|
|
// +- → ±
|
|
// (p) (P) -> §
|
|
// ... → … (also ?.... → ?.., !.... → !..)
|
|
// ???????? → ???, !!!!! → !!!, `,,` → `,`
|
|
// -- → –, --- → —
|
|
//
|
|
'use strict';
|
|
|
|
// TODO:
|
|
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
|
|
// - miltiplication 2 x 4 -> 2 × 4
|
|
|
|
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,/;
|
|
|
|
var DASHES_RE = /--/;
|
|
|
|
// Workaround for phantomjs - need regex without /g flag,
|
|
// or root check will fail every second time
|
|
var SCOPED_ABBR_TEST_RE = /\((c|tm|r|p)\)/i;
|
|
|
|
var SCOPED_ABBR_RE = /\((c|tm|r|p)\)/ig;
|
|
var SCOPED_ABBR = {
|
|
c: '©',
|
|
r: '®',
|
|
p: '§',
|
|
tm: '™'
|
|
};
|
|
|
|
function replaceFn(match, name) {
|
|
return SCOPED_ABBR[name.toLowerCase()];
|
|
}
|
|
|
|
function replace_scoped(inlineTokens) {
|
|
var i, token, inside_autolink = 0;
|
|
|
|
for (i = inlineTokens.length - 1; i >= 0; i--) {
|
|
token = inlineTokens[i];
|
|
|
|
if (token.type === 'text' && !inside_autolink) {
|
|
token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn);
|
|
}
|
|
|
|
if (token.type === 'link_open' && token.info === 'auto') {
|
|
inside_autolink--;
|
|
}
|
|
|
|
if (token.type === 'link_close' && token.info === 'auto') {
|
|
inside_autolink++;
|
|
}
|
|
}
|
|
}
|
|
|
|
function replace_helper(inlineTokens, re, replacer) {
|
|
var i, token, inside_autolink = 0;
|
|
|
|
for (i = inlineTokens.length - 1; i >= 0; i--) {
|
|
token = inlineTokens[i];
|
|
|
|
if (token.type === 'text' && !inside_autolink) {
|
|
if (re.test(token.content)) {
|
|
token.content = replacer(token.content);
|
|
}
|
|
}
|
|
|
|
if (token.type === 'link_open' && token.info === 'auto') {
|
|
inside_autolink--;
|
|
}
|
|
|
|
if (token.type === 'link_close' && token.info === 'auto') {
|
|
inside_autolink++;
|
|
}
|
|
}
|
|
}
|
|
|
|
function replace_rare(inlineTokens) {
|
|
replace_helper(inlineTokens, RARE_RE, function (content) {
|
|
return content
|
|
.replace(/\+-/g, '±')
|
|
// .., ..., ....... -> …
|
|
// but ?..... & !..... -> ?.. & !..
|
|
.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..')
|
|
.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',');
|
|
});
|
|
}
|
|
|
|
function replace_dashes(inlineTokens) {
|
|
replace_helper(inlineTokens, DASHES_RE, function (content) {
|
|
return content
|
|
// em-dash
|
|
.replace(/(^|[^-])---(?=[^-]|$)/mg, '$1\u2014')
|
|
// en-dash
|
|
.replace(/(^|\s)--(?=\s|$)/mg, '$1\u2013')
|
|
.replace(/(^|[^-\s])--(?=[^-\s]|$)/mg, '$1\u2013');
|
|
});
|
|
}
|
|
|
|
module.exports = function replace(state) {
|
|
var blkIdx;
|
|
var typographerOption = state.md.options.typographer;
|
|
|
|
if (!typographerOption) { return; }
|
|
|
|
var optionIsObject = typeof typographerOption === 'object';
|
|
var replaceRare = !optionIsObject || typographerOption.rare;
|
|
var replaceIP = !optionIsObject || typographerOption.intellectualProperty;
|
|
var replaceDashes = !optionIsObject || typographerOption.dashes;
|
|
|
|
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
|
|
|
|
if (state.tokens[blkIdx].type !== 'inline') { continue; }
|
|
|
|
if (replaceIP && SCOPED_ABBR_TEST_RE.test(state.tokens[blkIdx].content)) {
|
|
replace_scoped(state.tokens[blkIdx].children);
|
|
}
|
|
|
|
if (replaceDashes && DASHES_RE.test(state.tokens[blkIdx].content)) {
|
|
replace_dashes(state.tokens[blkIdx].children);
|
|
}
|
|
|
|
if (replaceRare && RARE_RE.test(state.tokens[blkIdx].content)) {
|
|
replace_rare(state.tokens[blkIdx].children);
|
|
}
|
|
|
|
}
|
|
};
|
|
|