@sphido/core 2.0.30 → 3.0.0
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/dist/all-pages.d.ts +6 -0
- package/dist/all-pages.d.ts.map +1 -0
- package/dist/all-pages.js +13 -0
- package/dist/copy-file.d.ts +5 -0
- package/dist/copy-file.d.ts.map +1 -0
- package/dist/copy-file.js +9 -0
- package/dist/get-pages.d.ts +6 -0
- package/dist/get-pages.d.ts.map +1 -0
- package/dist/get-pages.js +28 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/is-page.d.ts +11 -0
- package/dist/is-page.d.ts.map +1 -0
- package/dist/is-page.js +16 -0
- package/dist/read-file.d.ts +5 -0
- package/dist/read-file.d.ts.map +1 -0
- package/dist/read-file.js +7 -0
- package/dist/write-file.d.ts +5 -0
- package/dist/write-file.d.ts.map +1 -0
- package/dist/write-file.js +9 -0
- package/package.json +16 -20
- package/readme.md +63 -59
- package/lib/all-pages.js +0 -15
- package/lib/copy-file.js +0 -15
- package/lib/get-pages.js +0 -56
- package/lib/index.js +0 -5
- package/lib/is-page.js +0 -20
- package/lib/read-file.js +0 -10
- package/lib/write-file.js +0 -15
- package/types/all-pages.d.ts +0 -7
- package/types/copy-file.d.ts +0 -8
- package/types/get-pages.d.ts +0 -21
- package/types/is-page.d.ts +0 -12
- package/types/read-file.d.ts +0 -6
- package/types/write-file.d.ts +0 -8
- package/{types/index.d.ts → dist/index.js} +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-pages.d.ts","sourceRoot":"","sources":["../lib/all-pages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAE9C;;GAEG;AACH,wBAAiB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,CAQvD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copy-file.d.ts","sourceRoot":"","sources":["../lib/copy-file.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,wBAAsB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGvE"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { copyFile as copyFileAsync, mkdir } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Write content to the file and create directory if not exists
|
|
5
|
+
*/
|
|
6
|
+
export async function copyFile(src, dest) {
|
|
7
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
8
|
+
return copyFileAsync(src, dest);
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-pages.d.ts","sourceRoot":"","sources":["../lib/get-pages.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAoB,SAAS,EAAE,OAAO,EAAQ,KAAK,EAAE,MAAM,YAAY,CAAC;AAGpF;;GAEG;AACH,wBAAsB,QAAQ,CAC7B,EAAE,IAAgB,EAAE,OAAgB,EAAE,GAAE,OAAY,EACpD,GAAG,SAAS,EAAE,SAAS,GACrB,OAAO,CAAC,KAAK,CAAC,CA2BhB"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises";
|
|
2
|
+
import { join, parse } from "node:path";
|
|
3
|
+
import { isPage } from "./is-page.js";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieve an array tree of pages from path
|
|
6
|
+
*/
|
|
7
|
+
export async function getPages({ path = "content", include = isPage } = {}, ...extenders) {
|
|
8
|
+
const dir = await readdir(path, { withFileTypes: true });
|
|
9
|
+
return Promise.all(dir
|
|
10
|
+
.filter((dirent) => include(dirent, path.toString()))
|
|
11
|
+
.map(async (dirent) => {
|
|
12
|
+
// Page object
|
|
13
|
+
const page = {
|
|
14
|
+
name: parse(dirent.name).name,
|
|
15
|
+
path: join(path.toString(), dirent.name),
|
|
16
|
+
};
|
|
17
|
+
// Read subdirectory recursively
|
|
18
|
+
if (dirent.isDirectory()) {
|
|
19
|
+
page.children = await getPages({ path: page.path, include }, ...extenders);
|
|
20
|
+
}
|
|
21
|
+
// Calling callbacks in the series
|
|
22
|
+
for (const cb of extenders.filter((f) => typeof f === "function")) {
|
|
23
|
+
await cb(page, dirent, path.toString());
|
|
24
|
+
}
|
|
25
|
+
// Assign objects with page
|
|
26
|
+
return Object.assign(page, ...extenders.filter((o) => typeof o === "object"));
|
|
27
|
+
}));
|
|
28
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Dirent, PathLike } from "node:fs";
|
|
2
|
+
export { allPages } from "./all-pages.js";
|
|
3
|
+
export { copyFile } from "./copy-file.js";
|
|
4
|
+
export { getPages } from "./get-pages.js";
|
|
5
|
+
export { readFile } from "./read-file.js";
|
|
6
|
+
export { writeFile } from "./write-file.js";
|
|
7
|
+
export type Extenders = Array<ExtenderCallback>;
|
|
8
|
+
export type Page = {
|
|
9
|
+
name: string;
|
|
10
|
+
path: string;
|
|
11
|
+
content?: string;
|
|
12
|
+
children?: Pages;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
};
|
|
15
|
+
export type Pages = Array<Page>;
|
|
16
|
+
export type Options = {
|
|
17
|
+
path?: PathLike;
|
|
18
|
+
include?: IncludePage;
|
|
19
|
+
};
|
|
20
|
+
export type ExtenderCallback = (page: Page, dirent: Dirent, path?: string) => Promise<void>;
|
|
21
|
+
export type IncludePage = (dirent: Dirent, path?: string) => void;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC,gBAAgB,CAAC,CAAC;AAEhD,MAAM,MAAM,IAAI,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,CAAC;IAEjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;AAEhC,MAAM,MAAM,OAAO,GAAG;IAAE,IAAI,CAAC,EAAE,QAAQ,CAAC;IAAC,OAAO,CAAC,EAAE,WAAW,CAAA;CAAE,CAAC;AAEjE,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAE5F,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Dirent } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* Default page filter
|
|
4
|
+
*
|
|
5
|
+
* - skip hidden files (starts with .)
|
|
6
|
+
* - skip hidden directories (starts with .)
|
|
7
|
+
* - skip drafts files with underscore (_) at the beginning
|
|
8
|
+
* - accept only *.html and *.md files
|
|
9
|
+
*/
|
|
10
|
+
export declare function isPage(dirent: Dirent): boolean;
|
|
11
|
+
//# sourceMappingURL=is-page.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-page.d.ts","sourceRoot":"","sources":["../lib/is-page.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAQ9C"}
|
package/dist/is-page.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default page filter
|
|
3
|
+
*
|
|
4
|
+
* - skip hidden files (starts with .)
|
|
5
|
+
* - skip hidden directories (starts with .)
|
|
6
|
+
* - skip drafts files with underscore (_) at the beginning
|
|
7
|
+
* - accept only *.html and *.md files
|
|
8
|
+
*/
|
|
9
|
+
export function isPage(dirent) {
|
|
10
|
+
// Accept only *.md, *.html files
|
|
11
|
+
if (dirent.isFile() && !dirent.name.startsWith("_") && !dirent.name.startsWith(".")) {
|
|
12
|
+
return dirent.name.endsWith(".md") || dirent.name.endsWith(".html");
|
|
13
|
+
}
|
|
14
|
+
// Or not hidden directory
|
|
15
|
+
return dirent.isDirectory() && !dirent.name.startsWith(".");
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-file.d.ts","sourceRoot":"","sources":["../lib/read-file.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAE5D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-file.d.ts","sourceRoot":"","sources":["../lib/write-file.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG5E"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { mkdir, writeFile as writeFileAsync } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Write content to the file and create directory if not exists
|
|
5
|
+
*/
|
|
6
|
+
export async function writeFile(file, content) {
|
|
7
|
+
await mkdir(dirname(file), { recursive: true });
|
|
8
|
+
return writeFileAsync(file, content);
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphido/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"author": "Roman Ožana <roman@ozana.cz> (https://ozana.cz)",
|
|
5
5
|
"homepage": "https://sphido.cz",
|
|
6
6
|
"repository": "sphido/sphido",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"description": "A rocket 🚀 fast, lightweight, static site generator",
|
|
9
9
|
"type": "module",
|
|
10
|
-
"exports": "./lib/index.js",
|
|
11
10
|
"engines": {
|
|
12
|
-
"node": ">=
|
|
11
|
+
"node": ">=22"
|
|
13
12
|
},
|
|
14
|
-
"
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": "./dist/index.js",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"main": "dist/index.js",
|
|
15
19
|
"keywords": [
|
|
16
20
|
"markdown",
|
|
17
21
|
"markup",
|
|
@@ -26,21 +30,13 @@
|
|
|
26
30
|
],
|
|
27
31
|
"bugs": "https://github.com/sphido/sphido/issues",
|
|
28
32
|
"devDependencies": {
|
|
29
|
-
"@sindresorhus/slugify": "^
|
|
30
|
-
"
|
|
31
|
-
"
|
|
33
|
+
"@sindresorhus/slugify": "^3.0.0",
|
|
34
|
+
"marked": "^16.4.2",
|
|
35
|
+
"typescript": "^5.9.3",
|
|
36
|
+
"vitest": "^4.0.17"
|
|
32
37
|
},
|
|
33
38
|
"scripts": {
|
|
34
|
-
"test": "
|
|
35
|
-
"
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
"ignores": [
|
|
39
|
-
"types/**"
|
|
40
|
-
],
|
|
41
|
-
"rules": {
|
|
42
|
-
"no-await-in-loop": 0
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
"gitHead": "7868b45b174ec545fd47df09e57a41df9786dfe5"
|
|
46
|
-
}
|
|
39
|
+
"test": "vitest run --passWithNoTests",
|
|
40
|
+
"build": "rm -rf dist/* && tsc"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/readme.md
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
1
|
# @sphido/core
|
|
2
2
|
|
|
3
|
-
Sphido core package contains two most important
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
that
|
|
3
|
+
Sphido core package contains the two most important functions `getPages()` and `allPages()`. The `getPages()` function scans
|
|
4
|
+
directories for all `*.md` and `*.html` files. The second function, `allPages()`,
|
|
5
|
+
is a [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator)
|
|
6
|
+
that allows you to iterate over all pages.
|
|
7
7
|
|
|
8
8
|
```javascript
|
|
9
|
-
const pages = await getPages({path: 'content'}, /* ...
|
|
9
|
+
const pages = await getPages({path: 'content'}, /* ...extenders */);
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
The returned structure is very simple and looks as follows:
|
|
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
|
|
|
36
|
-
Then iterate over pages like
|
|
36
|
+
Then iterate over the pages like this:
|
|
37
37
|
|
|
38
38
|
```javascript
|
|
39
39
|
for (const page of allPages(pages)) {
|
|
40
|
-
|
|
40
|
+
console.log(page);
|
|
41
41
|
}
|
|
42
42
|
```
|
|
43
43
|
|
|
44
44
|
## Extending `page` object
|
|
45
45
|
|
|
46
|
-
Every single `page` object inside structure can be modified with extender. Extenders are set as additional parameters of
|
|
47
|
-
There are two types of extenders:
|
|
46
|
+
Every single `page` object inside structure can be modified with extender. Extenders are set as additional parameters of
|
|
47
|
+
the `getPages()` function. There are two types of extenders:
|
|
48
48
|
|
|
49
49
|
### *Callback* extenders
|
|
50
50
|
|
|
51
|
-
Callback extender is a function that is called during recursion over each page with three
|
|
52
|
-
|
|
51
|
+
Callback extender is a function that is called during recursion over each page with three parameters passed to the
|
|
52
|
+
function `page`, `path` and [`dirent`](https://nodejs.org/api/fs.html#class-fsdirent).
|
|
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);
|
|
@@ -61,23 +61,24 @@ const pages = await getPages({path: 'content'}, callbackExtender);
|
|
|
61
61
|
|
|
62
62
|
### *Object* extenders
|
|
63
63
|
|
|
64
|
-
This extender is just a simple JavaScript object that is combined with the `page` object using
|
|
64
|
+
This extender is just a simple JavaScript object that is combined with the `page` object using
|
|
65
|
+
the [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
|
|
66
|
+
function.
|
|
65
67
|
|
|
66
68
|
```javascript
|
|
67
69
|
const objectExtender = {
|
|
68
|
-
|
|
70
|
+
author: 'Roman Ožana'
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
const pages = await getPages({path: 'content'}, objectExtender);
|
|
72
74
|
```
|
|
73
75
|
|
|
74
|
-
There is no limit to the number of extenders, you can combine as many as you want.
|
|
75
|
-
Let's have the following code:
|
|
76
|
+
There is no limit to the number of extenders, you can combine as many as you want. Let's have the following code:
|
|
76
77
|
|
|
77
78
|
```javascript
|
|
78
79
|
const extenders = [
|
|
79
80
|
|
|
80
|
-
// callback extenders will be called during iteration
|
|
81
|
+
// callback extenders will be called during iteration one by one
|
|
81
82
|
|
|
82
83
|
(page) => {
|
|
83
84
|
// add property
|
|
@@ -91,13 +92,13 @@ const extenders = [
|
|
|
91
92
|
},
|
|
92
93
|
|
|
93
94
|
// callback extenders are called in the series
|
|
94
|
-
|
|
95
|
+
|
|
95
96
|
(page) => {
|
|
96
97
|
page.counter++;
|
|
97
98
|
},
|
|
98
99
|
|
|
99
100
|
// object extender will be merged with page object
|
|
100
|
-
|
|
101
|
+
|
|
101
102
|
{
|
|
102
103
|
"author": "Roman Ožana",
|
|
103
104
|
"getLink": function () {
|
|
@@ -113,47 +114,50 @@ then you get this structure:
|
|
|
113
114
|
|
|
114
115
|
```json
|
|
115
116
|
[
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
117
|
+
{
|
|
118
|
+
"name": "Main page",
|
|
119
|
+
"path": "content/Main page.md",
|
|
120
|
+
"title": "Main page | my best website",
|
|
121
|
+
"counter": 2,
|
|
122
|
+
"author": "Roman Ožana",
|
|
123
|
+
"getDate": "[Function: getDate]",
|
|
124
|
+
"getLink": "[Function: getLink]"
|
|
125
|
+
}
|
|
125
126
|
]
|
|
126
127
|
```
|
|
127
128
|
|
|
128
129
|
## Installation
|
|
129
130
|
|
|
131
|
+
Requires [Node.js](https://nodejs.org/) 22 or newer.
|
|
132
|
+
|
|
130
133
|
```bash
|
|
131
|
-
|
|
134
|
+
pnpm add @sphido/core
|
|
132
135
|
```
|
|
133
136
|
|
|
134
137
|
## Example
|
|
135
138
|
|
|
136
|
-
|
|
139
|
+
The following example reads all `*.md` files in the `content` directory and processes them
|
|
140
|
+
with [marked](https://github.com/markedjs/marked) into HTML files.
|
|
137
141
|
|
|
138
142
|
```javascript
|
|
139
143
|
#!/usr/bin/env node
|
|
140
144
|
|
|
141
|
-
import {dirname, relative, join} from 'node:path';
|
|
142
|
-
import {getPages, allPages, readFile, writeFile} from '@sphido/core';
|
|
145
|
+
import { dirname, relative, join } from 'node:path';
|
|
146
|
+
import { getPages, allPages, readFile, writeFile } from '@sphido/core';
|
|
143
147
|
import slugify from '@sindresorhus/slugify';
|
|
144
|
-
import {marked} from 'marked';
|
|
148
|
+
import { marked } from 'marked';
|
|
145
149
|
|
|
146
150
|
const pages = await getPages({path: 'content'}, // ... extenders
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
+
(page) => {
|
|
152
|
+
page.slug = slugify(page.name) + '.html';
|
|
153
|
+
page.dir = dirname(page.path);
|
|
154
|
+
});
|
|
151
155
|
|
|
152
156
|
for (const page of allPages(pages)) {
|
|
153
|
-
|
|
154
|
-
|
|
157
|
+
page.output = join('public', relative('content', page.dir), page.slug);
|
|
158
|
+
page.content = marked(await readFile(page.path));
|
|
155
159
|
|
|
156
|
-
|
|
160
|
+
await writeFile(page.output, `<!DOCTYPE html>
|
|
157
161
|
<html lang="en" dir="ltr">
|
|
158
162
|
<head>
|
|
159
163
|
<meta charset="UTF-8">
|
|
@@ -167,6 +171,6 @@ for (const page of allPages(pages)) {
|
|
|
167
171
|
}
|
|
168
172
|
```
|
|
169
173
|
|
|
170
|
-
## Source
|
|
174
|
+
## Source code
|
|
171
175
|
|
|
172
176
|
[@sphido/core](https://github.com/sphido/sphido/tree/main/packages/sphido-core)
|
package/lib/all-pages.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Page generator that flatten pages structure
|
|
3
|
-
*
|
|
4
|
-
* @param {import('get-pages.js').Pages} pages
|
|
5
|
-
* @return {import('get-pages.js').Page}
|
|
6
|
-
*/
|
|
7
|
-
export function * allPages(pages) {
|
|
8
|
-
for (const page of pages) {
|
|
9
|
-
if (page?.children) {
|
|
10
|
-
yield * allPages(page.children);
|
|
11
|
-
} else {
|
|
12
|
-
yield page;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
package/lib/copy-file.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {copyFile as copyFileAsync} from 'node:fs/promises';
|
|
2
|
-
import {dirname} from 'node:path';
|
|
3
|
-
import {mkdir} from 'node:fs/promises';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Write content to the file and create directory if not exists
|
|
7
|
-
*
|
|
8
|
-
* @param {string} src
|
|
9
|
-
* @param {string} dest
|
|
10
|
-
* @returns {Promise<*>}
|
|
11
|
-
*/
|
|
12
|
-
export async function copyFile(src, dest) {
|
|
13
|
-
await mkdir(dirname(dest), {recursive: true});
|
|
14
|
-
return copyFileAsync(src, dest);
|
|
15
|
-
}
|
package/lib/get-pages.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import {join, parse} from 'node:path';
|
|
2
|
-
import {readdir} from 'node:fs/promises';
|
|
3
|
-
import {isPage} from './is-page.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Retrieve an array tree of pages from path
|
|
7
|
-
*
|
|
8
|
-
* @param {Options}
|
|
9
|
-
* @param {Extenders} extenders
|
|
10
|
-
* @return {Promise<Awaited<Pages>[]>}
|
|
11
|
-
*/
|
|
12
|
-
export async function getPages({path = 'content', include = isPage} = {}, ...extenders) {
|
|
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
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @typedef {Array.<Object|ExtenderCallback>} Extenders
|
|
40
|
-
* @typedef {Array.<Page>} Pages
|
|
41
|
-
* @typedef {{name: string, path: string, children?: Pages}} Page
|
|
42
|
-
* @typedef {{path: string, include?: IncludePage}} Options
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @callback ExtenderCallback
|
|
47
|
-
* @param {Page} page
|
|
48
|
-
* @param {import('node:fs').Dirent} dirent
|
|
49
|
-
* @param {string=} path
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @callback IncludePage
|
|
54
|
-
* @param {import('node:fs').Dirent} dirent
|
|
55
|
-
* @param {string=} path
|
|
56
|
-
*/
|
package/lib/index.js
DELETED
package/lib/is-page.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Default page filter
|
|
3
|
-
*
|
|
4
|
-
* - skip hidden files (starts with .)
|
|
5
|
-
* - skip and directories (starts with .)
|
|
6
|
-
* - skip drafts files with underscore (_) at the beginning
|
|
7
|
-
* - accept only *.html and *.md files
|
|
8
|
-
*
|
|
9
|
-
* @param {import('node:fs').Dirent} dirent
|
|
10
|
-
* @returns {boolean}
|
|
11
|
-
*/
|
|
12
|
-
export function isPage(dirent) {
|
|
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
|
-
|
|
18
|
-
// Or not hidden directory
|
|
19
|
-
return dirent.isDirectory() && !dirent.name.startsWith('.');
|
|
20
|
-
}
|
package/lib/read-file.js
DELETED
package/lib/write-file.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {writeFile as writeFileAsync} from 'node:fs/promises';
|
|
2
|
-
import {dirname} from 'node:path';
|
|
3
|
-
import {mkdir} from 'node:fs/promises';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Write content to the file and create directory if not exists
|
|
7
|
-
*
|
|
8
|
-
* @param {string} file
|
|
9
|
-
* @param {string} content
|
|
10
|
-
* @returns {Promise<*>}
|
|
11
|
-
*/
|
|
12
|
-
export async function writeFile(file, content) {
|
|
13
|
-
await mkdir(dirname(file), {recursive: true});
|
|
14
|
-
return writeFileAsync(file, content);
|
|
15
|
-
}
|
package/types/all-pages.d.ts
DELETED
package/types/copy-file.d.ts
DELETED
package/types/get-pages.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retrieve an array tree of pages from path
|
|
3
|
-
*
|
|
4
|
-
* @param {Options}
|
|
5
|
-
* @param {Extenders} extenders
|
|
6
|
-
* @return {Promise<Awaited<Pages>[]>}
|
|
7
|
-
*/
|
|
8
|
-
export function getPages({ path, include }?: Options, ...extenders: Extenders): Promise<Awaited<Pages>[]>;
|
|
9
|
-
export type Extenders = Array<any | ExtenderCallback>;
|
|
10
|
-
export type Pages = Array<Page>;
|
|
11
|
-
export type Page = {
|
|
12
|
-
name: string;
|
|
13
|
-
path: string;
|
|
14
|
-
children?: Page[];
|
|
15
|
-
};
|
|
16
|
-
export type Options = {
|
|
17
|
-
path: string;
|
|
18
|
-
include?: IncludePage;
|
|
19
|
-
};
|
|
20
|
-
export type ExtenderCallback = (page: Page, dirent: import('node:fs').Dirent, path?: string | undefined) => any;
|
|
21
|
-
export type IncludePage = (dirent: import('node:fs').Dirent, path?: string | undefined) => any;
|
package/types/is-page.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Default page filter
|
|
3
|
-
*
|
|
4
|
-
* - skip hidden files (starts with .)
|
|
5
|
-
* - skip and directories (starts with .)
|
|
6
|
-
* - skip drafts files with underscore (_) at the beginning
|
|
7
|
-
* - accept only *.html and *.md files
|
|
8
|
-
*
|
|
9
|
-
* @param {import('node:fs').Dirent} dirent
|
|
10
|
-
* @returns {boolean}
|
|
11
|
-
*/
|
|
12
|
-
export function isPage(dirent: import('node:fs').Dirent): boolean;
|
package/types/read-file.d.ts
DELETED
package/types/write-file.d.ts
DELETED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { getPages } from "./get-pages.js";
|
|
2
1
|
export { allPages } from "./all-pages.js";
|
|
2
|
+
export { copyFile } from "./copy-file.js";
|
|
3
|
+
export { getPages } from "./get-pages.js";
|
|
3
4
|
export { readFile } from "./read-file.js";
|
|
4
5
|
export { writeFile } from "./write-file.js";
|
|
5
|
-
export { copyFile } from "./copy-file.js";
|