-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultClasses.js
More file actions
41 lines (35 loc) · 1.34 KB
/
defaultClasses.js
File metadata and controls
41 lines (35 loc) · 1.34 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
import { visit } from 'unist-util-visit';
const HEADING_CLASSES = ['alpha', 'beta', 'gamma', 'delta'];
/*
* Adds classes to headings and other content elements. For example, `<h2>` becomes
* `<h2 class="beta">. Many HTML elements should have default styles applied to them
* when used in a documentation article. However, there may be cases where those
* we don’t need the default styles, e.g. when using a list for a horizontal navigation.
* Applying the default styles explicitly using classes is easier and has fewer side
* effects compared to applying them using tag selectors (in which case they'd need to
* be reset manually whenever the default styles are not needed).
*/
export default function defaultClasses() {
return () => {
return (tree) => {
visit(tree, ['heading', 'list'], (node) => {
node.data = node.data || {};
node.data.hProperties = node.data.hProperties || {};
if (node.type === 'heading') {
node.data.hProperties = {
...node.data.hProperties,
class: HEADING_CLASSES[node.depth - 1],
};
return;
}
if (node.type === 'list') {
node.data.hProperties = {
...node.data.hProperties,
class: node.ordered ? 'ordered-list' : 'unordered-list',
};
return;
}
});
};
};
}