2022-11-26 12:36:08 +03:00
|
|
|
import {
|
|
|
|
Box,
|
2022-12-09 18:41:59 +03:00
|
|
|
Container,
|
2022-11-26 12:36:08 +03:00
|
|
|
Link,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemIcon,
|
|
|
|
ListItemSecondaryAction,
|
|
|
|
ListItemText,
|
|
|
|
Typography,
|
|
|
|
} from "@mui/material";
|
2022-12-03 17:25:32 +03:00
|
|
|
import Highlight from "react-highlight";
|
2022-11-26 12:36:08 +03:00
|
|
|
import LocalLibraryIcon from "@mui/icons-material/LocalLibrary";
|
|
|
|
import InputIcon from "@mui/icons-material/Input";
|
2022-12-03 17:25:32 +03:00
|
|
|
import { DocItem } from "../../types/nix";
|
|
|
|
import CodeIcon from "@mui/icons-material/Code";
|
2022-12-09 18:41:59 +03:00
|
|
|
import ReactMarkdown from "react-markdown";
|
2022-12-03 18:16:26 +03:00
|
|
|
|
|
|
|
// import hljs from "highlight.js";
|
|
|
|
|
|
|
|
// needed for nextjs to import the classes of github theme
|
|
|
|
import "highlight.js/styles/github.css";
|
2022-11-26 12:36:08 +03:00
|
|
|
interface PreviewProps {
|
2022-12-03 17:25:32 +03:00
|
|
|
docItem: DocItem;
|
2022-11-26 12:36:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Preview = (props: PreviewProps) => {
|
2022-12-03 17:25:32 +03:00
|
|
|
const { docItem } = props;
|
|
|
|
const { name, description, category, example, fn_type } = docItem;
|
|
|
|
|
2022-12-09 18:41:59 +03:00
|
|
|
const getGeneratedExamples = () => {
|
|
|
|
if (description) {
|
|
|
|
// const regex = /\`\`\`.*\`\`\`/gm;
|
|
|
|
const regex = /(```.+?```)/gms;
|
|
|
|
console.log({ description, regex });
|
|
|
|
if (typeof description === "object") {
|
|
|
|
return description
|
|
|
|
.join(" ")
|
|
|
|
.match(regex)
|
|
|
|
?.join("")
|
|
|
|
?.replace("```nix", "")
|
|
|
|
?.replace("```", "");
|
|
|
|
} else {
|
|
|
|
return description
|
|
|
|
.match(regex)
|
|
|
|
?.join("")
|
|
|
|
?.replace("```nix", "")
|
|
|
|
?.replace("```", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const finalExample = example || getGeneratedExamples();
|
|
|
|
console.log({ finalExample });
|
|
|
|
const prefix = category.split(/([\/.])/gm).at(4) || "builtins";
|
2022-12-03 18:16:26 +03:00
|
|
|
const libName = category
|
|
|
|
.match(/(?:[a-zA-Z]*)\.nix/gm)?.[0]
|
|
|
|
?.replace(".nix", "");
|
|
|
|
const docsRef = `https://nixos.org/manual/nixpkgs/stable/#function-library-lib.${libName}.${name}`;
|
2022-11-26 12:36:08 +03:00
|
|
|
return (
|
2022-12-03 19:38:50 +03:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
p: 1,
|
|
|
|
pt: 2,
|
|
|
|
mt: 2,
|
|
|
|
mb: -2,
|
|
|
|
width: "100%",
|
|
|
|
borderTop: "solid 1px",
|
|
|
|
borderTopColor: "primary.main",
|
|
|
|
}}
|
|
|
|
>
|
2022-12-03 17:25:32 +03:00
|
|
|
<Typography variant="h2">{`${prefix}.${name}`}</Typography>
|
2022-11-26 12:36:08 +03:00
|
|
|
<List sx={{ width: "100%" }}>
|
|
|
|
<ListItem>
|
|
|
|
<ListItemIcon>
|
|
|
|
<LocalLibraryIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
2022-12-03 17:25:32 +03:00
|
|
|
sx={{
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
|
|
|
}}
|
2022-12-09 18:41:59 +03:00
|
|
|
primaryTypographyProps={{
|
|
|
|
color: "text.secondary",
|
|
|
|
fontSize: 14,
|
|
|
|
}}
|
|
|
|
secondaryTypographyProps={{
|
|
|
|
color: "text.primary",
|
|
|
|
fontSize: "1rem",
|
|
|
|
}}
|
|
|
|
primary={"nixpkgs/" + category.replace("./", "")}
|
|
|
|
secondary={
|
|
|
|
<Container
|
|
|
|
sx={{ ml: "0 !important", pl: "0 !important" }}
|
|
|
|
maxWidth="lg"
|
|
|
|
>
|
|
|
|
{typeof description === "object"
|
|
|
|
? description.map((d, idx) => (
|
|
|
|
<ReactMarkdown key={idx}>{d}</ReactMarkdown>
|
|
|
|
))
|
|
|
|
: description && <ReactMarkdown>{description}</ReactMarkdown>}
|
|
|
|
</Container>
|
|
|
|
}
|
2022-11-26 12:36:08 +03:00
|
|
|
/>
|
2022-12-09 18:41:59 +03:00
|
|
|
{/* <ListItemSecondaryAction>
|
2022-12-03 18:16:26 +03:00
|
|
|
<Link href={docsRef}>View Docs</Link>
|
2022-12-09 18:41:59 +03:00
|
|
|
</ListItemSecondaryAction> */}
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItem>
|
|
|
|
<ListItem>
|
|
|
|
<ListItemIcon>
|
|
|
|
<InputIcon />
|
|
|
|
</ListItemIcon>
|
2022-12-03 17:25:32 +03:00
|
|
|
<ListItemText
|
|
|
|
sx={{
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
|
|
|
}}
|
2022-12-09 18:41:59 +03:00
|
|
|
primaryTypographyProps={{
|
|
|
|
color: "text.secondary",
|
|
|
|
fontSize: 14,
|
|
|
|
}}
|
|
|
|
secondaryTypographyProps={{
|
|
|
|
color: "text.primary",
|
|
|
|
fontSize: "1rem",
|
|
|
|
}}
|
|
|
|
secondary={fn_type || "no type yet provided"}
|
|
|
|
primary="function signature "
|
2022-12-03 17:25:32 +03:00
|
|
|
/>
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItem>
|
2022-12-03 17:25:32 +03:00
|
|
|
<ListItem
|
|
|
|
sx={{
|
|
|
|
backgroundColor: "background.paper",
|
|
|
|
}}
|
|
|
|
>
|
2022-11-26 12:36:08 +03:00
|
|
|
<ListItemIcon>
|
2022-12-03 17:25:32 +03:00
|
|
|
<CodeIcon />
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItemIcon>
|
2022-12-03 17:25:32 +03:00
|
|
|
<ListItemText
|
|
|
|
sx={{
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
|
|
|
}}
|
|
|
|
disableTypography
|
|
|
|
primary={
|
2022-12-09 18:41:59 +03:00
|
|
|
finalExample && (
|
2022-12-08 01:10:58 +03:00
|
|
|
<Typography sx={{ color: "text.secondary" }}>
|
|
|
|
Example
|
|
|
|
</Typography>
|
|
|
|
)
|
2022-12-03 17:25:32 +03:00
|
|
|
}
|
|
|
|
secondary={
|
2022-12-09 18:41:59 +03:00
|
|
|
finalExample ? (
|
2022-12-08 01:10:58 +03:00
|
|
|
<Box sx={{ mt: -2, pl: 1.5 }}>
|
2022-12-09 18:41:59 +03:00
|
|
|
<Highlight className="nix">{finalExample}</Highlight>
|
2022-12-08 01:10:58 +03:00
|
|
|
</Box>
|
|
|
|
) : (
|
|
|
|
<Typography
|
|
|
|
sx={{ color: "text.secondary" }}
|
|
|
|
>{`no example yet provided`}</Typography>
|
|
|
|
)
|
2022-12-03 17:25:32 +03:00
|
|
|
}
|
|
|
|
/>
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItem>
|
|
|
|
</List>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|