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.
 
 
 

57 lines
1.6 KiB

// Simple typographyc replacements
//
'use strict';
module.exports = function replace(t, state) {
var i, token, text,
tokens = state.tokens,
options = t.options;
for (i = tokens.length - 1; i >= 0; i--) {
token = tokens[i];
if (token.type === 'text') {
text = token.content;
if (text.indexOf('(') >= 0) {
if (options.copyright) {
text = text.replace(/\(c\)/gi, '©');
}
if (options.trademark) {
text = text.replace(/\(tm\)/gi, '™');
}
if (options.registered) {
text = text.replace(/\(r\)/gi, '®');
}
if (options.paragraph) {
text = text.replace(/\(p\)/gi, '§');
}
}
if (options.plusminus && text.indexOf('+-') >= 0) {
text = text.replace(/\+-/g, '±');
}
if (options.ellipsis && text.indexOf('..') >= 0) {
// .., ..., ....... -> …
// but ?..... & !..... -> ?.. & !..
text = text.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..');
}
if (options.dupes &&
(text.indexOf('????') >= 0 ||
text.indexOf('!!!!') >= 0 ||
text.indexOf(',,') >= 0)) {
text = text.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',');
}
if (options.dashes && text.indexOf('--') >= 0) {
text = text
// em-dash
.replace(/(^|[^-])---([^-]|$)/mg, '$1\u2014$2')
// en-dash
.replace(/(^|\s)--(\s|$)/mg, '$1\u2013$2')
.replace(/(^|[^-\s])--([^-\s]|$)/mg, '$1\u2013$2');
}
token.content = text;
}
}
};