forked from reactiflux/reactibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-docs.ts
More file actions
136 lines (123 loc) · 3.74 KB
/
react-docs.ts
File metadata and controls
136 lines (123 loc) · 3.74 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import fetch from "node-fetch";
import { gitHubReadToken } from "./env.js";
const LOOKUP_REGEX = /<Intro>\s*(.*?)\s*<\/Intro>/gs;
const LINK_REGEX = /\[([^\]]+)\]\((?!https?:\/\/)([^)]+)\)/g;
const EXTRACT_SEARCH_KEY_REGEX = /(?<=!(docs|react-docs)\s)[^\s.]+/;
const BASE_URL =
"https://api.github.com/repos/reactjs/react.dev/contents/src/content/reference/";
export const getReactDocsContent = async (searchPath: string) => {
try {
const response = await fetch(`${BASE_URL}${searchPath}.md`, {
method: "GET",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${gitHubReadToken}`,
"X-GitHub-Api-Version": "2022-11-28",
},
});
const json = (await response.json()) as { content: string };
const contentBase64 = json.content;
const decodedContent = Buffer.from(contentBase64, "base64").toString(
"utf8",
);
return processReactDocumentation(decodedContent);
} catch (error) {
console.error("Error:", error);
return null;
}
};
export const getReactDocsSearchKey = (search: string) => {
const normalizedSearch = search.toLowerCase();
return REACT_AVAILABLE_DOCS.find((key) => {
const keyParts = key.split("/");
const name = keyParts[keyParts.length - 1];
const namespace =
keyParts.length <= 2 ? key : `${keyParts[0]}/${keyParts[2]}`;
return (
namespace.toLowerCase() === normalizedSearch ||
name.toLowerCase() === normalizedSearch
);
});
};
export const extractSearchKey = (search: string) => {
const matches = search.match(EXTRACT_SEARCH_KEY_REGEX);
return matches ? matches[0] : "";
};
const processReactDocumentation = (content: string) => {
const patchedContentLinks = content.replace(LINK_REGEX, (_, text, link) => {
return `[${text}](https://react.dev${link})`;
});
const matches = [...patchedContentLinks.matchAll(LOOKUP_REGEX)];
if (matches.length > 0) {
const [introContent] = matches.map(([, match]) =>
match.trim().replace(/\n\n/g, "\n"),
);
return introContent;
}
return null;
};
const REACT_AVAILABLE_DOCS = [
"react/act",
"react/cache",
"react/Children",
"react/cloneElement",
"react/Component",
"react/createContext",
"react/createElement",
"react/createRef",
"react/experimental_taintObjectReference",
"react/experimental_taintUniqueValue",
"react/experimental_useEffectEvent",
"react/forwardRef",
"react/Fragment",
"react/isValidElement",
"react/lazy",
"react/legacy",
"react/memo",
"react/Profiler",
"react/PureComponent",
"react/startTransition",
"react/StrictMode",
"react/Suspense",
"react/use",
"react/useActionState",
"react/useCallback",
"react/useContext",
"react/useDebugValue",
"react/useDeferredValue",
"react/useEffect",
"react/useId",
"react/useImperativeHandle",
"react/useInsertionEffect",
"react/useLayoutEffect",
"react/useMemo",
"react/useOptimistic",
"react/useReducer",
"react/useRef",
"react/useState",
"react/useSyncExternalStore",
"react/useTransition",
"react-dom/client/createRoot",
"react-dom/client/hydrateRoot",
"react-dom/createPortal",
"react-dom/flushSync",
"react-dom/hooks/useFormStatus",
"react-dom/preconnect",
"react-dom/prefetchDNS",
"react-dom/preinit",
"react-dom/preinitModule",
"react-dom/preload",
"react-dom/preloadModule",
"react-dom/server/renderToPipeableStream",
"react-dom/server/renderToReadableStream",
"react-dom/server/renderToStaticMarkup",
"react-dom/server/renderToString",
"react-dom/static/prerender",
"react-dom/static/prerenderToNodeStream",
"react/Activity",
"react/useEffectEvent",
"react-dom/cacheSignal",
"react-dom/static/resume",
"react-dom/static/resumeAndPrerender",
"react-dom/static/resumeToPipeableStream",
];