paragraph.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Paragraph
  2. 'use strict';
  3. module.exports = function paragraph(state, startLine/*, endLine*/) {
  4. var content, terminate, i, l, token, oldParentType,
  5. nextLine = startLine + 1,
  6. terminatorRules = state.md.block.ruler.getRules('paragraph'),
  7. endLine = state.lineMax;
  8. oldParentType = state.parentType;
  9. state.parentType = 'paragraph';
  10. // jump line-by-line until empty one or EOF
  11. for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
  12. // this would be a code block normally, but after paragraph
  13. // it's considered a lazy continuation regardless of what's there
  14. if (state.sCount[nextLine] - state.blkIndent > 3) { continue; }
  15. // quirk for blockquotes, this line should already be checked by that rule
  16. if (state.sCount[nextLine] < 0) { continue; }
  17. // Some tags can terminate paragraph without empty line.
  18. terminate = false;
  19. for (i = 0, l = terminatorRules.length; i < l; i++) {
  20. if (terminatorRules[i](state, nextLine, endLine, true)) {
  21. terminate = true;
  22. break;
  23. }
  24. }
  25. if (terminate) { break; }
  26. }
  27. content = state.getLines(startLine, nextLine, state.blkIndent, false).trim();
  28. state.line = nextLine;
  29. token = state.push('paragraph_open', 'p', 1);
  30. token.map = [ startLine, state.line ];
  31. token = state.push('inline', '', 0);
  32. token.content = content;
  33. token.map = [ startLine, state.line ];
  34. token.children = [];
  35. token = state.push('paragraph_close', 'p', -1);
  36. state.parentType = oldParentType;
  37. return true;
  38. };