backticks.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Parse backticks
  2. 'use strict';
  3. module.exports = function backtick(state, silent) {
  4. var start, max, marker, matchStart, matchEnd, token,
  5. pos = state.pos,
  6. ch = state.src.charCodeAt(pos);
  7. if (ch !== 0x60/* ` */) { return false; }
  8. start = pos;
  9. pos++;
  10. max = state.posMax;
  11. while (pos < max && state.src.charCodeAt(pos) === 0x60/* ` */) { pos++; }
  12. marker = state.src.slice(start, pos);
  13. matchStart = matchEnd = pos;
  14. while ((matchStart = state.src.indexOf('`', matchEnd)) !== -1) {
  15. matchEnd = matchStart + 1;
  16. while (matchEnd < max && state.src.charCodeAt(matchEnd) === 0x60/* ` */) { matchEnd++; }
  17. if (matchEnd - matchStart === marker.length) {
  18. if (!silent) {
  19. token = state.push('code_inline', 'code', 0);
  20. token.markup = marker;
  21. token.content = state.src.slice(pos, matchStart)
  22. .replace(/[ \n]+/g, ' ')
  23. .trim();
  24. }
  25. state.pos = matchEnd;
  26. return true;
  27. }
  28. }
  29. if (!silent) { state.pending += marker; }
  30. state.pos += marker.length;
  31. return true;
  32. };