-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add previous and next link to each doc page
- Loading branch information
1 parent
c4e7eb1
commit 71cddee
Showing
33 changed files
with
286 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script lang="ts"> | ||
import {page} from '$app/stores'; | ||
import type {Page} from '@sveltejs/kit'; | ||
export let href = ''; | ||
export let title: string; | ||
const validMdRegex = /^\d{2}-([a-zA-Z-]*)\.md$/; | ||
const categoryRegex = /\/\d{2}-([a-zA-Z-]*\/)/g; | ||
const mdEndRegex = /\/\d{2}-([a-zA-Z-]*)\.md$/g; | ||
function computedAppliedHref(inputHref: string, page: Page<Record<string, string>>) { | ||
if (inputHref.startsWith('../')) { | ||
return new URL( | ||
inputHref.replace(categoryRegex, (_m, gr1) => '/' + gr1.toLowerCase()).replace(mdEndRegex, (_m, gr1) => '/' + gr1.toLowerCase()), | ||
page.url.href, | ||
).href; | ||
} else { | ||
return inputHref.match(validMdRegex)?.[1]?.toLowerCase() ?? href; | ||
} | ||
} | ||
$: appliedHref = computedAppliedHref(href, $page); | ||
</script> | ||
|
||
<a href={appliedHref} {title}><slot /></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,70 @@ | ||
import {readFile, readdir} from 'node:fs/promises'; | ||
|
||
const validMdRegex = /^\d{2}-[a-z-]*\.md$/g; | ||
const validMdRegex = /^\d{2}-[a-zA-Z-]*\.md$/g; | ||
|
||
export async function listMarkdown() { | ||
const categories = (await readdir(`../docs`)).filter((folder) => folder !== 'code'); | ||
const files: {slug: string; path: string}[] = []; | ||
for (const category of categories) { | ||
(await readdir(`../docs/${category}`)) | ||
.filter((file) => file.match(validMdRegex)) | ||
.forEach((file) => { | ||
files.push({slug: `${category}/${file.slice(3, -3)}`, path: `../docs/${category}/${file}`}); | ||
}); | ||
} | ||
return files; | ||
} | ||
const componentsSubMenu = [ | ||
{title: 'Accordion', slug: 'components/accordion/', subpath: 'examples'}, | ||
{title: 'Alert', slug: `components/alert/`, subpath: 'examples'}, | ||
{title: 'Modal', slug: `components/modal/`, subpath: 'examples'}, | ||
{title: 'Pagination', slug: `components/pagination/`, subpath: 'examples'}, | ||
{title: 'Progressbar', slug: `components/progressbar/`, subpath: 'examples'}, | ||
{title: 'Rating', slug: `components/rating/`, subpath: 'examples'}, | ||
{title: 'Select', slug: `components/select/`, subpath: 'examples'}, | ||
{title: 'Slider', slug: `components/slider/`, subpath: 'examples'}, | ||
]; | ||
|
||
export async function listPages() { | ||
const docFolders = (await readdir(`../docs`)).filter((folder) => folder !== 'code'); | ||
const categories: {name: string; files: {slug: string; mdpath?: string; title: string; subpath: string}[]}[] = []; | ||
|
||
export async function listSections(base: string) { | ||
return (await readdir(`../docs/${base}`)) | ||
.filter((file) => file.match(validMdRegex)) | ||
.map((file) => { | ||
const name = file.slice(3, -3); | ||
return {name, title: name.substring(0, 1).toUpperCase() + name.substring(1).replace('-', ' ')}; | ||
for (const docFolder of docFolders) { | ||
const name = docFolder.substring(3); | ||
categories.push({ | ||
name, | ||
files: | ||
name === 'Components' | ||
? componentsSubMenu | ||
: (await readdir(`../docs/${docFolder}`)) | ||
.filter((file) => file.match(validMdRegex)) | ||
.map((file) => { | ||
const normalizedFileName = file.slice(3, -3); | ||
return { | ||
slug: `${name.toLowerCase()}/${normalizedFileName.toLowerCase()}`, | ||
mdpath: `../docs/${docFolder}/${file}`, | ||
title: normalizedFileName.replace('-', ' '), | ||
name: normalizedFileName, | ||
subpath: '', | ||
}; | ||
}), | ||
}); | ||
} | ||
return categories; | ||
} | ||
|
||
export async function retrieveMarkdown(slug: string) { | ||
const files = await listMarkdown(); | ||
const file = files.find((file) => file.slug === slug); | ||
return file ? await readFile(file.path, 'utf-8') : undefined; | ||
const categories = await listPages(); | ||
let prev; | ||
let next; | ||
let file; | ||
for (let i = 0; i < categories.length; i++) { | ||
const category = categories[i]; | ||
for (let j = 0; j < category.files.length; j++) { | ||
if (category.files[j].slug === slug) { | ||
file = category.files[j]; | ||
if (j > 0) { | ||
prev = category.files[j - 1]; | ||
} else if (i > 0) { | ||
prev = categories[i - 1].files[categories[i - 1].files.length - 1]; | ||
prev = {...prev, title: `${categories[i - 1].name.replace('-', ' ')}: ${prev.title}`}; | ||
} | ||
if (j < category.files.length - 1) { | ||
next = category.files[j + 1]; | ||
} else if (i < categories.length - 1) { | ||
next = categories[i + 1].files[0]; | ||
next = {...next, title: `${categories[i + 1].name.replace('-', ' ')}: ${next.title}`}; | ||
} | ||
} | ||
} | ||
} | ||
return file ? {prev, next, content: await readFile(file.mdpath!, 'utf-8')} : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,15 @@ | ||
import {listSections} from '$lib/server'; | ||
import {listPages} from '$lib/server'; | ||
|
||
export const load = async ({params}) => { | ||
return { | ||
includesFwk: true, | ||
menu: [ | ||
{ | ||
title: 'Getting started', | ||
submenu: (await listSections('getting-started')).map((file) => ({ | ||
label: file.title, | ||
path: `docs/${params.framework}/getting-started/${file.name}`, | ||
subpath: '', | ||
})), | ||
}, | ||
{ | ||
title: 'Components', | ||
submenu: [ | ||
{label: 'Accordion', path: `docs/${params.framework}/components/accordion/`, subpath: 'examples'}, | ||
{label: 'Alert', path: `docs/${params.framework}/components/alert/`, subpath: 'examples'}, | ||
{label: 'Modal', path: `docs/${params.framework}/components/modal/`, subpath: 'examples'}, | ||
{label: 'Pagination', path: `docs/${params.framework}/components/pagination/`, subpath: 'examples'}, | ||
{label: 'Progressbar', path: `docs/${params.framework}/components/progressbar/`, subpath: 'examples'}, | ||
{label: 'Rating', path: `docs/${params.framework}/components/rating/`, subpath: 'examples'}, | ||
{label: 'Select', path: `docs/${params.framework}/components/select/`, subpath: 'examples'}, | ||
{label: 'Slider', path: `docs/${params.framework}/components/slider/`, subpath: 'examples'}, | ||
], | ||
}, | ||
{ | ||
title: 'Services', | ||
submenu: (await listSections('services')).map((file) => ({ | ||
label: file.title, | ||
path: `docs/${params.framework}/services/${file.name}`, | ||
subpath: '', | ||
})), | ||
}, | ||
], | ||
menu: (await listPages()).map((category) => ({ | ||
title: category.name.replace('-', ' '), | ||
submenu: category.files.map((file) => ({ | ||
label: file.title, | ||
path: `docs/${params.framework}/${file.slug}`, | ||
subpath: file.subpath, | ||
})), | ||
})), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script> | ||
import {page} from '$app/stores'; | ||
import {pathToRoot$, selectedFramework$} from '$lib/stores'; | ||
</script> | ||
|
||
<slot /> | ||
|
||
<hr /> | ||
|
||
<div class="w-100 d-flex justify-content-between"> | ||
<div> | ||
{#if $page.data.prev} | ||
<div class="fw-bolder text-start">PREVIOUS</div> | ||
<a class="text-start" href="{$pathToRoot$}docs/{$selectedFramework$}/{$page.data.prev.slug}{$page.data.prev.subpath}">{$page.data.prev.title}</a | ||
> | ||
{/if} | ||
</div> | ||
<div> | ||
{#if $page.data.next} | ||
<div class="fw-bolder text-end">NEXT</div> | ||
<a class="text-end" href="{$pathToRoot$}docs/{$selectedFramework$}/{$page.data.next.slug}{$page.data.next.subpath}">{$page.data.next.title}</a> | ||
{/if} | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.