Skip to content

Commit

Permalink
Better init
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed May 18, 2024
1 parent 4145a9b commit 1cd8264
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare global {
// interface Locals {}
interface PageData {
highlighter: import('shiki').Highlighter;
pagefind: import('$docs/types.ts').Pagefind;
}
// interface PageState {}
// interface Platform {}
Expand Down
18 changes: 6 additions & 12 deletions src/docs/components/PageHeader/SearchDialog.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<script lang="ts">
import Dialog from '../Dialog/Dialog.svelte';
import SearchIcon from 'lucide-svelte/icons/search';
import type { Pagefind } from '$docs/types.js';
import { page } from '$app/stores';
let open = $state(false);
let pagefind: Pagefind | null = $state(null);
let query = $state('');
const searchPromise = $derived.by(async () => {
if (pagefind === null || query === '') {
if (query === '') {
return [];
}
const result = await pagefind.search(query);
// FIXME: https://github.com/sveltejs/eslint-plugin-svelte/issues/652
// eslint-disable-next-line svelte/valid-compile
const result = await $page.data.pagefind.search(query);
if (result === null) {
return [];
Expand All @@ -20,14 +22,6 @@
return await Promise.all(result.results.map((result) => result.data()));
});
$effect(() => {
// @ts-expect-error - Pagefind will be present at runtime
import('/pagefind/pagefind.js').then((module: Pagefind) => {
pagefind = module;
pagefind.init();
});
});
$effect(() => {
function onKeydown(event: KeyboardEvent) {
if (event.key === 'k' && (event.ctrlKey || event.metaKey)) {
Expand Down
12 changes: 12 additions & 0 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getHighlighter } from 'shiki';
import MoonlightDark from '$docs/themes/moonlight-dark.json';
import type { Pagefind } from '$docs/types.js';
import { browser } from '$app/environment';

export async function load() {
const highlighter = await getHighlighter({
Expand All @@ -8,6 +10,16 @@ export async function load() {
themes: [MoonlightDark],
});

if (browser) {
// @ts-expect-error - Pagefind will be present at runtime
const pagefind: Pagefind = await import('/pagefind/pagefind.js');
await pagefind.init();
return {
highlighter,
pagefind,
};
}

return {
highlighter,
};
Expand Down

0 comments on commit 1cd8264

Please sign in to comment.