-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle-toc.ts
35 lines (30 loc) · 923 Bytes
/
handle-toc.ts
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
import {META, NODE} from '../constants.js';
import {Item, Leaf, Text} from '../types/item.types.js';
import {MetaNode} from '../types/meta.types.js';
import {merge} from '../utils/merge.js';
export const addTocItem = (item: Item, tocStyle = {}) => {
if ('text' in item && typeof item.text === 'string') {
item.tocItem = true;
merge(item, tocStyle);
} else if ('stack' in item) {
const text = item.stack.find(s => 'text' in s) as Text | Leaf | undefined;
if (text && typeof text !== 'string') {
text.tocItem = true;
merge(text, tocStyle);
}
}
};
export const handleHeadlineToc = (item: MetaNode<Item>) => {
const tocStyle = {};
if (item[META]?.[NODE]?.nodeName === 'H1') {
Object.assign(tocStyle, {
tocNumberStyle: {bold: true}
});
} else {
Object.assign(tocStyle, {
tocMargin: [10, 0, 0, 0]
});
}
addTocItem(item, tocStyle);
return item;
};