replacements.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Simple typographic replacements
  2. //
  3. // (c) (C) → ©
  4. // (tm) (TM) → ™
  5. // (r) (R) → ®
  6. // +- → ±
  7. // (p) (P) -> §
  8. // ... → … (also ?.... → ?.., !.... → !..)
  9. // ???????? → ???, !!!!! → !!!, `,,` → `,`
  10. // -- → –, --- → —
  11. //
  12. 'use strict';
  13. // TODO:
  14. // - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
  15. // - miltiplication 2 x 4 -> 2 × 4
  16. var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/;
  17. // Workaround for phantomjs - need regex without /g flag,
  18. // or root check will fail every second time
  19. var SCOPED_ABBR_TEST_RE = /\((c|tm|r|p)\)/i;
  20. var SCOPED_ABBR_RE = /\((c|tm|r|p)\)/ig;
  21. var SCOPED_ABBR = {
  22. c: '©',
  23. r: '®',
  24. p: '§',
  25. tm: '™'
  26. };
  27. function replaceFn(match, name) {
  28. return SCOPED_ABBR[name.toLowerCase()];
  29. }
  30. function replace_scoped(inlineTokens) {
  31. var i, token, inside_autolink = 0;
  32. for (i = inlineTokens.length - 1; i >= 0; i--) {
  33. token = inlineTokens[i];
  34. if (token.type === 'text' && !inside_autolink) {
  35. token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn);
  36. }
  37. if (token.type === 'link_open' && token.info === 'auto') {
  38. inside_autolink--;
  39. }
  40. if (token.type === 'link_close' && token.info === 'auto') {
  41. inside_autolink++;
  42. }
  43. }
  44. }
  45. function replace_rare(inlineTokens) {
  46. var i, token, inside_autolink = 0;
  47. for (i = inlineTokens.length - 1; i >= 0; i--) {
  48. token = inlineTokens[i];
  49. if (token.type === 'text' && !inside_autolink) {
  50. if (RARE_RE.test(token.content)) {
  51. token.content = token.content
  52. .replace(/\+-/g, '±')
  53. // .., ..., ....... -> …
  54. // but ?..... & !..... -> ?.. & !..
  55. .replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..')
  56. .replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',')
  57. // em-dash
  58. .replace(/(^|[^-])---(?=[^-]|$)/mg, '$1\u2014')
  59. // en-dash
  60. .replace(/(^|\s)--(?=\s|$)/mg, '$1\u2013')
  61. .replace(/(^|[^-\s])--(?=[^-\s]|$)/mg, '$1\u2013');
  62. }
  63. }
  64. if (token.type === 'link_open' && token.info === 'auto') {
  65. inside_autolink--;
  66. }
  67. if (token.type === 'link_close' && token.info === 'auto') {
  68. inside_autolink++;
  69. }
  70. }
  71. }
  72. module.exports = function replace(state) {
  73. var blkIdx;
  74. if (!state.md.options.typographer) { return; }
  75. for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
  76. if (state.tokens[blkIdx].type !== 'inline') { continue; }
  77. if (SCOPED_ABBR_TEST_RE.test(state.tokens[blkIdx].content)) {
  78. replace_scoped(state.tokens[blkIdx].children);
  79. }
  80. if (RARE_RE.test(state.tokens[blkIdx].content)) {
  81. replace_rare(state.tokens[blkIdx].children);
  82. }
  83. }
  84. };