Skip to content

Commit

Permalink
Add GitHub source code button to API reference
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Dec 28, 2023
1 parent acd0be1 commit 7b5c73a
Show file tree
Hide file tree
Showing 172 changed files with 276 additions and 4 deletions.
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build.preview": "vite build --ssr src/entry.preview.tsx",
"build.server": "vite build -c adapters/netlify-edge/vite.config.ts",
"build.types": "tsc --incremental --noEmit",
"sources": "tsm ./scripts/sources.ts",
"contributors": "tsm ./scripts/contributors.ts",
"sitemap": "tsm ./scripts/sitemap.ts",
"deploy": "netlify deploy --build",
Expand Down
87 changes: 87 additions & 0 deletions website/scripts/sources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import fs from 'node:fs';
import path from 'node:path';
import graymatter from 'gray-matter';

/**
* Finds all index files in the given directory.
*
* @param directory The directory to search in.
*/
function findIndexFiles(directory: string) {
// Create file paths list
const filePaths: string[] = [];

// Get items of directory
const items = fs.readdirSync(directory);

for (const itemName of items) {
// If item is a index file, add it to list
if (itemName === 'index.mdx') {
filePaths.push(path.join(directory, itemName));

// Otherwise, search for nested index files
} else {
const itemPath = path.join(directory, itemName);
const itemStat = fs.statSync(itemPath);
if (itemStat.isDirectory()) {
filePaths.push(...findIndexFiles(itemPath));
}
}
}

// Return file paths list
return filePaths;
}

/**
* Updates the source file paths of the API reference.
*/
async function updateSources() {
// Find all MDX files of API reference
const filePaths = findIndexFiles(path.join('src', 'routes', 'api'));

// Update source file paths of MDX files
for (const filePath of filePaths) {
if (!filePath.includes('(types)')) {
// Log info to console
console.log('Update:', filePath);

// Get group and name of source code file
const pathList = filePath.split('/');
let group = pathList.slice(3, 4)[0].replace('(', '').replace(')', '');
const name = pathList.slice(4, 5)[0];

// If group is async, find non-async group
if (group === 'async') {
const nonAsync = filePaths.find(
(filePath) =>
!filePath.includes('(async)') &&
filePath.includes(name.replace('Async', ''))
);
group = nonAsync!
.split('/')
.slice(3, 4)[0]
.replace('(', '')
.replace(')', '');
}

// Create path to source code file
const source = `/${group}/${name.replace('Async', '')}/${name}.ts`;

// Read frontmatter of MDX file
const frontmatter = graymatter.read(filePath);

// Add source code file path to frontmatter
frontmatter.data.source = source;

// Write changes to MDX file
fs.writeFileSync(
filePath,
graymatter.stringify(frontmatter.content, frontmatter.data)
);
}
}
}

