-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·101 lines (85 loc) · 3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
import {dirname, relative, join} from 'node:path';
import {getPages, allPages, readFile, writeFile, copyFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {globby} from 'globby';
import {createSitemap} from '@sphido/sitemap';
import {cp} from 'node:fs/promises';
import got from 'got';
import {getPageHtml} from './src/get-html-page.js';
async function content(page, dirent) {
if (dirent.isFile()) {
page.content = await readFile(page.path);
page.title = page.content.match(/(?<=(^#)\s).*/gm)?.pop();
page.name = page.name || page.title;
}
}
function slug(page, dirent, path) {
if (dirent.isFile()) {
// Packages
if (path.startsWith('node_modules')) {
page.name = relative('node_modules', path);
page.slug = join('/', slugify(page.name) + '.html');
page.output = join('public', page.slug);
}
// Content
if (path.startsWith('content')) {
page.slug = join('/', relative('content', dirname(page.path)), slugify(page.name) + '.html');
page.output = join('public', page.slug);
}
// read file content
page.url = new URL(page.slug, 'https://sphido.cz/').toString();
}
}
const pages = [
{
slug: 'index.html',
content: await got('https://raw.githubusercontent.com/sphido/sphido/main/readme.md').text(),
title: 'Home',
name: 'A rocket 🚀 fast, 💭 light-weight and flexible static site 🤖 generator',
url: 'https://sphido.cz/',
output: 'public/index.html',
},
{
slug: 'core.html',
content: await got('https://raw.githubusercontent.com/sphido/sphido/main/packages/sphido-core/readme.md').text(),
title: 'Sphido core',
url: 'https://sphido.cz/core.html',
output: 'public/core.html',
},
...await getPages({path: 'content'}, slug, content),
{
slug: 'frontmatter.html',
content: await got('https://raw.githubusercontent.com/sphido/sphido/main/packages/sphido-frontmatter/readme.md').text(),
title: 'Frontmatter',
url: 'https://sphido.cz/frontmatter.html',
output: 'public/frontmatter.html',
},
{
slug: 'hashtags.html',
content: await got('https://raw.githubusercontent.com/sphido/sphido/main/packages/sphido-hashtags/readme.md').text(),
title: 'Hashtags',
url: 'https://sphido.cz/hashtags.html',
output: 'public/hashtags.html',
},
{
slug: 'sitemap.html',
content: await got('https://raw.githubusercontent.com/sphido/sphido/main/packages/sphido-sitemap/readme.md').text(),
title: 'sitemap.xml',
url: 'https://sphido.cz/sitemap.html',
output: 'public/sitemap.html',
},
];
const sitemap = await createSitemap();
// Generate HTML pages
for (const page of allPages(pages)) {
await writeFile(page.output, await getPageHtml(page, pages));
sitemap.add(page);
}
sitemap.end();
// Copy static files
const files = await globby(['content/**/*.*', '!**/*.{md,html}']);
for await (const file of files) {
await copyFile(file, join('public', relative('content', file)));
}
await cp('static', 'public', {recursive: true});