2022-11-26 12:36:08 +03:00
|
|
|
import {
|
|
|
|
Box,
|
2022-12-09 18:41:59 +03:00
|
|
|
Container,
|
2022-12-10 15:26:11 +03:00
|
|
|
IconButton,
|
2022-11-26 12:36:08 +03:00
|
|
|
Link,
|
|
|
|
List,
|
|
|
|
ListItem,
|
2022-12-10 15:26:11 +03:00
|
|
|
ListItemButton,
|
2022-11-26 12:36:08 +03:00
|
|
|
ListItemIcon,
|
|
|
|
ListItemSecondaryAction,
|
|
|
|
ListItemText,
|
2022-12-10 15:26:11 +03:00
|
|
|
Tooltip,
|
2022-11-26 12:36:08 +03:00
|
|
|
Typography,
|
|
|
|
} from "@mui/material";
|
2022-12-10 15:26:11 +03:00
|
|
|
// import CloseIcon from "@mui/icons-material/Close";
|
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-10 15:26:11 +03:00
|
|
|
import ExpandLessIcon from "@mui/icons-material/ExpandLess";
|
2022-12-11 23:22:45 +03:00
|
|
|
import styles from "./preview.module.css";
|
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-12-10 15:26:11 +03:00
|
|
|
handleClose: () => void;
|
2022-11-26 12:36:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Preview = (props: PreviewProps) => {
|
2022-12-10 15:26:11 +03:00
|
|
|
const { docItem, handleClose } = props;
|
2022-12-03 17:25:32 +03:00
|
|
|
const { name, description, category, example, fn_type } = docItem;
|
|
|
|
|
2022-12-09 18:41:59 +03:00
|
|
|
const getGeneratedExamples = () => {
|
|
|
|
if (description) {
|
|
|
|
const regex = /(```.+?```)/gms;
|
|
|
|
console.log({ description, regex });
|
|
|
|
if (typeof description === "object") {
|
|
|
|
return description
|
|
|
|
.join(" ")
|
|
|
|
.match(regex)
|
2022-12-10 15:26:11 +03:00
|
|
|
?.join("\n#---\n")
|
|
|
|
?.replaceAll("```nix", "")
|
|
|
|
?.replaceAll("```", "");
|
2022-12-09 18:41:59 +03:00
|
|
|
} else {
|
|
|
|
return description
|
|
|
|
.match(regex)
|
2022-12-10 15:26:11 +03:00
|
|
|
?.join("\n#---\n")
|
|
|
|
?.replaceAll("```nix", "")
|
|
|
|
?.replaceAll("```", "");
|
2022-12-09 18:41:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
width: "100%",
|
2022-12-11 23:22:45 +03:00
|
|
|
overflow: "none",
|
2022-12-03 19:38:50 +03:00
|
|
|
}}
|
|
|
|
>
|
2022-12-11 23:22:45 +03:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: { md: "flex", xs: "flex" },
|
|
|
|
flexDirection: { md: "row", xs: "column-reverse" },
|
|
|
|
width: "100%",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Typography
|
|
|
|
variant="h4"
|
|
|
|
sx={{ wordWrap: "normal", lineBreak: "anywhere" }}
|
|
|
|
>{`${prefix}.${name}`}</Typography>
|
|
|
|
<Tooltip title="close details">
|
|
|
|
<IconButton
|
|
|
|
sx={{
|
|
|
|
mx: { xs: "auto", md: 1 },
|
|
|
|
left: { lg: "calc(50% - 2rem)", xs: "unset" },
|
|
|
|
position: { lg: "absolute", xs: "relative" },
|
|
|
|
}}
|
|
|
|
size="small"
|
|
|
|
onClick={() => handleClose()}
|
|
|
|
>
|
|
|
|
<ExpandLessIcon fontSize="large" />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
</Box>
|
2022-11-26 12:36:08 +03:00
|
|
|
<List sx={{ width: "100%" }}>
|
2022-12-11 23:22:45 +03:00
|
|
|
<ListItem sx={{ flexDirection: { xs: "column", sm: "row" } }}>
|
2022-11-26 12:36:08 +03:00
|
|
|
<ListItemIcon>
|
2022-12-11 23:22:45 +03:00
|
|
|
<LocalLibraryIcon sx={{ m: "auto" }} />
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
2022-12-03 17:25:32 +03:00
|
|
|
sx={{
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
2022-12-11 23:22:45 +03:00
|
|
|
alignSelf: "flex-start",
|
2022-12-03 17:25:32 +03:00
|
|
|
}}
|
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
|
2022-12-11 23:22:45 +03:00
|
|
|
component={"div"}
|
2022-12-09 18:41:59 +03:00
|
|
|
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
|
|
|
/>
|
|
|
|
</ListItem>
|
2022-12-11 23:22:45 +03:00
|
|
|
<ListItem sx={{ flexDirection: { xs: "column", sm: "row" } }}>
|
2022-11-26 12:36:08 +03:00
|
|
|
<ListItemIcon>
|
2022-12-11 23:22:45 +03:00
|
|
|
<InputIcon sx={{ m: "auto" }} />
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItemIcon>
|
2022-12-03 17:25:32 +03:00
|
|
|
<ListItemText
|
|
|
|
sx={{
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
2022-12-11 23:22:45 +03:00
|
|
|
alignSelf: "flex-start",
|
2022-12-03 17:25:32 +03:00
|
|
|
}}
|
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-12-11 23:22:45 +03:00
|
|
|
flexDirection: { xs: "column", sm: "row" },
|
2022-12-03 17:25:32 +03:00
|
|
|
}}
|
|
|
|
>
|
2022-11-26 12:36:08 +03:00
|
|
|
<ListItemIcon>
|
2022-12-11 23:22:45 +03:00
|
|
|
<CodeIcon sx={{ m: "auto" }} />
|
2022-11-26 12:36:08 +03:00
|
|
|
</ListItemIcon>
|
2022-12-03 17:25:32 +03:00
|
|
|
<ListItemText
|
|
|
|
sx={{
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
2022-12-11 23:22:45 +03:00
|
|
|
alignSelf: "flex-start",
|
2022-12-03 17:25:32 +03:00
|
|
|
}}
|
|
|
|
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-11 23:22:45 +03:00
|
|
|
<Box sx={{ mt: -2 }}>
|
|
|
|
<Highlight className={`nix ${styles.hljs}`}>
|
|
|
|
{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>
|
|
|
|
);
|
|
|
|
};
|