escape.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Process escaped chars and hardbreaks
  2. 'use strict';
  3. var isSpace = require('../common/utils').isSpace;
  4. var ESCAPED = [];
  5. for (var i = 0; i < 256; i++) { ESCAPED.push(0); }
  6. '\\!"#$%&\'()*+,./:;<=>?@[]^_`{|}~-'
  7. .split('').forEach(function (ch) { ESCAPED[ch.charCodeAt(0)] = 1; });
  8. module.exports = function escape(state, silent) {
  9. var ch1, ch2, origStr, escapedStr, token, pos = state.pos, max = state.posMax;
  10. if (state.src.charCodeAt(pos) !== 0x5C/* \ */) return false;
  11. pos++;
  12. // '\' at the end of the inline block
  13. if (pos >= max) return false;
  14. ch1 = state.src.charCodeAt(pos);
  15. if (ch1 === 0x0A) {
  16. if (!silent) {
  17. state.push('hardbreak', 'br', 0);
  18. }
  19. pos++;
  20. // skip leading whitespaces from next line
  21. while (pos < max) {
  22. ch1 = state.src.charCodeAt(pos);
  23. if (!isSpace(ch1)) break;
  24. pos++;
  25. }
  26. state.pos = pos;
  27. return true;
  28. }
  29. escapedStr = state.src[pos];
  30. if (ch1 >= 0xD800 && ch1 <= 0xDBFF && pos + 1 < max) {
  31. ch2 = state.src.charCodeAt(pos + 1);
  32. if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
  33. escapedStr += state.src[pos + 1];
  34. pos++;
  35. }
  36. }
  37. origStr = '\\' + escapedStr;
  38. if (!silent) {
  39. token = state.push('text_special', '', 0);
  40. if (ch1 < 256 && ESCAPED[ch1] !== 0) {
  41. token.content = escapedStr;
  42. } else {
  43. token.content = origStr;
  44. }
  45. token.markup = origStr;
  46. token.info = 'escape';
  47. }
  48. state.pos = pos + 1;
  49. return true;
  50. };