hr.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Horizontal rule
  2. 'use strict';
  3. var isSpace = require('../common/utils').isSpace;
  4. module.exports = function hr(state, startLine, endLine, silent) {
  5. var marker, cnt, ch, token,
  6. pos = state.bMarks[startLine] + state.tShift[startLine],
  7. max = state.eMarks[startLine];
  8. // if it's indented more than 3 spaces, it should be a code block
  9. if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
  10. marker = state.src.charCodeAt(pos++);
  11. // Check hr marker
  12. if (marker !== 0x2A/* * */ &&
  13. marker !== 0x2D/* - */ &&
  14. marker !== 0x5F/* _ */) {
  15. return false;
  16. }
  17. // markers can be mixed with spaces, but there should be at least 3 of them
  18. cnt = 1;
  19. while (pos < max) {
  20. ch = state.src.charCodeAt(pos++);
  21. if (ch !== marker && !isSpace(ch)) { return false; }
  22. if (ch === marker) { cnt++; }
  23. }
  24. if (cnt < 3) { return false; }
  25. if (silent) { return true; }
  26. state.line = startLine + 1;
  27. token = state.push('hr', 'hr', 0);
  28. token.map = [ startLine, state.line ];
  29. token.markup = Array(cnt + 1).join(String.fromCharCode(marker));
  30. return true;
  31. };