Skip to content

Commit

Permalink
docs: add programmatic example
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jan 9, 2023
1 parent 00fb23d commit 3b6d2cc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/programmatic/README.md
@@ -0,0 +1,5 @@
# Programmatic

This example shows programmatic usage of Akte. This is helpful for running Akte in various environments, including serverless.

Peek inside `programmatic.ts` to see some example usage of Akte API.
71 changes: 71 additions & 0 deletions examples/programmatic/akte.app.ts
@@ -0,0 +1,71 @@
import { defineAkteApp, defineAkteFile, defineAkteFiles } from "akte";

type GlobalData = { siteDescription: string };

// Unique file
const index = defineAkteFile<GlobalData>().from({
path: "/",
data() {
// We assume those are sourced one way or another
const posts = {
"/posts/foo": "foo",
"/posts/bar": "bar",
"/posts/baz": "bar",
};

return { posts };
},
render(context) {
const posts = Object.entries(context.data.posts).map(
([href, title]) => /* html */ `<li><a href="${href}">${title}</a></li>`,
);

return /* html */ `<main>
<h1>basic typescript</h1>
<p>${context.globalData.siteDescription}</p>
<ul>
${posts.join("\n")}
</ul>
</main>
`;
},
});

// Multiple files
const posts = defineAkteFiles<GlobalData>().from({
path: "/posts/:slug",
bulkData() {
// We assume those are sourced one way or another
const posts = {
"/posts/foo": {
title: "foo",
body: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod, dignissimos enim qui cupiditate provident cumque distinctio id reiciendis quia consectetur fugiat dolorem mollitia laborum libero natus et, vero voluptatibus dolorum?",
},
"/posts/bar": {
title: "bar",
body: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod, dignissimos enim qui cupiditate provident cumque distinctio id reiciendis quia consectetur fugiat dolorem mollitia laborum libero natus et, vero voluptatibus dolorum?",
},
"/posts/baz": {
title: "baz",
body: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod, dignissimos enim qui cupiditate provident cumque distinctio id reiciendis quia consectetur fugiat dolorem mollitia laborum libero natus et, vero voluptatibus dolorum?",
},
};

return posts;
},
render(context) {
return /* html */ `<main>
<h1>${context.data.title}</h1>
<p>${context.data.body}</p>
</main>`;
},
});

export const app = defineAkteApp({
files: [index, posts],
globalData: () => {
return {
siteDescription: "A really simple website",
};
},
});
9 changes: 9 additions & 0 deletions examples/programmatic/package.json
@@ -0,0 +1,9 @@
{
"name": "akte.examples.programmatic",
"version": "0.0.0",
"private": true,
"type": "module",
"devDependencies": {
"akte": "^0.0.2"
}
}
17 changes: 17 additions & 0 deletions examples/programmatic/programmatic.ts
@@ -0,0 +1,17 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { app } from "./akte.app";

// Renders all files and returns them.
const files = await app.renderAll();

// Renders all files and returns them.
await app.writeAll({ files, outDir: "my-out-dir" });

// Renders and writes all files to the config output directory.
await app.buildAll();

// Looks up the Akte file responsible for rendering the given path.
const match = app.lookup("/foo");

// Renders a match from `app.lookup()`
const file = await app.render(match);

0 comments on commit 3b6d2cc

Please sign in to comment.