tranform file paths ids 'a-b' into camelCase 'aB'

This commit is contained in:
hsjobeki 2023-09-21 09:27:11 +02:00 committed by Johannes Kirschbauer
parent d48d4fe6df
commit 425ce3573c
5 changed files with 39 additions and 15 deletions

View File

@ -22,21 +22,21 @@ export function NixFunctions(props: FunctionsProps) {
const { search, searchResults, rawResults } = useMiniSearch<DocItem>(data, {
fields: ["id", "name", "category", "description", "example", "fn_type"],
searchOptions: {
// allow 25% levenshtein distance (e.g. 2.5 of 10 characters don't match)
fuzzy: 0.25,
// allow 22% levenshtein distance (e.g. 2.2 of 10 characters don't match)
fuzzy: 0.22,
// prefer to show builtins first
boostDocument: (id, term) => {
let boost = 1;
boost += id.includes("builtins") ? 1 : 0;
boost += id.includes(term) ? 10 : 0;
boost *= id.includes("builtins") ? 10 : 1;
boost *= id.includes(term) ? 10 : 1;
return boost;
},
boost: {
id: 10,
name: 8,
category: 6,
id: 1000,
name: 100,
category: 10,
example: 0.5,
fn_type: 3,
fn_type: 10,
description: 1,
},
},

View File

@ -15,6 +15,7 @@ import { useSnackbar } from "notistack";
import { Marker } from "react-mark.js";
import ManageSearchIcon from "@mui/icons-material/ManageSearch";
import { normalizePath } from "../../models/internals";
interface FunctionItemProps {
selected: boolean;
@ -53,6 +54,8 @@ export default function FunctionItem(props: FunctionItemProps) {
enqueueSnackbar("link copied to clipboard", { variant: "default" });
};
const normalId: string = useMemo(() => normalizePath(id), [id]);
return (
<Paper
elevation={0}
@ -77,7 +80,7 @@ export default function FunctionItem(props: FunctionItemProps) {
<Stack sx={{ width: "100%" }}>
{!selected && (
<>
<ListItemText primary={`${id}`} secondary={category} />
<ListItemText primary={`${normalId}`} secondary={category} />
<ListItemText secondary={descriptionPreview} />
<Typography
sx={{

View File

@ -58,6 +58,7 @@ export const PageContextProvider = (props: PageContextProviderProps) => {
const { children, pageProps } = props;
const [pageState, setPageState] = useState<PageState>(pageProps);
const { term, filter, viewMode } = pageState;
function setPageStateVariable<T>(field: keyof InitialPageState) {
return function (value: React.SetStateAction<T> | T) {
if (typeof value !== "function") {
@ -77,12 +78,14 @@ export const PageContextProvider = (props: PageContextProviderProps) => {
}
};
}
function resetQueries() {
if (Object.entries(router.query).length !== 0) {
router.push({ query: undefined });
}
setPageState((curr) => ({ ...curr, ...initialPageState }));
}
useEffect(() => {
setPageState((c) => ({
...c,

View File

@ -1,4 +1,4 @@
import React from "react";
import React, { useMemo } from "react";
import {
Box,
Container,
@ -19,6 +19,7 @@ import LocalLibraryIcon from "@mui/icons-material/LocalLibrary";
import InputIcon from "@mui/icons-material/Input";
import { MarkdownPreview } from "../markdownPreview";
import { CodeHighlight } from "../codeHighlight";
import { normalizePath } from "../../models/internals";
interface PreviewProps {
docItem: DocItem;
@ -31,6 +32,8 @@ export const Preview = (props: PreviewProps) => {
const { name, description, category, example, fn_type, id, line } = docItem;
const theme = useTheme();
const normalId: string = useMemo(() => normalizePath(id), [id]);
const prefix = category.split(/([\/.])/gm).at(4) || "builtins";
const libName = category
.match(/(?:[a-zA-Z]*)\.nix/gm)?.[0]
@ -58,7 +61,7 @@ export const Preview = (props: PreviewProps) => {
color={"text.primary"}
sx={{ wordWrap: "normal", lineBreak: "anywhere" }}
>
{`${id}`}
{`${normalId}`}
</Typography>
{closeComponent || (
<Tooltip title="close details">

View File

@ -3,9 +3,9 @@ import { MetaData, NixType } from "./nix";
export type ViewMode = "explore" | "browse";
export type ComputedState = {
export type ComputedState = {
FOTD: boolean;
}
};
export type PageState = {
data: MetaData;
@ -26,7 +26,22 @@ export const initialPageState: InitialPageState = {
filter: { from: "any", to: "any" },
page: 1,
itemsPerPage: 10,
viewMode: "explore"
viewMode: "explore",
};
export type Filter = { to: NixType; from: NixType };
export type Filter = { to: NixType; from: NixType };
export const normalizePath = (id: string) => {
if (!id.includes("-")) return id;
return id
.split("-")
.map((substr, idx) => {
if (idx === 0) return substr;
const start = substr.slice(0, 1);
const end = substr.slice(1);
console.log({ start, end });
return start.toUpperCase() + end;
})
.join("");
};