Skip to content

Commit

Permalink
chore: remove some stuff better search
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Oct 6, 2024
1 parent a59f683 commit efd0327
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 108 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@astrojs/mdx": "^3.1.7",
"@astrojs/sitemap": "^3.2.0",
"@astrojs/tailwind": "^5.1.1",
"@builder.io/qwik": "1.7.3",
"@builder.io/qwik": "1.7.2",
"@fontsource-variable/fira-code": "^5.1.0",
"@fontsource-variable/fredoka": "^5.1.0",
"@fontsource-variable/outfit": "^5.1.0",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/Doc/JSDocDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const JSDocDescription = component$(({ data }: Props) => {
if (!jsDoc) return null;

return (
<p class="prose">
<p class="prose" data-pagefind-weight="4">
{doc.map(async (e: any) => (
<>
{e.kind === "JSDocText" && <>{e.text}</>}
Expand Down
33 changes: 12 additions & 21 deletions src/components/Sidebar/Sidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import VersionSelector from "./VersionSelector.astro";
type LinkListEntry = {
title: string;
description: string;
link: string;
};
Expand Down Expand Up @@ -182,49 +183,37 @@ else if (sidebarMode === "books") {
</div>
<div class="divider"></div>
<ul class="flex-col gap-2">
<SidebarLink href="/" lang={lang} icon="home">{
t(lang, "sidebar.home")
}</SidebarLink>
<SidebarLink href="/" icon="home">Home</SidebarLink>
<SidebarLink
href="https://kaboomjs.com/discord"
target="_blank"
lang={lang}
noTranslate
icon="discord"
>
Discord
</SidebarLink>
<SidebarLink
href="https://github.com/kaplayjs/kaplay"
target="_blank"
lang={lang}
noTranslate
icon="github"
>
GitHub
</SidebarLink>
<SidebarLink
href="/changelog"
lang={lang}
noTranslate
icon="pog"
>
What's new on v4000
</SidebarLink>
<SidebarLink
href="https://play.kaplayjs.com"
target="_blank"
lang={lang}
noTranslate
icon="controller"
>
{t(lang, "sidebar.playground")}
KA-PLAYGROUND
</SidebarLink>
<SidebarLink
href="https://opencollective.com/kaplay"
target="_blank"
lang={lang}
noTranslate
icon="donate"
>
Donate to KAPLAY
Expand All @@ -234,19 +223,21 @@ else if (sidebarMode === "books") {
<ul class="flex-col gap-2">
<SidebarLink
href="/guides/install"
lang={lang}
icon="guides"
reloadAll
>{t(lang, "sidebar.guides")}</SidebarLink>
>
Guides
</SidebarLink>
<SidebarLink
href="/doc/kaplay"
lang={lang}
icon="api"
reloadAll
>{t(lang, "sidebar.refdoc")}</SidebarLink>
<SidebarLink href="/blog" lang={lang} icon="blog" reloadAll>{
t(lang, "sidebar.blog")
}</SidebarLink>
>
API Docs
</SidebarLink>
<SidebarLink href="/blog" icon="blog" reloadAll>
Blog
</SidebarLink>
</ul>
<div class="divider"></div>
<SidebarList
Expand Down
12 changes: 1 addition & 11 deletions src/components/Sidebar/SidebarLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { component$, Slot } from "@builder.io/qwik";
type SidebarLinkProps = {
href: string;
target?: string;
lang?: Locale;
noTranslate?: boolean;
reloadAll?: boolean;
icon?: keyof typeof icons;
};
Expand All @@ -32,19 +30,11 @@ const icons = {
"api": apiIcon,
};

const dataReload = {
"data-astro-reload": undefined,
};

export const SidebarLink = component$((props: SidebarLinkProps) => {
const lang = props.lang || "en";

return (
<li class="sidebar-link list-none">
<a
href={props.noTranslate
? props.href
: getLangedRoute(lang, props.href)}
href={props.href}
class="sidebar-link-a btn btn-sm w-full text-wrap h-auto justify-start text-left text-lg aria-[current=page]:btn-primary aria-[current=false]:btn-ghost"
target={props.target}
data-link={props.href}
Expand Down
109 changes: 44 additions & 65 deletions src/components/Sidebar/SidebarList.tsx
Original file line number Diff line number Diff line change
@@ -1,94 +1,73 @@
import { $lang } from "@/stores";

import {
component$,
useComputed$,
useSignal,
useVisibleTask$,
} from "@builder.io/qwik";
import { component$, useSignal, useTask$ } from "@builder.io/qwik";
import type { SidebarEntry, SidebarProps } from "./Sidebar.astro";
import { SidebarFolder } from "./SidebarFolder";
import { SidebarLink } from "./SidebarLink";

type SidebarListProps = {
type Props = {
sidebarEntries: SidebarEntry[];
} & SidebarProps;

export const SidebarList = component$(
({ sidebarMode, sidebarEntries }: SidebarListProps) => {
let renderList = useSignal<SidebarEntry[]>(sidebarEntries ?? []);
let filter = useSignal("");
let filteredList = useComputed$(() => {
return renderList.value.filter(({ linkList }) =>
linkList.some(
({ title }) =>
(title ?? "").toLowerCase().includes(
filter.value.toLowerCase(),
),
)
);
});
export const SidebarList = component$((props: Props) => {
let sidebarEntries = useSignal<SidebarEntry[]>(props.sidebarEntries ?? []);
let filter = useSignal("");

const searchInputRef = useSignal<HTMLInputElement>();

const searchInputRef = useSignal<HTMLInputElement>();
const allOpen = sidebarMode === "guides";
useTask$(({ track }) => {
track(filter);

useVisibleTask$(() => {
if (!searchInputRef.value) return;
const defaultValue = new URLSearchParams(location.search).get(
"search",
) ?? "";
if (filter.value === "") return;

searchInputRef.value.defaultValue = defaultValue;
filter.value = defaultValue;
const newSidebarEntries = props.sidebarEntries.map((entry) => {
const linkList = entry.linkList.filter((l) =>
(`${l.title ?? ""} ${l.description ?? ""}`).toLowerCase()
.includes(filter.value.toLowerCase())
);

return {
...entry,
linkList,
};
});

return (
<>
<input
class="input input-primary w-full my-2"
placeholder={sidebarMode === "reference"
? "Search for API..."
: "Search for Guides..."}
bind:value={filter}
ref={searchInputRef}
onKeyPress$={(e) => {
if (e.key === "Enter") {
const search = searchInputRef.value?.value;
const url = new URL(location.href);
history.pushState({}, "", url.toString());
sidebarEntries.value = newSidebarEntries;

if (search) filter.value = search;
}
}}
/>
console.log(
newSidebarEntries.map((e) => e.linkList.map((l) => l.title)).flat(),
);
});

{filteredList.value.map(({ linkList, folder }) => {
const filteredList = linkList.filter((
{ title },
) => (title ?? "").toLowerCase().includes(
filter.value.toLowerCase(),
));
return (
<>
<input
class="input input-primary w-full my-2"
placeholder={props.sidebarMode === "reference"
? "Search for API..."
: "Search for Guides..."}
bind:value={filter}
ref={searchInputRef}
/>

{sidebarEntries.value.filter(({ linkList }) => linkList.length > 0)
.map(({ linkList, folder }, i) => {
return (
<SidebarFolder
title={folder}
id={folder}
isOpen={filteredList.length > 0 || allOpen}
isOpen={linkList.length > 0}
key={folder + i}
>
{filteredList.map(({ title, link }) => (
{linkList.map(({ title, link }, i) => (
<SidebarLink
href={link}
lang={$lang.get()}
noTranslate={sidebarMode === "reference"}
key={link}
key={link + i}
>
{title}
</SidebarLink>
))}
</SidebarFolder>
);
})}
</>
);
},
);
</>
);
});
24 changes: 24 additions & 0 deletions src/components/Sidebar/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const getGuidesEntries = async () => {
linkList: guidesByCategory[category].map((guide) => ({
title: guide.data.title,
link: `/guides/${guide.data.url ?? guide.id.split("/")[2]}`,
description: guide.data.description,
})),
})),
];
Expand All @@ -77,6 +78,7 @@ export const getBlogEntries = async () => {
linkList: sortedEntries.map((entry: any) => ({
title: entry.data.title,
link: `/blog/${entry.slug}`,
description: "",
})),
},
];
Expand Down Expand Up @@ -114,13 +116,34 @@ export const getBookEntries = async () => {
link: `/books/${book.slug.split("/")[1]}/${
book.slug.split("/")[2]
}`,
description: "",
})),
})),
];

return renderList;
};

const getDocEntryDescription = (item: string) => {
const docObjs = allDoc.KaboomCtx?.[0].members[item]
?? allDoc.KAPLAYCtx?.[0].members[item] ?? allDoc[item];
const docObj = docObjs[0];
const docDesc = getDocDescription(docObj?.jsDoc?.doc);

return docDesc;
};

const getDocDescription = (item: unknown) => {
if (typeof item === "string") {
return item;
}
else if (Array.isArray(item)) {
return item.map((part: any) => part.text).join("");
}

return "";
};

export const getDocEntries = async () => {
const renderList: SidebarEntry[] = [];
const ctxMembers = Object.keys(
Expand All @@ -140,6 +163,7 @@ export const getDocEntries = async () => {
link: isCtxMember
? `/doc/ctx/${item}`
: `/doc/${item}`,
description: `${getDocEntryDescription(item)}`,
};
}),
});
Expand Down
2 changes: 1 addition & 1 deletion src/pages/[...path]/doc/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getStaticPathsByLocales } from "@/util/path";
export async function getStaticPaths() {
const allDoc: any = doc.types;
const allDocKeys: string[] = Object.keys(doc.types).filter(
(k) => k !== "KAPLAYCtx" && k !== "KaboomCtx",
(k) => k !== "KAPLAYCtx" && k !== "KaboomCtx" && k !== "undefined",
);
return getStaticPathsByLocales().flatMap((locale) => {
Expand Down

0 comments on commit efd0327

Please sign in to comment.