Skip to content

Commit

Permalink
feat(demo): list and use all available examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Dec 14, 2024
1 parent be93bed commit 71bd46b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
9 changes: 1 addition & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ <h1>Astro Decap Collection Demo</h1>
placeholder="Paste your Decap config.yml here"
></textarea>
</div>
<nav>
<button data-example="./examples/blog.yml" onclick="window.handleClick(event)">
folder example
</button>
<button data-example="./examples/navigation.yml" onclick="window.handleClick(event)">
file example
</button>
</nav>
<nav></nav>
</fieldset>
<fieldset id="config">
<legend>Parsed config collections</legend>
Expand Down
25 changes: 22 additions & 3 deletions src/demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// <reference types="vite/client" />

import hljs from 'highlight.js';
import json from 'highlight.js/lib/languages/json';
import ts from 'highlight.js/lib/languages/typescript';
import yaml from 'highlight.js/lib/languages/yaml';
import zod from 'zod';

import { parseConfig } from '../utils/decap.utils.js';
import { formatCode } from '../utils/format.utils.js';
Expand All @@ -18,6 +19,7 @@ declare global {
handleInput(event: InputEvent): Promise<void>;
handleScroll(event: Event): void;

initExamples(): void;
loadExample(path: string): void;
updateExample(data: string): void;
updateInput(from: InputEvent): string;
Expand All @@ -38,7 +40,10 @@ hljs.registerLanguage('yaml', yaml);
// prevent errors from libs using ancient `global` instead of `globalThis`
window.global ||= window;

window.handleLoad = () => window.initFromUrl();
window.handleLoad = () => {
window.initExamples();
window.initFromUrl();
};

window.handleClick = event => {
event.preventDefault();
Expand All @@ -55,7 +60,7 @@ window.handleInput = async event => {

const schemas = await Promise.all(
collections.map(async collection => {
const { compiled: cptime } = transformCollection(collection, { zod });
const { compiled: cptime } = transformCollection(collection);
return [collection.name, await formatCode(cptime, undefined, { printWidth: 50 })];
}),
);
Expand All @@ -80,6 +85,20 @@ window.updateInput = event => {
return input.value;
};

// lists all examples from the `examples` folder
window.initExamples = async () => {
const examples = Object.keys(import.meta.glob('/public/examples/*.yml'));
document.querySelector('#input > nav')!.innerHTML = examples
.map(path => {
const [, , folder, name] = path.split('/');
return `
<button data-example="${`${folder}/${name}`}" onclick="window.handleClick(event)">
${name.replace(/\.yml$/, '')} example
</button>`;
})
.join('');
};

// loads an example from the `examples` folder
window.loadExample = async (path: string) => {
const example = await fetch(path);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext"
"target": "ESNext",
"useDefineForClassFields": true
},
"ts-node": {
"experimentalSpecifierResolution": "node",
Expand Down

0 comments on commit 71bd46b

Please sign in to comment.