-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhance-dividers.ts
More file actions
56 lines (52 loc) · 1.65 KB
/
Copy pathenhance-dividers.ts
File metadata and controls
56 lines (52 loc) · 1.65 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
49
50
51
52
53
54
55
56
import { JSDOM } from 'jsdom';
import type { FEElement } from '../types/content';
const isDinkus = (element: FEElement): boolean => {
// Classic dinkus do not trigger dropcaps
if (
element._type !==
'model.dotcomrendering.pageElements.SubheadingBlockElement' &&
element._type !== 'model.dotcomrendering.pageElements.TextBlockElement'
) {
return false;
}
const frag = JSDOM.fragment(element.html);
if (!frag.firstChild) return false;
// A dinkus is can be spaced or unspaced
return (
frag.textContent === '***' ||
frag.textContent === '* * *' ||
frag.textContent === '•••' ||
frag.textContent === '• • •'
);
};
export const enhanceDividers = (elements: FEElement[]): FEElement[] =>
// checkForDividers loops the array of article elements looking for star flags and
// enhancing the data accordingly. In short, if a h2 tag is equal to * * * then we
// insert a divider and any the text element immediately afterwards should have dropCap
// set to true
elements.map<FEElement>((element, i) => {
const previous = elements[i - 1];
if (i === 0) {
// Always pass first element through
return element;
} else if (isDinkus(element)) {
// If this element is a dinkus, replace it with a divider
return {
_type: 'model.dotcomrendering.pageElements.DividerBlockElement',
};
} else if (
previous &&
// If the previous element was a dinkus and this one is a text block, set it's dropCap flag
isDinkus(previous) &&
element._type ===
'model.dotcomrendering.pageElements.TextBlockElement'
) {
return {
...element,
dropCap: 'on',
};
} else {
// Otherwise, do nothing
return element;
}
});