format-heading-as-setext.js 757 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * @typedef {import('mdast').Heading} Heading
  3. * @typedef {import('../types.js').State} State
  4. */
  5. import {EXIT, visit} from 'unist-util-visit'
  6. import {toString} from 'mdast-util-to-string'
  7. /**
  8. * @param {Heading} node
  9. * @param {State} state
  10. * @returns {boolean}
  11. */
  12. export function formatHeadingAsSetext(node, state) {
  13. let literalWithBreak = false
  14. // Look for literals with a line break.
  15. // Note that this also
  16. visit(node, function (node) {
  17. if (
  18. ('value' in node && /\r?\n|\r/.test(node.value)) ||
  19. node.type === 'break'
  20. ) {
  21. literalWithBreak = true
  22. return EXIT
  23. }
  24. })
  25. return Boolean(
  26. (!node.depth || node.depth < 3) &&
  27. toString(node) &&
  28. (state.options.setext || literalWithBreak)
  29. )
  30. }