-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: implement poc for react documentation * fix: mdn logo not found
- Loading branch information
Showing
2 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import fetch from "node-fetch"; | ||
import { gitHubToken } from "./env"; | ||
|
||
const LOOKUP_REGEX = /<Intro>\s*(.*?)\s*<\/Intro>/gs; | ||
const LINK_REGEX = /\[([^\]]+)\]\((?!https?:\/\/)([^)]+)\)/g; | ||
|
||
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 ${gitHubToken}`, | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
}, | ||
}); | ||
const json = await response.json(); | ||
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 | ||
); | ||
}); | ||
}; | ||
|
||
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()); | ||
return introContent; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const REACT_AVAILABLE_DOCS = [ | ||
"react/cache", | ||
"react/Children", | ||
"react/cloneElement", | ||
"react/Component", | ||
"react/createContext", | ||
"react/createElement", | ||
"react/createFactory", | ||
"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-client", | ||
"react/use-server", | ||
"react/use", | ||
"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/hooks/useFormState", | ||
"react-dom/hooks/useFormStatus", | ||
"react-dom/server/renderToNodeStream", | ||
"react-dom/server/renderToPipeableStream", | ||
"react-dom/server/renderToReadableStream", | ||
"react-dom/server/renderToStaticMarkup", | ||
"react-dom/server/renderToStaticNodeStream", | ||
"react-dom/server/renderToString", | ||
"react-dom/unmountComponentAtNode", | ||
"react-dom/hydrate", | ||
"react-dom/render", | ||
"react-dom/createPortal", | ||
"react-dom/findDOMNode", | ||
"react-dom/flushSync", | ||
]; |