blog software from scratch with SvelteKit 🖊️
npm i -D @fuzdev/fuz_blog
introduction #
fuz_blog is a SvelteKit blog template and a library for adding blogs to existing projects.
Each post is a SvelteKit route in an <id>-<slug> directory that
exports its own metadata, and gro gen validates them to produce an Atom feed, a
runtime feed module, and a route redirecting each bare post id to its post. Everything is
statically generated -- no database, no admin interface. See the API reference for the full surface.
Install #
Published as @fuzdev/fuz_blog to npm:
npm i -D @fuzdev/fuz_blogSet up a blog #
In a SvelteKit project, run fuz_blog's init task by its path:
gro task node_modules/@fuzdev/fuz_blog/dist/init.task.js It creates whatever is missing -- the src/routes/blogs.ts registry, the local
gen and task files, src/params.ts, the blog's +layout.svelte and +page.svelte, and a root +layout.svelte linking the feeds -- then
runs gro gen. It never overwrites a file: when one exists without what the blog
needs, like an existing root layout without the feed links, it prints the code to add.
Running it again changes nothing. The feed metadata defaults from package.json's homepage, name, description, and author, overridden by flags -- pass --url when homepage is a repo page rather than your site:
$ gro task node_modules/@fuzdev/fuz_blog/dist/init.task.js --dirname notes --url https://www.example.com
$ gro task node_modules/@fuzdev/fuz_blog/dist/init.task.js --help The sections below describe the pieces it wires up.
Declare your blogs #
Declare your blogs in src/routes/blogs.ts — one entry per blog, each with a
route dirname and feed metadata:
// src/routes/blogs.ts
import type {BlogConfig} from '@fuzdev/fuz_blog/blog_config.ts';
export const blogs: Array<BlogConfig> = [
{
dirname: 'blog',
feed: {
title: 'my blog',
home_page_url: 'https://www.example.com/blog',
// ...the rest of the feed metadata
},
},
]; Set home_page_url to the blog's index page: its path must end with the dirname, after any deploy base path, and gro gen fails otherwise.
Post URLs derive from it, and so does the feed's own URL, <home_page_url>/feed.xml (see to_blog_feed_url).
See BlogConfig for the rest of the options, like customizing what gro task src/lib/post.task.ts scaffolds.
Post directories #
Each post lives at src/routes/<dirname>/<id>-<slug>/+page.svelte, like src/routes/blog/1-hello-world/+page.svelte, so its URL is its directory, /blog/1-hello-world, and the route is yours to extend with a +page.ts or colocated assets. The id is a positive integer and the slug is
lowercase letters and digits joined by single hyphens. Ids may have gaps but not repeats.
For a blog with posts, gro gen also writes src/routes/<dirname>/[post=blog_post_id]/+page.ts, which redirects /blog/1 to /blog/1-hello-world. Each feed entry's id is that id
URL, stable across slug changes, so after renaming a post's directory its id URL still
resolves but its old slug URL does not.
Register the route's blog_post_id param matcher in src/params.ts,
adding it to your defineParams call if you already have one, so the route only
matches bare ids:
// src/params.ts
import {defineParams} from '@sveltejs/kit/params';
import {blog_post_id_param} from '@fuzdev/fuz_blog/blog_post_id.ts';
export const params = defineParams({blog_post_id: blog_post_id_param});Render a post #
A post's +page.svelte exports its metadata as post from its <script module>, checked against BlogPostMetadata, and renders whatever it likes. Wrapping the
content in BlogPost adds the title, dates, and canonical link from the post's
entry in the blog's feed context (see below); without a matching entry it renders an error
in place of the content:
<!-- src/routes/blog/1-hello-world/+page.svelte -->
<script lang="ts" module>
import type {BlogPostMetadata} from '@fuzdev/fuz_blog/blog.ts';
export const post = {
title: 'Hello world',
date_published: '2024-07-10T16:04:49.688Z',
date_modified: '2024-07-10T16:04:49.688Z',
summary: 'the first post',
tags: ['meta'],
} satisfies BlogPostMetadata;
</script>
<script lang="ts">
import BlogPost from '@fuzdev/fuz_blog/BlogPost.svelte';
</script>
<BlogPost {post}>
<p>the content</p>
</BlogPost> BlogPost's optional meta and comments snippets
receive the post's feed entry and its post, so one snippet shared by many posts
can render the fields you add beyond BlogPostMetadata, like an
autoblog's model. BlogPost is generic over the post: its type is
inferred from the post you pass, so with satisfies keeping the
extra fields, an inline snippet sees post.model as a string. A
shared snippet types its post with your own post type, and each post passing it
must have those fields:
<!-- src/routes/autoblog/PostSnippets.svelte -->
<script lang="ts" module>
import type {BlogFeedItem, BlogPostMetadata} from '@fuzdev/fuz_blog/blog.ts';
export type AutoblogPostMetadata = BlogPostMetadata & {model: string};
export {provenance};
</script>
{#snippet provenance(item: BlogFeedItem, post: AutoblogPostMetadata)}
<p>post {item.blog_post_id}, written by {post.model}</p>
{/snippet}
<!-- in each autoblog post -->
<script lang="ts" module>
import type {AutoblogPostMetadata} from '../PostSnippets.svelte';
export const post = {
// ...the BlogPostMetadata fields
model: 'some-model',
} satisfies AutoblogPostMetadata;
</script>
<script lang="ts">
import {provenance} from '../PostSnippets.svelte';
</script>
<BlogPost {post} meta={provenance}>...</BlogPost> BlogPostList renders a blog's index, newest first, from its generated feed:
<!-- src/routes/blog/+page.svelte -->
<script lang="ts">
import BlogPostList from '@fuzdev/fuz_blog/BlogPostList.svelte';
import {feed} from './feed.ts';
</script>
<BlogPostList {feed} />Publish the feed #
Set each blog's generated feed as the feed context in its own layout, so BlogPostList and BlogPost in that subtree read the right blog:
<!-- src/routes/blog/+layout.svelte -->
<script lang="ts">
import {blog_feed_context} from '@fuzdev/fuz_blog/blog_feed_context.ts';
import {feed} from './feed.ts';
const {children} = $props();
blog_feed_context.set(feed);
</script>
{@render children()} To enable your generated feed.xml, include it as a link in your
root layout so feed readers discover it from any page:
<!-- src/routes/+layout.svelte -->
<script lang="ts">
import {to_blog_feed_url} from '@fuzdev/fuz_blog/blog.ts';
import {blogs} from './blogs.ts';
</script>
<svelte:head>
<title>(your title here)</title>
{#each blogs as blog (blog.dirname)}
<link
rel="alternate"
type="application/atom+xml"
title={blog.feed.title}
href={to_blog_feed_url(blog.feed.home_page_url)}
/>
{/each}
</svelte:head>Create a new post #
Gro runs a project task by its path, so init creates a one-line local task file
that re-exports fuz_blog's, and the same for update_post.task.ts and blog.gen.ts:
// src/lib/post.task.ts
export * from '@fuzdev/fuz_blog/post.task.ts'; $ gro task src/lib/post.task.ts "Some post title"
$ gro task src/lib/post.task.ts "Some post title" --slug custom-slug
$ gro task src/lib/post.task.ts --helpUpdate an existing post #
Name the post by its id, its directory name, or its slug, which must match only one post:
$ gro task src/lib/update_post.task.ts 1
$ gro task src/lib/update_post.task.ts 1-hello-world
$ gro task src/lib/update_post.task.ts hello-world
$ gro task src/lib/update_post.task.ts --helpapi #
Browse the full api docs.