lheading.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // lheading (---, ===)
  2. 'use strict';
  3. module.exports = function lheading(state, startLine, endLine/*, silent*/) {
  4. var content, terminate, i, l, token, pos, max, level, marker,
  5. nextLine = startLine + 1, oldParentType,
  6. terminatorRules = state.md.block.ruler.getRules('paragraph');
  7. // if it's indented more than 3 spaces, it should be a code block
  8. if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
  9. oldParentType = state.parentType;
  10. state.parentType = 'paragraph'; // use paragraph to match terminatorRules
  11. // jump line-by-line until empty one or EOF
  12. for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
  13. // this would be a code block normally, but after paragraph
  14. // it's considered a lazy continuation regardless of what's there
  15. if (state.sCount[nextLine] - state.blkIndent > 3) { continue; }
  16. //
  17. // Check for underline in setext header
  18. //
  19. if (state.sCount[nextLine] >= state.blkIndent) {
  20. pos = state.bMarks[nextLine] + state.tShift[nextLine];
  21. max = state.eMarks[nextLine];
  22. if (pos < max) {
  23. marker = state.src.charCodeAt(pos);
  24. if (marker === 0x2D/* - */ || marker === 0x3D/* = */) {
  25. pos = state.skipChars(pos, marker);
  26. pos = state.skipSpaces(pos);
  27. if (pos >= max) {
  28. level = (marker === 0x3D/* = */ ? 1 : 2);
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. // quirk for blockquotes, this line should already be checked by that rule
  35. if (state.sCount[nextLine] < 0) { continue; }
  36. // Some tags can terminate paragraph without empty line.
  37. terminate = false;
  38. for (i = 0, l = terminatorRules.length; i < l; i++) {
  39. if (terminatorRules[i](state, nextLine, endLine, true)) {
  40. terminate = true;
  41. break;
  42. }
  43. }
  44. if (terminate) { break; }
  45. }
  46. if (!level) {
  47. // Didn't find valid underline
  48. return false;
  49. }
  50. content = state.getLines(startLine, nextLine, state.blkIndent, false).trim();
  51. state.line = nextLine + 1;
  52. token = state.push('heading_open', 'h' + String(level), 1);
  53. token.markup = String.fromCharCode(marker);
  54. token.map = [ startLine, state.line ];
  55. token = state.push('inline', '', 0);
  56. token.content = content;
  57. token.map = [ startLine, state.line - 1 ];
  58. token.children = [];
  59. token = state.push('heading_close', 'h' + String(level), -1);
  60. token.markup = String.fromCharCode(marker);
  61. state.parentType = oldParentType;
  62. return true;
  63. };