Examples
Basic example
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);
page.content = marked(page.content);
await writeFile(page.output, getHtml(page));
}
View on GitHub
Extenders
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'},
metadata,
author,
(page) => {
page.date = new Date();
},
{
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
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 {renderFeed, writeFeed} from '@sphido/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 items = [];
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.date = new Date();
items.push({title: page.title, url: page.url, date: page.date});
await writeFile(page.output, getHtml(page));
}
const xml = renderFeed({
title: 'RSS example',
link: 'https://sphido.org/',
description: 'This is my personal feed!',
feedUrl: 'https://sphido.org/rss.xml',
}, items);
await writeFeed('public/rss.xml', xml);
View on GitHub
Generate JSON file
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);
await writeFile(output, JSON.stringify(page));
}
View on GitHub
Sitemap
import {dirname, relative, join} from 'node:path';
import {getPages, allPages} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {pagesToSitemap, writeSitemap} from '@sphido/sitemap';
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.url = new URL(page.slug, 'https://sphido.org/').toString();
page.priority = page.name === 'index' ? 1 : 0.8;
}
const xml = pagesToSitemap(pages, {
baseUrl: 'https://sphido.org/',
defaults: {changefreq: 'daily', lastmod: new Date()},
});
await writeSitemap('public/sitemap.xml', xml);
View on GitHub