// Start update process
updateSources();
23 changes: 19 additions & 4 deletions website/src/components/DocsLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrowLeftIcon, ArrowRightIcon, PenIcon } from '~/icons';
import { ArrowLeftIcon, ArrowRightIcon, GitHubIcon, PenIcon } from '~/icons';
import { IconButton } from './IconButton';
import { Navigation } from './Navigation';
import { SideBar } from './SideBar';
Expand Down Expand Up @@ -62,6 +62,7 @@ export const DocsLayout = component$(() => {
<div q:slot="buttons" class="mr-4 flex space-x-6 lg:hidden">
<NavButtons
pageIndex={navIndex.value}
sourcePath={documentHead.frontmatter.source}
prevPage={prevPage.value}
nextPage={nextPage.value}
/>
Expand All @@ -76,6 +77,7 @@ export const DocsLayout = component$(() => {
<div class="hidden px-8 lg:absolute lg:right-0 lg:flex lg:space-x-6 lg:px-10">
<NavButtons
pageIndex={navIndex.value}
sourcePath={documentHead.frontmatter.source}
prevPage={prevPage.value}
nextPage={nextPage.value}
/>
Expand Down Expand Up @@ -158,15 +160,16 @@ export const DocsLayout = component$(() => {

type NavButtonsProps = {
pageIndex: number;
prevPage?: ContentMenu;
nextPage?: ContentMenu;
sourcePath: string | undefined;
prevPage: ContentMenu | undefined;
nextPage: ContentMenu | undefined;
};

/**
* Buttons to navigate to the previous or next page.
*/
export const NavButtons = component$<NavButtonsProps>(
({ pageIndex, prevPage, nextPage }) => (
({ pageIndex, sourcePath, prevPage, nextPage }) => (
<>
{pageIndex !== -1 && (
<>
Expand Down Expand Up @@ -196,6 +199,18 @@ export const NavButtons = component$<NavButtonsProps>(
) : (
<div class="w-10" />
)}
{sourcePath && (
<IconButton
variant="secondary"
type="link"
href={`https://github.com/fabian-hiller/valibot/blob/main/library/src${sourcePath}`}
target="_blank"
label="Source code"
hideLabel
>
<GitHubIcon class="h-[18px]" />
</IconButton>
)}
</>
)}
</>
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/anyAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: anyAsync
source: /schemas/any/anyAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/arrayAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: arrayAsync
source: /schemas/array/arrayAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/bigintAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: bigintAsync
source: /schemas/bigint/bigintAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/blobAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: blobAsync
source: /schemas/blob/blobAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/booleanAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: booleanAsync
source: /schemas/boolean/booleanAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/coerceAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: coerceAsync
source: /methods/coerce/coerceAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/customAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: customAsync
source: /validations/custom/customAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/dateAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: dateAsync
source: /schemas/date/dateAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/enumAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: enumAsync
source: /schemas/enum/enumAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/fallbackAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: fallbackAsync
source: /methods/fallback/fallbackAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/forwardAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: forwardAsync
source: /methods/forward/forwardAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/getDefaultAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: getDefaultAsync
source: /methods/getDefault/getDefaultAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/getDefaultsAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: getDefaultsAsync
source: /methods/getDefaults/getDefaultsAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/getFallbackAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: getFallbackAsync
source: /methods/getFallback/getFallbackAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/getFallbacksAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: getFallbacksAsync
source: /methods/getFallbacks/getFallbacksAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/instanceAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: instanceAsync
source: /schemas/instance/instanceAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/intersectAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: intersectAsync
source: /schemas/intersect/intersectAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/literalAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: literalAsync
source: /schemas/literal/literalAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/mapAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: mapAsync
source: /schemas/map/mapAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/mergeAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: mergeAsync
source: /methods/merge/mergeAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nanAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nanAsync
source: /schemas/nan/nanAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/neverAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: neverAsync
source: /schemas/never/neverAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nonNullableAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nonNullableAsync
source: /schemas/nonNullable/nonNullableAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nonNullishAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nonNullishAsync
source: /schemas/nonNullish/nonNullishAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nonOptionalAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nonOptionalAsync
source: /schemas/nonOptional/nonOptionalAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nullAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nullAsync
source: /schemas/null/nullAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nullableAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nullableAsync
source: /schemas/nullable/nullableAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/nullishAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: nullishAsync
source: /schemas/nullish/nullishAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/numberAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: numberAsync
source: /schemas/number/numberAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/objectAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: objectAsync
source: /schemas/object/objectAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/omitAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: omitAsync
source: /methods/omit/omitAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/optionalAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: optionalAsync
source: /schemas/optional/optionalAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/parseAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: parseAsync
source: /methods/parse/parseAsync.ts
contributors:
- fabian-hiller
---
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(async)/partialAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: partialAsync
source: /methods/partial/partialAsync.ts
contributors:
- fabian-hiller
---
Expand Down
Loading

0 comments on commit 7b5c73a

Please sign in to comment.