// Simple typographyc replacements // 'use strict'; // TODO: // - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾ // - miltiplication 2 x 4 -> 2 × 4 var COPY_RE = /\((c|tm|r|p)\)/i; var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/; module.exports = function replace(state) { var i, token, text, inlineTokens, blkIdx; if (!state.options.typographer) { return; } for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) { if (state.tokens[blkIdx].type !== 'inline') { continue; } inlineTokens = state.tokens[blkIdx].children; for (i = inlineTokens.length - 1; i >= 0; i--) { token = inlineTokens[i]; if (token.type === 'text') { text = token.content; if (COPY_RE.test(text)) { text = text.replace(/\(c\)/gi, '©') .replace(/\(tm\)/gi, '™') .replace(/\(r\)/gi, '®') .replace(/\(p\)/gi, '§'); } if (RARE_RE.test(text)) { text = text.replace(/\+-/g, '±') // .., ..., ....... -> … // but ?..... & !..... -> ?.. & !.. .replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..') .replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',') // 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; } } } };