mirror of
https://github.com/actix/actix-website
synced 2025-02-02 04:09:06 +01:00
ad4aeac34f
* Update file extensions and exports for TypeScript compatibility * docs: fix typo in getting-started.md * chore: add no-trailing-punctuation rule to VS Code settings * feat: add @docusaurus/theme-mermaid for mermaid diagram support * Update import paths for MermaidDiagram component * remove redudndant check, use effect only runs after the component is mounted * Update docusaurus.config.ts to fix syntax error * bring back check because it's not possible to properly cancel a dynamic import * feat: optimize dynamic import in CodeBlock component * chore: update VS Code extensions.json with eslint recommendation * Update docusaurus.config.ts to add GitHub repository link in header
45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
import React, { useState, useEffect } from "react";
|
|
import RenderCodeBlock from "@theme/CodeBlock";
|
|
import { useIsMounted } from "usehooks-ts";
|
|
|
|
type Props = {
|
|
example: string;
|
|
file?: string;
|
|
section: string;
|
|
language?: string;
|
|
};
|
|
|
|
const CodeBlock = ({ example, file, section, language }: Props) => {
|
|
const isMounted = useIsMounted();
|
|
const [code, setCode] = useState("");
|
|
|
|
useEffect(() => {
|
|
const path =
|
|
file === "manifest" ? "Cargo.toml" : `src/${file ?? "main.rs"}`;
|
|
|
|
import(`!!raw-loader!@site/examples/${example}/${path}`)
|
|
.then((source) => {
|
|
if (!isMounted()) {
|
|
return;
|
|
}
|
|
|
|
source = source.default.match(
|
|
new RegExp(
|
|
`(?:\/\/|#) <${section}>\n([\\s\\S]*)(?:\/\/|#) <\/${section}>`
|
|
)
|
|
)[1];
|
|
|
|
setCode(source);
|
|
})
|
|
.catch((err) => console.log(err));
|
|
}, [isMounted]);
|
|
|
|
return (
|
|
<RenderCodeBlock className={`language-${language ?? "rust"}`}>
|
|
{code}
|
|
</RenderCodeBlock>
|
|
);
|
|
};
|
|
|
|
export default CodeBlock;
|