emphasis.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Process *this* and _that_
  2. //
  3. 'use strict';
  4. // Insert each marker as a separate text token, and add it to delimiter list
  5. //
  6. module.exports.tokenize = function emphasis(state, silent) {
  7. var i, scanned, token,
  8. start = state.pos,
  9. marker = state.src.charCodeAt(start);
  10. if (silent) { return false; }
  11. if (marker !== 0x5F /* _ */ && marker !== 0x2A /* * */) { return false; }
  12. scanned = state.scanDelims(state.pos, marker === 0x2A);
  13. for (i = 0; i < scanned.length; i++) {
  14. token = state.push('text', '', 0);
  15. token.content = String.fromCharCode(marker);
  16. state.delimiters.push({
  17. // Char code of the starting marker (number).
  18. //
  19. marker: marker,
  20. // Total length of these series of delimiters.
  21. //
  22. length: scanned.length,
  23. // An amount of characters before this one that's equivalent to
  24. // current one. In plain English: if this delimiter does not open
  25. // an emphasis, neither do previous `jump` characters.
  26. //
  27. // Used to skip sequences like "*****" in one step, for 1st asterisk
  28. // value will be 0, for 2nd it's 1 and so on.
  29. //
  30. jump: i,
  31. // A position of the token this delimiter corresponds to.
  32. //
  33. token: state.tokens.length - 1,
  34. // If this delimiter is matched as a valid opener, `end` will be
  35. // equal to its position, otherwise it's `-1`.
  36. //
  37. end: -1,
  38. // Boolean flags that determine if this delimiter could open or close
  39. // an emphasis.
  40. //
  41. open: scanned.can_open,
  42. close: scanned.can_close
  43. });
  44. }
  45. state.pos += scanned.length;
  46. return true;
  47. };
  48. function postProcess(state, delimiters) {
  49. var i,
  50. startDelim,
  51. endDelim,
  52. token,
  53. ch,
  54. isStrong,
  55. max = delimiters.length;
  56. for (i = max - 1; i >= 0; i--) {
  57. startDelim = delimiters[i];
  58. if (startDelim.marker !== 0x5F/* _ */ && startDelim.marker !== 0x2A/* * */) {
  59. continue;
  60. }
  61. // Process only opening markers
  62. if (startDelim.end === -1) {
  63. continue;
  64. }
  65. endDelim = delimiters[startDelim.end];
  66. // If the previous delimiter has the same marker and is adjacent to this one,
  67. // merge those into one strong delimiter.
  68. //
  69. // `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
  70. //
  71. isStrong = i > 0 &&
  72. delimiters[i - 1].end === startDelim.end + 1 &&
  73. delimiters[i - 1].token === startDelim.token - 1 &&
  74. delimiters[startDelim.end + 1].token === endDelim.token + 1 &&
  75. delimiters[i - 1].marker === startDelim.marker;
  76. ch = String.fromCharCode(startDelim.marker);
  77. token = state.tokens[startDelim.token];
  78. token.type = isStrong ? 'strong_open' : 'em_open';
  79. token.tag = isStrong ? 'strong' : 'em';
  80. token.nesting = 1;
  81. token.markup = isStrong ? ch + ch : ch;
  82. token.content = '';
  83. token = state.tokens[endDelim.token];
  84. token.type = isStrong ? 'strong_close' : 'em_close';
  85. token.tag = isStrong ? 'strong' : 'em';
  86. token.nesting = -1;
  87. token.markup = isStrong ? ch + ch : ch;
  88. token.content = '';
  89. if (isStrong) {
  90. state.tokens[delimiters[i - 1].token].content = '';
  91. state.tokens[delimiters[startDelim.end + 1].token].content = '';
  92. i--;
  93. }
  94. }
  95. }
  96. // Walk through delimiter list and replace text tokens with tags
  97. //
  98. module.exports.postProcess = function emphasis(state) {
  99. var curr,
  100. tokens_meta = state.tokens_meta,
  101. max = state.tokens_meta.length;
  102. postProcess(state, state.delimiters);
  103. for (curr = 0; curr < max; curr++) {
  104. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  105. postProcess(state, tokens_meta[curr].delimiters);
  106. }
  107. }
  108. };