Examples

Basic example

#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {frontmatter} from '@sphido/frontmatter';

function getHtml({title, content, path, date}) {
	return `<!DOCTYPE html>
<html lang="cs" dir="ltr">
<head>
	<meta charset="UTF-8">
	<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
	<title>${title} | Sphido Example page</title>	
</head>
<body class="prose mx-auto my-6">
	<header class="border-b text-2xl py-2 mb-4">Sphido Example website</header>
	<main>${content}</main>
	<footer class="border-t text-sm text-gray-500 pt-1 mt-1 text-right">Created: ${date?.toLocaleDateString()}</footer>
</body>
<!-- Generated with Sphido from ${path} -->
</html>`;
}

const pages = await getPages({path: 'content'}, frontmatter);

for (const page of allPages(pages)) {
	page.slug = slugify(page.name) + '.html';
	page.output = join('public', relative('content', dirname(page.path)), page.slug);

	// process markdown (PS: frontmatter already load page.content)
	page.content = marked(page.content);

	// save HTML file
	await writeFile(page.output, getHtml(page));
}
View on GitHub

Extenders

#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile, readFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';


function metadata(page, dirent) {
	if (dirent.isFile()) {
		page.slug = slugify(page.name) + '.html';
		page.dir = join('public', relative('content', dirname(page.path)));
	}
}

function author(page) {
	page.author = 'Roman Ožana';
}

async function getContent() {
	return marked(await readFile(this.path));
}

const pages = await getPages({path: 'content'},

	// callback extender
	metadata,
	author,
	(page) => {
		page.date = new Date();
	},

	// Object extenders
	{
		getContent,
		getFooter: function () {
			return `<footer class="border-t text-sm text-gray-500 pt-1 mt-1 text-right">Generated from file ${this.path}</footer>`;
		},

		getTitle: function () {
			return this.name + ' | Sphido example website';
		},

		getHtml: async function () {
			return `<!DOCTYPE html>
<html lang="cs" dir="ltr">
<head>
	<meta charset="UTF-8">
	<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
	<title>${this.getTitle()}</title>	
</head>
<body class="prose mx-auto my-6">
	<header class="border-b text-2xl py-2 mb-4">Sphido Example website</header>
	<main>${await this.getContent()}</main> 
	${this.getFooter()}	
</body>
</html>`;
		},

		save: async function () {
			await writeFile(join(this.dir, this.slug), await this.getHtml());
		},
	},
);

for (const page of allPages(pages)) {
	await page.save();
}
View on GitHub

RSS

#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile, readFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {Feed} from 'feed';

function getHtml({title, content, path, date}) {
	return `<!DOCTYPE html>
<html lang="cs" dir="ltr">
<head>
	<meta charset="UTF-8">
	<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
	<title>${title} | Sphido Example page</title>	
</head>
<body class="prose mx-auto my-6"><main>${content}</main></body>
<!-- Generated with Sphido from ${path} -->
</html>`;
}

const feed = new Feed({
	title: 'RSS example',
	description: 'This is my personal feed!',
	id: 'https://sphido.org/rss.xml',
	author: {
		name: 'Roman Ožana',
		email: 'roman@ozana.cz',
		link: 'https://ozana.cz',
	},
});


const pages = await getPages({path: 'content'});

for (const page of allPages(pages)) {
	page.slug = join('/', relative('content', dirname(page.path)), slugify(page.name) + '.html');
	page.output = join('public', page.slug);

	page.url = new URL(page.slug, 'https://sphido.org/').toString();
	page.content = marked(await readFile(page.path));
	page.title = page.content.match(/(?<=<h[12][^>]*?>)([^<>]+?)(?=<\/h[12]>)/i)?.pop();
	page.id = page.url;
	page.link = page.url;
	page.description = '';

	feed.addItem(page);

	// save HTML file
	await writeFile(page.output, getHtml(page));
}

await writeFile('public/rss.xml', feed.rss2());
View on GitHub

Generate JSON file

#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {frontmatter} from '@sphido/frontmatter';

const pages = await getPages({path: 'content'}, frontmatter);

for (const page of allPages(pages)) {
	page.slug = slugify(page.name) + '.json';
	const output = join('public', relative('content', dirname(page.path)), page.slug);
	page.content = marked(page.content);

	// save JSON file
	await writeFile(output, JSON.stringify(page));
}
View on GitHub

Sitemap

#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {createSitemap} from '@sphido/sitemap';

const pages = await getPages({path: 'content'});
const map = await createSitemap('public/sitemap.xml');

for (const page of await allPages(pages)) {
	page.slug = join('/', relative('content', dirname(page.path)), slugify(page.name) + '.html');
	page.url = new URL(page.slug, 'https://sphido.org/').toString();
	page.changefreq = 'daily';
	page.date = new Date();
	page.priority = page.name === 'index' ? 1 : 0.8;

	// you should save pages here...

	map.add(page);
}

map.end();
View on GitHub