emphasis.js 3.5 KB

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