emphasis.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // A position of the token this delimiter corresponds to.
  24. //
  25. token: state.tokens.length - 1,
  26. // If this delimiter is matched as a valid opener, `end` will be
  27. // equal to its position, otherwise it's `-1`.
  28. //
  29. end: -1,
  30. // Boolean flags that determine if this delimiter could open or close
  31. // an emphasis.
  32. //
  33. open: scanned.can_open,
  34. close: scanned.can_close
  35. });
  36. }
  37. state.pos += scanned.length;
  38. return true;
  39. };
  40. function postProcess(state, delimiters) {
  41. var i,
  42. startDelim,
  43. endDelim,
  44. token,
  45. ch,
  46. isStrong,
  47. max = delimiters.length;
  48. for (i = max - 1; i >= 0; i--) {
  49. startDelim = delimiters[i];
  50. if (startDelim.marker !== 0x5F/* _ */ && startDelim.marker !== 0x2A/* * */) {
  51. continue;
  52. }
  53. // Process only opening markers
  54. if (startDelim.end === -1) {
  55. continue;
  56. }
  57. endDelim = delimiters[startDelim.end];
  58. // If the previous delimiter has the same marker and is adjacent to this one,
  59. // merge those into one strong delimiter.
  60. //
  61. // `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
  62. //
  63. isStrong = i > 0 &&
  64. delimiters[i - 1].end === startDelim.end + 1 &&
  65. // check that first two markers match and adjacent
  66. delimiters[i - 1].marker === startDelim.marker &&
  67. delimiters[i - 1].token === startDelim.token - 1 &&
  68. // check that last two markers are adjacent (we can safely assume they match)
  69. delimiters[startDelim.end + 1].token === endDelim.token + 1;
  70. ch = String.fromCharCode(startDelim.marker);
  71. token = state.tokens[startDelim.token];
  72. token.type = isStrong ? 'strong_open' : 'em_open';
  73. token.tag = isStrong ? 'strong' : 'em';
  74. token.nesting = 1;
  75. token.markup = isStrong ? ch + ch : ch;
  76. token.content = '';
  77. token = state.tokens[endDelim.token];
  78. token.type = isStrong ? 'strong_close' : 'em_close';
  79. token.tag = isStrong ? 'strong' : 'em';
  80. token.nesting = -1;
  81. token.markup = isStrong ? ch + ch : ch;
  82. token.content = '';
  83. if (isStrong) {
  84. state.tokens[delimiters[i - 1].token].content = '';
  85. state.tokens[delimiters[startDelim.end + 1].token].content = '';
  86. i--;
  87. }
  88. }
  89. }
  90. // Walk through delimiter list and replace text tokens with tags
  91. //
  92. module.exports.postProcess = function emphasis(state) {
  93. var curr,
  94. tokens_meta = state.tokens_meta,
  95. max = state.tokens_meta.length;
  96. postProcess(state, state.delimiters);
  97. for (curr = 0; curr < max; curr++) {
  98. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  99. postProcess(state, tokens_meta[curr].delimiters);
  100. }
  101. }
  102. };