p-as-heading-matches.js 663 B

123456789101112131415161718192021
  1. function pAsHeadingMatches(node) {
  2. const children = Array.from(node.parentNode.childNodes);
  3. const nodeText = node.textContent.trim();
  4. const isSentence = /[.!?:;](?![.!?:;])/g;
  5. // Check that there is text, and it is not more than a single sentence
  6. if (nodeText.length === 0 || (nodeText.match(isSentence) || []).length >= 2) {
  7. return false;
  8. }
  9. // Grab sibling p element following the current node
  10. const siblingsAfter = children
  11. .slice(children.indexOf(node) + 1)
  12. .filter(
  13. elm => elm.nodeName.toUpperCase() === 'P' && elm.textContent.trim() !== ''
  14. );
  15. return siblingsAfter.length !== 0;
  16. }
  17. export default pAsHeadingMatches;