@chronocide/spider
v5.4.0
Published
Tiny static site generator
Readme
spider is a tiny TypeScript static site generator (SSR) meant for small personal websites. It uses a modular plugin system for maximum flexibility.
Features
- No dependencies
- No templating language, uses plain JS/TS
- Modular loaders, allowing any file type to be used
- Flexible API, every page has full access to the whole website allowing for the creation of RSS feeds, collection pages, etc.
- Sensible defaults
- Markdown file URL's are generated based on folder structure and blog post title (
/<folder>/<title>) - Creation and update dates are truncated to days
- Output files are HTML
- Markdown file URL's are generated based on folder structure and blog post title (
Installation
npm i @chronocide/spiderUsage
spider
Builds static site
import Spider from '@chronocide/spark';
const spider = new Spider({
files: ['src/**/*.ts', 'src/**/*.md'],
root: 'src',
dirout: 'build',
exclude: ['**/*.spec.ts']
});
spider.build();import type { Template, Page } from '@chronocide/spider';
import h from '@chronocide/spark';
const template: Template = registry =>
document => {
const template = h('html')({ lang: 'en-GB' })(
h('head')()(h('title')()()),
h('body')()(document.body(registry))
);
return `<!DOCTYPE html>${template}`;
};
const page: Page = {
title: 'Home',
url: '/',
template,
body: registry => h('main')()(
h('p')()('This is a page'),
h('a')({ href: registry.node('/about')?.url })(registry.node('/about')?.title)
)
};
export default page;SpiderOptions
export type PageOptions = {
title: string;
description: string | null;
url: string;
ext: string | null;
created: Date;
updated: Date | null;
template: Template | null;
body: Body | null;
};export type Loader = (root: string) => (file: string) => Promise<PageOptions>;type SpiderOptions = {
files: string[];
exclude?: string[];
root?: string;
dirout?: string;
loader?: Record<string, Loader>;
}files, entry files. Supports Node's glob pattern.exclude, entry file filter. Supports Node's glob pattern.root, base directory relative tofiles.dirout, output directory. If empty, does not write files.loader, file loaders.
Loaders
Loaders are used to load different file types. By default, spider supports loading .js, .ts and .md files. Loaders can be created or overwritten.
import type { Loader } from '@chronocide/spider';
import Spider from '@chronocide/spider';
const loader: Loader = async context => ({
title: 'loader',
description: null,
url: '/',
ext: '.html',
created: new Date(),
updated: null,
template: registry => document => document.body(registry),
body: registry => `<a href="${registry.node('/').title}">Home</a>`
});
const spider = new Spider({
files: ['src/**/*.ts', 'src/**/*.md'],
root: 'src',
dirout: 'build',
exclude: ['**/*.spec.ts'],
loader: {
'.md': loader,
'.ts': loader
}
});
spider.build();