You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mercury-parser/src/utils/dom/brs-to-ps.js

30 lines
740 B
JavaScript

import { paragraphize } from './index';
// ## NOTES:
// Another good candidate for refactoring/optimizing.
// Very imperative code, I don't love it. - AP
// Given cheerio object, convert consecutive <br /> tags into
// <p /> tags instead.
//
// :param $: A cheerio object
export default function brsToPs($) {
let collapsing = false;
$('br').each((index, element) => {
const $element = $(element);
const nextElement = $element.next().get(0);
if (nextElement && nextElement.tagName.toLowerCase() === 'br') {
collapsing = true;
$element.remove();
} else if (collapsing) {
collapsing = false;
// $(element).replaceWith('<p />')
paragraphize(element, $, true);
}
});
return $;
}