hr.mjs 1.0 KB

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