text.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Skip text characters for text token, place those to pending buffer
  2. // and increment current pos
  3. 'use strict';
  4. // Rule to skip pure text
  5. // '{}$%@~+=:' reserved for extentions
  6. // !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, or ~
  7. // !!!! Don't confuse with "Markdown ASCII Punctuation" chars
  8. // http://spec.commonmark.org/0.15/#ascii-punctuation-character
  9. function isTerminatorChar(ch) {
  10. switch (ch) {
  11. case 0x0A/* \n */:
  12. case 0x21/* ! */:
  13. case 0x23/* # */:
  14. case 0x24/* $ */:
  15. case 0x25/* % */:
  16. case 0x26/* & */:
  17. case 0x2A/* * */:
  18. case 0x2B/* + */:
  19. case 0x2D/* - */:
  20. case 0x3A/* : */:
  21. case 0x3C/* < */:
  22. case 0x3D/* = */:
  23. case 0x3E/* > */:
  24. case 0x40/* @ */:
  25. case 0x5B/* [ */:
  26. case 0x5C/* \ */:
  27. case 0x5D/* ] */:
  28. case 0x5E/* ^ */:
  29. case 0x5F/* _ */:
  30. case 0x60/* ` */:
  31. case 0x7B/* { */:
  32. case 0x7D/* } */:
  33. case 0x7E/* ~ */:
  34. return true;
  35. default:
  36. return false;
  37. }
  38. }
  39. module.exports = function text(state, silent) {
  40. var pos = state.pos;
  41. while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) {
  42. pos++;
  43. }
  44. if (pos === state.pos) { return false; }
  45. if (!silent) { state.pending += state.src.slice(state.pos, pos); }
  46. state.pos = pos;
  47. return true;
  48. };
  49. // Alternative implementation, for memory.
  50. //
  51. // It costs 10% of performance, but allows extend terminators list, if place it
  52. // to `ParcerInline` property. Probably, will switch to it sometime, such
  53. // flexibility required.
  54. /*
  55. var TERMINATOR_RE = /[\n!#$%&*+\-:<=>@[\\\]^_`{}~]/;
  56. module.exports = function text(state, silent) {
  57. var pos = state.pos,
  58. idx = state.src.slice(pos).search(TERMINATOR_RE);
  59. // first char is terminator -> empty text
  60. if (idx === 0) { return false; }
  61. // no terminator -> text till end of string
  62. if (idx < 0) {
  63. if (!silent) { state.pending += state.src.slice(pos); }
  64. state.pos = state.src.length;
  65. return true;
  66. }
  67. if (!silent) { state.pending += state.src.slice(pos, pos + idx); }
  68. state.pos += idx;
  69. return true;
  70. };*/