replacements.mjs 2.6 KB

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