@sphido/core 2.0.8 → 2.0.11
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/all-pages.js +3 -0
- package/lib/copy-file.js +2 -2
- package/lib/get-pages.js +3 -3
- package/lib/is-page.js +0 -1
- package/package.json +2 -2
- package/readme.md +37 -21
package/lib/all-pages.js
CHANGED
package/lib/copy-file.js
CHANGED
|
@@ -5,8 +5,8 @@ import {dirname} from 'node:path';
|
|
|
5
5
|
/**
|
|
6
6
|
* Write content to the file and create directory if not exists
|
|
7
7
|
*
|
|
8
|
-
* @param {string}
|
|
9
|
-
* @param {string}
|
|
8
|
+
* @param {string} src
|
|
9
|
+
* @param {string} dest
|
|
10
10
|
* @returns {Promise<*>}
|
|
11
11
|
*/
|
|
12
12
|
export async function copyFile(src, dest) {
|
package/lib/get-pages.js
CHANGED
|
@@ -5,9 +5,9 @@ import {isPage} from './is-page.js';
|
|
|
5
5
|
/**
|
|
6
6
|
* Retrieve an array tree of pages from path
|
|
7
7
|
* @param {string} path
|
|
8
|
-
* @param {
|
|
9
|
-
* @param extenders
|
|
10
|
-
* @returns {Promise<Awaited<unknown>[{name, path}]>}
|
|
8
|
+
* @param {function(dirent:Dirent, path:string)} include
|
|
9
|
+
* @param {Object|function(page:{name:string, path:string}, dirent:Dirent, path:string)} extenders
|
|
10
|
+
* @returns {Promise<Awaited<unknown>[{name:string, path:string}]>}
|
|
11
11
|
*/
|
|
12
12
|
export async function getPages({path = 'content', include = isPage} = {}, ...extenders) {
|
|
13
13
|
const dir = await readdir(path, {withFileTypes: true});
|
package/lib/is-page.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphido/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.11",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Roman Ožana",
|
|
6
6
|
"email": "roman@ozana.cz",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"no-await-in-loop": 0
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "d242c810431b4070f81198c5266f54b72927ee19"
|
|
49
49
|
}
|
package/readme.md
CHANGED
|
@@ -41,12 +41,12 @@ for (const page of allPages(pages)) {
|
|
|
41
41
|
}
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
##
|
|
44
|
+
## Extending `page` object
|
|
45
45
|
|
|
46
46
|
Every single `page` object inside structure can be modified with extender. Extenders are set as additional parameters of the `getPages()` function.
|
|
47
47
|
There are two types of extenders:
|
|
48
48
|
|
|
49
|
-
### *
|
|
49
|
+
### *Callback* extenders
|
|
50
50
|
|
|
51
51
|
Callback extender is a function that is called during recursion over each page with three
|
|
52
52
|
parameters passed to the function `page`, `path` and [`dirent`](https://nodejs.org/api/fs.html#class-fsdirent).
|
|
@@ -59,7 +59,7 @@ const callbackExtender = (page, path, dirent) => {
|
|
|
59
59
|
const pages = await getPages({path: 'content'}, callbackExtender);
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
### *
|
|
62
|
+
### *Object* extenders
|
|
63
63
|
|
|
64
64
|
This extender is just a simple JavaScript object that is combined with the `page` object using the [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) function.
|
|
65
65
|
|
|
@@ -76,15 +76,28 @@ Let's have the following code:
|
|
|
76
76
|
|
|
77
77
|
```javascript
|
|
78
78
|
const extenders = [
|
|
79
|
-
|
|
79
|
+
|
|
80
|
+
// callback extenders will be called during iteration ony by one
|
|
81
|
+
|
|
80
82
|
(page) => {
|
|
81
83
|
// add property
|
|
82
84
|
page.title = `${page.name} | my best website`;
|
|
85
|
+
|
|
83
86
|
// or function
|
|
84
87
|
page.getDate = () => new Date();
|
|
88
|
+
|
|
89
|
+
// or something else
|
|
90
|
+
page.counter = 1;
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
// callback extenders are called in the series
|
|
94
|
+
|
|
95
|
+
(page) => {
|
|
96
|
+
page.counter++;
|
|
85
97
|
},
|
|
86
98
|
|
|
87
99
|
// object extender will be merged with page object
|
|
100
|
+
|
|
88
101
|
{
|
|
89
102
|
"author": "Roman Ožana",
|
|
90
103
|
"getLink": function () {
|
|
@@ -104,6 +117,7 @@ then you get this structure:
|
|
|
104
117
|
"name": "Main page",
|
|
105
118
|
"path": "content/Main page.md",
|
|
106
119
|
"title": "Main page | my best website",
|
|
120
|
+
"counter": 2,
|
|
107
121
|
"author": "Roman Ožana",
|
|
108
122
|
"getDate": "[Function: getDate]",
|
|
109
123
|
"getLink": "[Function: getLink]"
|
|
@@ -129,28 +143,30 @@ import {getPages, allPages, readFile, writeFile} from '@sphido/core';
|
|
|
129
143
|
import slugify from '@sindresorhus/slugify';
|
|
130
144
|
import {marked} from 'marked';
|
|
131
145
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
|
|
138
|
-
<title>${name} | Sphido Example page</title>
|
|
139
|
-
</head>
|
|
140
|
-
<body class="prose mx-auto my-6">${content}</body>
|
|
141
|
-
<!-- Generated with Sphido from ${path} -->
|
|
142
|
-
</html>`;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const pages = await getPages({path: 'content'});
|
|
146
|
+
const pages = await getPages({path: 'content'}, // ... extenders
|
|
147
|
+
(page) => {
|
|
148
|
+
page.slug = slugify(page.name) + '.html';
|
|
149
|
+
page.dir = dirname(page.path);
|
|
150
|
+
});
|
|
146
151
|
|
|
147
152
|
for (const page of allPages(pages)) {
|
|
148
|
-
page.output = join('public', relative('content',
|
|
153
|
+
page.output = join('public', relative('content', page.dir), page.slug);
|
|
149
154
|
page.content = marked(await readFile(page.path));
|
|
150
|
-
|
|
155
|
+
|
|
156
|
+
await writeFile(page.output, `<!DOCTYPE html>
|
|
157
|
+
<html lang="en" dir="ltr">
|
|
158
|
+
<head>
|
|
159
|
+
<meta charset="UTF-8">
|
|
160
|
+
<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
|
|
161
|
+
<title>${page.name} | Sphido Example</title>
|
|
162
|
+
</head>
|
|
163
|
+
<body class="prose mx-auto my-6">${page.content}</body>
|
|
164
|
+
<!-- Generated by Sphido from ${page.path} -->
|
|
165
|
+
</html>
|
|
166
|
+
`);
|
|
151
167
|
}
|
|
152
168
|
```
|
|
153
169
|
|
|
154
170
|
## Source codes
|
|
155
171
|
|
|
156
|
-
[@sphido/core](https://github.com/sphido/sphido/tree/main/packages/sphido-core)
|
|
172
|
+
[@sphido/core](https://github.com/sphido/sphido/tree/main/packages/sphido-core)
|