Extract wiki page layout

This commit is contained in:
Maciej Jur 2023-06-07 22:32:23 +02:00
parent dd70c2bc38
commit ad9efaccd7
8 changed files with 76 additions and 42 deletions

View file

@ -1,11 +1,11 @@
---
import { Tree, pathify } from "@utils/tree";
import type { Maybe } from "purify-ts";
import { PageTree, pathify } from "@utils/tree";
import { Maybe } from "purify-ts";
interface Props {
tree: Tree;
slug: string;
tree: PageTree;
slug: Maybe<string>;
prefix: Maybe<string>;
}
@ -17,6 +17,12 @@ function compare(a: {title: string}, b: {title: string}) {
sensitivity: "base",
});
}
function checkCurrent(checked: Maybe<string>) {
return Maybe
.sequence([checked, slug])
.mapOrDefault(([a, b]) => a == pathify(b), false)
}
---
{tree.children
@ -26,7 +32,7 @@ function compare(a: {title: string}, b: {title: string}) {
<ul class="link-tree__nav-list">
{pages
.sort(compare)
.map(page => ({...page, current: page.slug.mapOrDefault(other => other === pathify(slug), false)}))
.map(page => ({...page, current: checkCurrent(page.slug) }))
.map(page =>
<li class="link-tree__nav-list-item">
{page.slug

View file

@ -1,13 +1,13 @@
---
import List from "./List.astro";
import { Tree, pathify } from "@utils/tree";
import { PageTree, pathify } from "@utils/tree";
import type { Maybe } from "purify-ts";
interface Props {
heading: string;
tree: Tree;
slug: string;
tree: PageTree;
slug: Maybe<string>;
prefix: Maybe<string>;
}

48
src/layouts/Wiki.astro Normal file
View file

@ -0,0 +1,48 @@
---
import Base from "@layouts/Base.astro";
import Tree from "@components/tree/Tree.astro";
import { getCollection } from "astro:content";
import { Maybe, MaybeAsync } from "purify-ts";
import { collapse, type PageTree } from "@utils/tree";
interface Props {
frontmatter: {
title: string;
};
slug?: string;
tree?: PageTree;
}
const {
frontmatter,
slug,
tree,
} = Astro.props;
function constructTree(tree?: PageTree) {
return MaybeAsync
.liftMaybe(Maybe.fromNullable(tree))
.alt(
MaybeAsync(async () => await getCollection('wiki'))
.map(collapse));
}
const pageTree = (await constructTree(tree))
.orDefaultLazy(() => { throw new Error("Couldn't load page tree") });
---
<Base>
<main class="wiki-main">
<Tree heading="Personal Wiki"
tree={pageTree}
slug={Maybe.fromNullable(slug)}
prefix={Maybe.of("/wiki/")}
/>
<article class="wiki-article markdown">
<h1>{frontmatter.title}</h1>
<slot />
</article>
</main>
</Base>

View file

@ -1,10 +1,8 @@
---
import Tree from '@components/tree/Tree.astro';
import Base from '@layouts/Base.astro';
import type { InferGetStaticPropsType } from 'astro';
import { getCollection } from 'astro:content';
import { Maybe } from 'purify-ts';
import { collapse } from '@utils/tree';
import Wiki from '@layouts/Wiki.astro';
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
@ -19,12 +17,6 @@ const { tree, entry } = Astro.props;
const { Content } = await entry.render();
---
<Base>
<main class="wiki-main">
<Tree heading="Personal Wiki" tree={tree} slug={entry.slug} prefix={Maybe.of("/wiki/")}/>
<article class="wiki-article markdown">
<h1>{entry.data.title}</h1>
<Content />
</article>
</main>
</Base>
<Wiki slug={entry.slug} frontmatter={entry.data} tree={tree}>
<Content />
</Wiki>

View file

@ -1,18 +0,0 @@
---
import Base from "@layouts/Base.astro";
import Tree from "@components/tree/Tree.astro";
import { collapse } from "@utils/tree";
import { getCollection } from "astro:content";
import { Maybe } from "purify-ts";
const tree = collapse(await getCollection('wiki'));
---
<Base>
<main class="wiki-main">
<Tree heading="Personal Wiki" tree={tree} slug="wiki" prefix={Maybe.of("wiki")} />
<article class="wiki-article">
siema
</article>
</main>
</Base>

6
src/pages/wiki/index.md Normal file
View file

@ -0,0 +1,6 @@
---
layout: ../../layouts/Wiki.astro
title: Wiki home page
---
Hello

View file

View file

@ -6,14 +6,14 @@ interface Page {
data: { title: string };
}
export interface Tree {
export interface PageTree {
title: string;
slug: Maybe<string>;
children: Maybe<{ [key: string]: Tree }>;
children: Maybe<{ [key: string]: PageTree }>;
}
export function collapse(pages: Page[]): Tree {
const root: Tree = { title: '', slug: Maybe.empty(), children: Maybe.empty() };
export function collapse(pages: Page[]): PageTree {
const root: PageTree = { title: '', slug: Maybe.empty(), children: Maybe.empty() };
for (const page of pages) {
const ptr = page.slug.split('/')