@sphido/core 2.0.17 → 2.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/copy-file.js +4 -7
- package/lib/get-pages.js +23 -23
- package/lib/is-page.js +7 -7
- package/lib/write-file.js +4 -7
- package/package.json +51 -52
- package/readme.md +37 -37
- package/types/is-page.d.ts +1 -1
package/lib/copy-file.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {existsSync} from 'node:fs';
|
|
1
|
+
import {copyFile as copyFileAsync} from 'node:fs/promises';
|
|
3
2
|
import {dirname} from 'node:path';
|
|
3
|
+
import {mkdir} from 'node:fs/promises';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Write content to the file and create directory if not exists
|
|
@@ -10,9 +10,6 @@ import {dirname} from 'node:path';
|
|
|
10
10
|
* @returns {Promise<*>}
|
|
11
11
|
*/
|
|
12
12
|
export async function copyFile(src, dest) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return copyFileSync(src, dest);
|
|
13
|
+
await mkdir(dirname(dest), {recursive: true});
|
|
14
|
+
return copyFileAsync(src, dest);
|
|
18
15
|
}
|
package/lib/get-pages.js
CHANGED
|
@@ -10,29 +10,29 @@ import {isPage} from './is-page.js';
|
|
|
10
10
|
* @return {Promise<Awaited<Pages>[]>}
|
|
11
11
|
*/
|
|
12
12
|
export async function getPages({path = 'content', include = isPage} = {}, ...extenders) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
13
|
+
const dir = await readdir(path, {withFileTypes: true});
|
|
14
|
+
|
|
15
|
+
return Promise.all(
|
|
16
|
+
dir
|
|
17
|
+
.filter(dirent => include(dirent, path))
|
|
18
|
+
.map(async dirent => {
|
|
19
|
+
// Page object
|
|
20
|
+
const page = {name: parse(dirent.name).name, path: join(path, dirent.name)};
|
|
21
|
+
|
|
22
|
+
// Read subdirectory recursively
|
|
23
|
+
if (dirent.isDirectory()) {
|
|
24
|
+
page.children = await getPages({path: page.path, include}, ...extenders);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Calling callbacks in the series
|
|
28
|
+
for (const cb of extenders.filter(f => typeof f === 'function')) {
|
|
29
|
+
/** @type {ExtenderCallback} */
|
|
30
|
+
await cb(page, dirent, path);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Assign objects with page
|
|
34
|
+
return Object.assign(page, ...extenders.filter(o => typeof o === 'object'));
|
|
35
|
+
}));
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
package/lib/is-page.js
CHANGED
|
@@ -3,18 +3,18 @@
|
|
|
3
3
|
*
|
|
4
4
|
* - skip hidden files (starts with .)
|
|
5
5
|
* - skip and directories (starts with .)
|
|
6
|
-
* - skip drafts files with underscore at the beginning
|
|
6
|
+
* - skip drafts files with underscore (_) at the beginning
|
|
7
7
|
* - accept only *.html and *.md files
|
|
8
8
|
*
|
|
9
9
|
* @param {import('node:fs').Dirent} dirent
|
|
10
10
|
* @returns {boolean}
|
|
11
11
|
*/
|
|
12
12
|
export function isPage(dirent) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
// Accept only *.md, *.html files
|
|
14
|
+
if (dirent.isFile() && !dirent.name.startsWith('_') && !dirent.name.startsWith('.')) {
|
|
15
|
+
return dirent.name.endsWith('.md') || dirent.name.endsWith('.html');
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// Or not hidden directory
|
|
19
|
+
return dirent.isDirectory() && !dirent.name.startsWith('.');
|
|
20
20
|
}
|
package/lib/write-file.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {existsSync} from 'node:fs';
|
|
1
|
+
import {writeFile as writeFileAsync} from 'node:fs/promises';
|
|
3
2
|
import {dirname} from 'node:path';
|
|
3
|
+
import {mkdir} from 'node:fs/promises';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Write content to the file and create directory if not exists
|
|
@@ -10,9 +10,6 @@ import {dirname} from 'node:path';
|
|
|
10
10
|
* @returns {Promise<*>}
|
|
11
11
|
*/
|
|
12
12
|
export async function writeFile(file, content) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return writeFileAsync(file, content);
|
|
13
|
+
await mkdir(dirname(file), {recursive: true});
|
|
14
|
+
return writeFileAsync(file, content);
|
|
18
15
|
}
|
package/package.json
CHANGED
|
@@ -1,54 +1,53 @@
|
|
|
1
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
|
-
"gitHead": "6d977aa261aecb80b4221c9af8ff61e8756a9456"
|
|
2
|
+
"name": "@sphido/core",
|
|
3
|
+
"version": "2.0.21",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Roman Ožana",
|
|
6
|
+
"email": "roman@ozana.cz",
|
|
7
|
+
"url": "https://ozana.cz"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"description": "A rocket 🚀 fast, lightweight, static site generator",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": "./lib/index.js",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=16"
|
|
15
|
+
},
|
|
16
|
+
"types": "types/index.d.ts",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"markdown",
|
|
19
|
+
"markup",
|
|
20
|
+
"content management system",
|
|
21
|
+
"cms",
|
|
22
|
+
"static",
|
|
23
|
+
"static site generator",
|
|
24
|
+
"generator",
|
|
25
|
+
"framework",
|
|
26
|
+
"blog",
|
|
27
|
+
"scaffold"
|
|
28
|
+
],
|
|
29
|
+
"homepage": "https://sphido.org",
|
|
30
|
+
"bugs": "https://github.com/sphido/sphido/issues",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git@github.com:sphido/sphido.git"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@sindresorhus/slugify": "^2.2.1",
|
|
37
|
+
"ava": "^5.3.1",
|
|
38
|
+
"marked": "^9.1.3"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"test": "ava",
|
|
42
|
+
"types": "tsc lib/*.js --declaration --allowJs --emitDeclarationOnly --outDir types"
|
|
43
|
+
},
|
|
44
|
+
"xo": {
|
|
45
|
+
"ignores": [
|
|
46
|
+
"types/**"
|
|
47
|
+
],
|
|
48
|
+
"rules": {
|
|
49
|
+
"no-await-in-loop": 0
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"gitHead": "59185f7c4e4708ba554fe8efcdc7398ec0a669f9"
|
|
54
53
|
}
|
package/readme.md
CHANGED
|
@@ -13,23 +13,23 @@ Returned structure is very simple and looks like follow:
|
|
|
13
13
|
|
|
14
14
|
```json
|
|
15
15
|
[
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
16
|
+
{
|
|
17
|
+
"name": "Main page",
|
|
18
|
+
"path": "content/Main page.md"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"name": "Directory",
|
|
22
|
+
"children": [
|
|
23
|
+
{
|
|
24
|
+
"name": "Subpage one",
|
|
25
|
+
"path": "content/Directory/Subpage one.md"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"name": "Subpage two",
|
|
29
|
+
"path": "content/Directory/Subpage two.md"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
33
|
]
|
|
34
34
|
```
|
|
35
35
|
|
|
@@ -37,7 +37,7 @@ Then iterate over pages like follow:
|
|
|
37
37
|
|
|
38
38
|
```javascript
|
|
39
39
|
for (const page of allPages(pages)) {
|
|
40
|
-
|
|
40
|
+
console.log(page);
|
|
41
41
|
}
|
|
42
42
|
```
|
|
43
43
|
|
|
@@ -53,7 +53,7 @@ parameters passed to the function `page`, `path` and [`dirent`](https://nodejs.o
|
|
|
53
53
|
|
|
54
54
|
```javascript
|
|
55
55
|
const callbackExtender = (page, path, dirent) => {
|
|
56
|
-
|
|
56
|
+
// do anything with page object
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
const pages = await getPages({path: 'content'}, callbackExtender);
|
|
@@ -65,7 +65,7 @@ This extender is just a simple JavaScript object that is combined with the `page
|
|
|
65
65
|
|
|
66
66
|
```javascript
|
|
67
67
|
const objectExtender = {
|
|
68
|
-
|
|
68
|
+
author: 'Roman Ožana'
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
const pages = await getPages({path: 'content'}, objectExtender);
|
|
@@ -113,15 +113,15 @@ then you get this structure:
|
|
|
113
113
|
|
|
114
114
|
```json
|
|
115
115
|
[
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
116
|
+
{
|
|
117
|
+
"name": "Main page",
|
|
118
|
+
"path": "content/Main page.md",
|
|
119
|
+
"title": "Main page | my best website",
|
|
120
|
+
"counter": 2,
|
|
121
|
+
"author": "Roman Ožana",
|
|
122
|
+
"getDate": "[Function: getDate]",
|
|
123
|
+
"getLink": "[Function: getLink]"
|
|
124
|
+
}
|
|
125
125
|
]
|
|
126
126
|
```
|
|
127
127
|
|
|
@@ -144,16 +144,16 @@ import slugify from '@sindresorhus/slugify';
|
|
|
144
144
|
import {marked} from 'marked';
|
|
145
145
|
|
|
146
146
|
const pages = await getPages({path: 'content'}, // ... extenders
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
147
|
+
(page) => {
|
|
148
|
+
page.slug = slugify(page.name) + '.html';
|
|
149
|
+
page.dir = dirname(page.path);
|
|
150
|
+
});
|
|
151
151
|
|
|
152
152
|
for (const page of allPages(pages)) {
|
|
153
|
-
|
|
154
|
-
|
|
153
|
+
page.output = join('public', relative('content', page.dir), page.slug);
|
|
154
|
+
page.content = marked(await readFile(page.path));
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
await writeFile(page.output, `<!DOCTYPE html>
|
|
157
157
|
<html lang="en" dir="ltr">
|
|
158
158
|
<head>
|
|
159
159
|
<meta charset="UTF-8">
|
|
@@ -163,7 +163,7 @@ for (const page of allPages(pages)) {
|
|
|
163
163
|
<body class="prose mx-auto my-6">${page.content}</body>
|
|
164
164
|
<!-- Generated by Sphido from ${page.path} -->
|
|
165
165
|
</html>
|
|
166
|
-
|
|
166
|
+
`);
|
|
167
167
|
}
|
|
168
168
|
```
|
|
169
169
|
|
package/types/is-page.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* - skip hidden files (starts with .)
|
|
5
5
|
* - skip and directories (starts with .)
|
|
6
|
-
* - skip drafts files with underscore at the beginning
|
|
6
|
+
* - skip drafts files with underscore (_) at the beginning
|
|
7
7
|
* - accept only *.html and *.md files
|
|
8
8
|
*
|
|
9
9
|
* @param {import('node:fs').Dirent} dirent
|