Skip to content

Commit e0ec865

Browse files
committed
feat(remark): migrate remark plugins to TS support
1 parent 394610c commit e0ec865

19 files changed

+1195
-145
lines changed

packages/tools/docs-tools/package.json

+13-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,23 @@
2727
"@kadena-dev/eslint-config": "workspace:^",
2828
"@kadena-dev/lint-package": "workspace:^",
2929
"@kadena-dev/shared-config": "workspace:^",
30-
"@types/mdast": "^4.0.1",
31-
"@types/node": "^18.17.14",
32-
"@types/unist": "^3.0.1",
33-
"eslint": "^8.48.0",
30+
"@types/js-yaml": "^4.0.9",
31+
"@types/mdast": "^4.0.3",
32+
"@types/node": "^20.9.0",
33+
"@types/react": "^18.2.15",
34+
"@types/unist": "^3.0.2",
35+
"acorn": "~8.8.2",
36+
"date-fns": "~2.30.0",
37+
"eslint": "^8.53.0",
38+
"js-yaml": "~4.1.0",
3439
"mdast": "^3.0.0",
3540
"prettier": "~3.0.3",
36-
"typescript": "5.2.2"
41+
"react": "^18.2.0",
42+
"typescript": "^5.2.2",
43+
"unified": "^11.0.4"
3744
},
3845
"dependencies": {
39-
"mdast-util-to-string": "~3.2.0",
46+
"mdast-util-to-string": "~4.0.0",
4047
"remark-gfm": "^4.0.0"
4148
}
4249
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
[
2+
{
3+
"id": "stuart.popejoy",
4+
"name": "Stuart Popejoy",
5+
"avatar": "/assets/blog/author/stuart.popejoy.jpg",
6+
"description": "Founder & CEO",
7+
"twitter": "SirLensALot"
8+
},
9+
{
10+
"id": "will.martino",
11+
"name": "Will Martino",
12+
"avatar": "/assets/blog/author/will.martino.jpg",
13+
"description": "Founder & President",
14+
"twitter": "_wjmartino_"
15+
},
16+
{
17+
"id": "doug.beardsley",
18+
"name": "Doug Beardsley",
19+
"avatar": "/assets/blog/author/doug.beardsley.jpg",
20+
"description": "Director of Engineering",
21+
"twitter": "BlockchainDoug"
22+
},
23+
{
24+
"id": "albert.g",
25+
"name": "Albert Groothedde",
26+
"avatar": "/assets/blog/author/albert.groothedde.jpg",
27+
"description": "Architect DevEx at Kadena",
28+
"twitter": "alber70g"
29+
},
30+
{
31+
"id": "vivienne.chen",
32+
"name": "Vivienne Chen",
33+
"avatar": "/assets/blog/author/vivienne.chen.jpg",
34+
"twitter": "ViviennexChen"
35+
},
36+
{
37+
"id": "john.wiegley",
38+
"name": "John Wiegley",
39+
"avatar": "/assets/blog/author/john.wiegley.jpg",
40+
"description": "Chief Technology Officer",
41+
"twitter": "jwiegley"
42+
},
43+
{
44+
"id": "leah.bingham",
45+
"name": "Leah Bingham",
46+
"avatar": "/assets/blog/author/leah.bingham.jpg"
47+
},
48+
{
49+
"id": "linda.ortega",
50+
"name": "Linda Ortega",
51+
"avatar": "/assets/blog/author/linda.ortega.jpg",
52+
"twitter": "linda_ioc"
53+
},
54+
{
55+
"id": "dan.wiggins",
56+
"name": "Dan Wiggins",
57+
"avatar": "/assets/blog/author/dan.wiggins.jpg"
58+
},
59+
{
60+
"id": "monica.quaintance",
61+
"name": "Monica Quaintance",
62+
"avatar": "/assets/blog/author/monica.quaintance.jpg",
63+
"twitter": "QuaintM"
64+
},
65+
{
66+
"id": "tyler.benster",
67+
"name": "Tyler Benster",
68+
"avatar": "/assets/blog/author/tyler.benster.jpg"
69+
},
70+
{
71+
"id": "ben.jessel",
72+
"name": "Ben Jessel",
73+
"avatar": "/assets/blog/author/ben.jessel.jpg"
74+
},
75+
{
76+
"id": "marie.leaf",
77+
"name": "Marie Leaf",
78+
"avatar": "/assets/blog/author/marie.leaf.jpg"
79+
},
80+
{
81+
"id": "anagha.mercado",
82+
"name": "Anagha Mercado",
83+
"avatar": "/assets/blog/author/anagha.mercado.jpg"
84+
},
85+
{
86+
"id": "miguel.angel",
87+
"name": "Miguel Angel Romero Jr.",
88+
"avatar": "/assets/blog/author/miguel.angel.romero.png"
89+
},
90+
{
91+
"id": "rebecca.rodriguez",
92+
"name": "Rebecca Rodriguez",
93+
"avatar": "/assets/blog/author/rebecca.rodriguez.jpg"
94+
},
95+
{
96+
"id": "tony.pham",
97+
"name": "Tony Pham",
98+
"avatar": "/assets/blog/author/tony.pham.jpg"
99+
},
100+
{
101+
"id": "taylor.rolfe",
102+
"name": "Taylor Rolfe",
103+
"avatar": "/assets/blog/author/taylor.rolfe.jpg"
104+
},
105+
{
106+
"id": "francesco.melpignano",
107+
"name": "Francesco Melpignano",
108+
"avatar": "/assets/blog/author/francesco.melpignano.jpg",
109+
"twitter": "fmelp"
110+
},
111+
{
112+
"id": "andy",
113+
"name": "Andy",
114+
"avatar": "/assets/blog/author/andy.tang.jpg",
115+
"description": "Frontend developer",
116+
"twitter": "andortang"
117+
},
118+
{
119+
"id": "jermaine.jong",
120+
"name": "Jermaine Jong",
121+
"avatar": "/assets/blog/author/jermaine.jong.png",
122+
"description": "Frontend developer"
123+
},
124+
{
125+
"id": "randy.daal",
126+
"name": "Randy Daal",
127+
"avatar": "/assets/blog/author/randy.daal.jpg",
128+
"description": "Chief Experience Officer @kadena_io",
129+
"twitter": "Randynamic_4"
130+
},
131+
{
132+
"id": "jose.cardona",
133+
"name": "Jose Cardona",
134+
"avatar": "/assets/blog/author/jose.cardona.jpg",
135+
"twitter": "JM0x5C"
136+
},
137+
{
138+
"id": "kadena",
139+
"name": "Kadena",
140+
"avatar": "/assets/blog/author/kadena.png",
141+
"description": "The only layer 1 PoW blockchain that scales.",
142+
"twitter": "kadena_io"
143+
},
144+
{
145+
"id": "heekyun",
146+
"name": "Heekyun",
147+
"avatar": "/assets/blog/author/hee.kyun.yun.jpg",
148+
"description": "Dev Experience @kadena_io PactJS and Pact Smart Contract Lead",
149+
"twitter": "HeeKyunYun1"
150+
},
151+
{
152+
"id": "robert.soeldner",
153+
"name": "Robert Soeldner",
154+
"avatar": "/assets/blog/author/robert.soeldner.jpeg",
155+
"description": "Software Engineer"
156+
},
157+
{
158+
"id": "emily.pillmore",
159+
"name": "Emily Pillmore",
160+
"avatar": "/assets/blog/author/emily.pillmore.jpg",
161+
"description": "M L E that's me",
162+
"twitter": "yandereidiot"
163+
},
164+
{
165+
"id": "ryan.levy",
166+
"name": "Ryan Levy",
167+
"avatar": "/assets/blog/author/ryan.levy.png",
168+
"description": "Business Development"
169+
},
170+
{
171+
"id": "glenn.reyes",
172+
"name": "Glenn Reyes",
173+
"avatar": "/assets/blog/author/glenn.reyes.jpeg",
174+
"description": "Developer Relations"
175+
}
176+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { Paragraph, PhrasingContent } from "mdast";
2+
import { DocsRootContent, ITree, Plugin, ChildrenWithValues, typeWithValue } from "./types";
3+
4+
const STARTNOTER_EGEXP = /^:::\s?(\w+)\s?([\w\s]+)?$/;
5+
const ENDNOTER_EGEXP = /:::\s*$/m;
6+
7+
let STARTELM: Paragraph | undefined;
8+
9+
// check the branch with the start of a notification
10+
// notifications start with ':::' followed by a label and maybe a title
11+
const isStart = (branch: DocsRootContent): null | string[] => {
12+
if('children' in branch === false) return null;
13+
14+
if ('children' in branch) {
15+
if(branch.children.length === 0) return null;
16+
const value = (branch.children as ChildrenWithValues)?.[0].value ?? '';
17+
console.log('isStart ', value.match(STARTNOTER_EGEXP));
18+
return value.match(STARTNOTER_EGEXP);
19+
}
20+
return null;
21+
};
22+
23+
const isEnd = (branch: DocsRootContent): boolean => {
24+
if('children' in branch === false) return false;
25+
if('children' in branch) {
26+
if (branch.children.length === 0) return false;
27+
28+
const endLeaf = (branch.children as ChildrenWithValues).find((item: typeWithValue) =>
29+
item.value?.match(ENDNOTER_EGEXP),
30+
);
31+
32+
if (endLeaf) {
33+
endLeaf.value = endLeaf.value?.replace(':::', '');
34+
return Boolean(endLeaf);
35+
}
36+
}
37+
38+
return false;
39+
};
40+
41+
// get the props (label and title) for the notification
42+
const getProps = (branch: DocsRootContent) => {
43+
if('children' in branch === false) return {};
44+
45+
if('children' in branch) {
46+
if (branch.children.length === 0) return {};
47+
const value = (branch.children as ChildrenWithValues)[0].value ?? '';
48+
49+
const match = STARTNOTER_EGEXP.exec(value);
50+
if (!match) return {};
51+
52+
return {
53+
label: match[1],
54+
title: match[2],
55+
};
56+
}
57+
};
58+
59+
const getStartElm = () => {
60+
return STARTELM;
61+
};
62+
63+
const setStartElm = (startElm: Paragraph) => {
64+
STARTELM = startElm;
65+
};
66+
67+
const clearStartElm = () => {
68+
STARTELM = undefined;
69+
};
70+
71+
/**
72+
* the reduce function will make all the branches between the start and end of a notification a child of the start branch
73+
*/
74+
const reduceToNotifications = (acc: DocsRootContent[], branch:DocsRootContent) => {
75+
if(branch.type === 'paragraph') {
76+
if (isStart(branch)) {
77+
setStartElm(branch);
78+
79+
const props = getProps(branch);
80+
// @ts-ignore
81+
branch.type = 'element';
82+
(branch.children as ChildrenWithValues)[0].value = '';
83+
branch.data = {
84+
hName: 'kda-notification',
85+
hProperties: props,
86+
};
87+
88+
if (isEnd(branch)) {
89+
clearStartElm();
90+
}
91+
92+
return [...acc, branch];
93+
}
94+
} else {
95+
return [...acc, branch];
96+
}
97+
98+
99+
const startElm = getStartElm();
100+
if (startElm) {
101+
if (isEnd(branch)) {
102+
clearStartElm();
103+
}
104+
console.log('startElm ', startElm);
105+
startElm.children = [
106+
...startElm.children,
107+
branch,
108+
] as PhrasingContent[];
109+
110+
// if in the middle or end of the notification do not return the branch.
111+
// the branch is now a child of the startbranch
112+
return acc;
113+
}
114+
115+
return [...acc, branch];
116+
};
117+
118+
const remarkAdmonitions = (): Plugin => {
119+
return async (tree): Promise<ITree> => {
120+
const newChildren = tree.children
121+
.reduce(reduceToNotifications, []);
122+
123+
tree.children = newChildren;
124+
return tree;
125+
};
126+
};
127+
128+
export { remarkAdmonitions as default };

packages/tools/docs-tools/src/remarkCheckForCodeTitle.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import type { Root, RootContent } from 'mdast';
2-
3-
interface Tree extends Omit<Root, 'children'> {
4-
children: RootContent[];
5-
}
1+
import { ITree, Plugin } from './types';
62

73
const replaceOrConcatString = (str?: string | null): string => {
84
if (!str) str = '';
@@ -18,12 +14,12 @@ const replaceOrConcatString = (str?: string | null): string => {
1814
return str;
1915
};
2016

21-
const remarkCheckForCodeTitle = () => {
22-
return async (tree: Tree) => {
17+
const remarkCheckForCodeTitle = (): Plugin => {
18+
return async (tree: ITree): Promise<ITree> => {
2319
const children = tree.children.map((node) => {
2420
const { type } = node;
2521

26-
if (type == 'code') {
22+
if (type === 'code') {
2723
node.meta = replaceOrConcatString(node.meta);
2824
}
2925

0 commit comments

Comments
 (0)