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.
105 lines
2.6 KiB
105 lines
2.6 KiB
6 years ago
|
// Simple typographic replacements
|
||
10 years ago
|
//
|
||
10 years ago
|
// (c) (C) → ©
|
||
|
// (tm) (TM) → ™
|
||
|
// (r) (R) → ®
|
||
|
// +- → ±
|
||
|
// ... → … (also ?.... → ?.., !.... → !..)
|
||
|
// ???????? → ???, !!!!! → !!!, `,,` → `,`
|
||
|
// -- → –, --- → —
|
||
|
//
|
||
10 years ago
|
|
||
|
// TODO:
|
||
|
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
|
||
3 years ago
|
// - multiplications 2 x 4 -> 2 × 4
|
||
10 years ago
|
|
||
|
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/;
|
||
|
|
||
10 years ago
|
// Workaround for phantomjs - need regex without /g flag,
|
||
|
// or root check will fail every second time
|
||
3 years ago
|
var SCOPED_ABBR_TEST_RE = /\((c|tm|r)\)/i;
|
||
10 years ago
|
|
||
3 years ago
|
var SCOPED_ABBR_RE = /\((c|tm|r)\)/ig;
|
||
10 years ago
|
var SCOPED_ABBR = {
|
||
9 years ago
|
c: '©',
|
||
|
r: '®',
|
||
|
tm: '™'
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
function replaceFn(match, name) {
|
||
|
return SCOPED_ABBR[name.toLowerCase()];
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
function replace_scoped(inlineTokens) {
|
||
8 years ago
|
var i, token, inside_autolink = 0;
|
||
10 years ago
|
|
||
|
for (i = inlineTokens.length - 1; i >= 0; i--) {
|
||
|
token = inlineTokens[i];
|
||
8 years ago
|
|
||
|
if (token.type === 'text' && !inside_autolink) {
|
||
10 years ago
|
token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn);
|
||
10 years ago
|
}
|
||
8 years ago
|
|
||
|
if (token.type === 'link_open' && token.info === 'auto') {
|
||
|
inside_autolink--;
|
||
|
}
|
||
|
|
||
|
if (token.type === 'link_close' && token.info === 'auto') {
|
||
|
inside_autolink++;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
function replace_rare(inlineTokens) {
|
||
8 years ago
|
var i, token, inside_autolink = 0;
|
||
10 years ago
|
|
||
|
for (i = inlineTokens.length - 1; i >= 0; i--) {
|
||
|
token = inlineTokens[i];
|
||
8 years ago
|
|
||
|
if (token.type === 'text' && !inside_autolink) {
|
||
10 years ago
|
if (RARE_RE.test(token.content)) {
|
||
|
token.content = token.content
|
||
6 years ago
|
.replace(/\+-/g, '±')
|
||
|
// .., ..., ....... -> …
|
||
|
// but ?..... & !..... -> ?.. & !..
|
||
|
.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..')
|
||
|
.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',')
|
||
|
// em-dash
|
||
5 years ago
|
.replace(/(^|[^-])---(?=[^-]|$)/mg, '$1\u2014')
|
||
6 years ago
|
// en-dash
|
||
5 years ago
|
.replace(/(^|\s)--(?=\s|$)/mg, '$1\u2013')
|
||
|
.replace(/(^|[^-\s])--(?=[^-\s]|$)/mg, '$1\u2013');
|
||
10 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
if (token.type === 'link_open' && token.info === 'auto') {
|
||
|
inside_autolink--;
|
||
|
}
|
||
|
|
||
|
if (token.type === 'link_close' && token.info === 'auto') {
|
||
|
inside_autolink++;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
1 year ago
|
export default function replace(state) {
|
||
10 years ago
|
var blkIdx;
|
||
10 years ago
|
|
||
10 years ago
|
if (!state.md.options.typographer) { return; }
|
||
10 years ago
|
|
||
|
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
|
||
|
|
||
|
if (state.tokens[blkIdx].type !== 'inline') { continue; }
|
||
|
|
||
10 years ago
|
if (SCOPED_ABBR_TEST_RE.test(state.tokens[blkIdx].content)) {
|
||
10 years ago
|
replace_scoped(state.tokens[blkIdx].children);
|
||
|
}
|
||
|
|
||
|
if (RARE_RE.test(state.tokens[blkIdx].content)) {
|
||
|
replace_rare(state.tokens[blkIdx].children);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|
||
1 year ago
|
}
|