-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft-admonition.mjs
52 lines (47 loc) · 1.25 KB
/
draft-admonition.mjs
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
import { strict as assert } from 'node:assert';
const _meta = { draftSAdmonition: true };
export function draftSAdmonition({
text = '',
type = '_meta',
title = 'Drafted',
} = {}) {
assert(typeof title === 'string', new TypeError('`title` should be string'));
assert(typeof type === 'string', new TypeError('`type` should be string'));
assert(typeof text === 'string', new TypeError('`text` should be string'));
return (tree, file) => {
if (
text &&
file.data.frontMatter?.draft &&
!tree.children.some((node) => node._meta?.draftSAdmonition)
) {
const index =
tree.children.findIndex(
(node) => node.type === 'heading' && node.depth === 1,
) || 0;
const context = {
type: 'paragraph',
children: [{ type: 'text', value: text }],
};
tree.children.splice(index + 1, 0, {
type: 'containerDirective',
name: type,
_meta,
children: [
{
type: 'paragraph',
data: {
directiveLabel: true,
},
children: [
{
type: 'text',
value: title,
},
],
},
context,
],
});
}
};
}