2022-07-16 11:59:20 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import RenderCodeBlock from '@theme/CodeBlock';
|
|
|
|
|
2023-11-01 16:08:40 +01:00
|
|
|
const CodeBlock = ({ example, file, section, language }) => {
|
|
|
|
const [code, setCode] = useState('');
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let isMounted = true;
|
|
|
|
|
|
|
|
const path =
|
|
|
|
file === 'manifest' ? 'Cargo.toml' : `src/${file ?? 'main.rs'}`;
|
|
|
|
|
|
|
|
import(`!!raw-loader!@site/examples/${example}/${path}`)
|
|
|
|
.then((source) => {
|
|
|
|
source = source.default.match(
|
|
|
|
new RegExp(
|
|
|
|
`(?:\/\/|#) <${section}>\n([\\s\\S]*)(?:\/\/|#) <\/${section}>`
|
|
|
|
)
|
|
|
|
)[1];
|
|
|
|
|
|
|
|
if (isMounted) setCode(source);
|
|
|
|
})
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
isMounted = false;
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RenderCodeBlock className={`language-${language ?? 'rust'}`}>
|
|
|
|
{code}
|
|
|
|
</RenderCodeBlock>
|
|
|
|
);
|
|
|
|
};
|
2022-07-16 11:59:20 +02:00
|
|
|
|
|
|
|
export default CodeBlock;
|