-
Notifications
You must be signed in to change notification settings - Fork 529
Expand file tree
/
Copy pathconvert-to-paragraphs.js
More file actions
48 lines (39 loc) 路 1.17 KB
/
Copy pathconvert-to-paragraphs.js
File metadata and controls
48 lines (39 loc) 路 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { brsToPs, convertNodeTo } from 'utils/dom';
import { DIV_TO_P_BLOCK_TAGS } from './constants';
function convertDivs($) {
$('div').each((index, div) => {
const $div = $(div);
const convertible = $div.find(DIV_TO_P_BLOCK_TAGS).length === 0;
if (convertible) {
convertNodeTo($div, $, 'p');
}
});
return $;
}
function convertSpans($) {
$('span').each((index, span) => {
const $span = $(span);
const convertible = $span.parents('p, div, li, figcaption').length === 0;
if (convertible) {
convertNodeTo($span, $, 'p');
}
});
return $;
}
// Loop through the provided doc, and convert any p-like elements to
// actual paragraph tags.
//
// Things fitting this criteria:
// * Multiple consecutive <br /> tags.
// * <div /> tags without block level elements inside of them
// * <span /> tags who are not children of <p /> or <div /> tags.
//
// :param $: A cheerio object to search
// :return cheerio object with new p elements
// (By-reference mutation, though. Returned just for convenience.)
export default function convertToParagraphs($) {
$ = brsToPs($);
$ = convertDivs($);
$ = convertSpans($);
return $;
}