@static-pages/core 7.0.0-alpha.5 → 7.0.0-beta.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/CHANGELOG.md +0 -1
- package/README.md +25 -151
- package/cjs/index.d.ts +13 -4
- package/cjs/index.js +30 -8
- package/esm/index.d.ts +13 -4
- package/esm/index.js +28 -3
- package/package.json +1 -8
- package/cjs/autoparse.d.ts +0 -1
- package/cjs/autoparse.js +0 -37
- package/cjs/create-reader.d.ts +0 -15
- package/cjs/create-reader.js +0 -76
- package/cjs/create-writer.d.ts +0 -14
- package/cjs/create-writer.js +0 -94
- package/cjs/helpers.d.ts +0 -21
- package/cjs/helpers.js +0 -11
- package/cjs/static-pages.d.ts +0 -54
- package/cjs/static-pages.js +0 -66
- package/esm/autoparse.d.ts +0 -1
- package/esm/autoparse.js +0 -30
- package/esm/create-reader.d.ts +0 -15
- package/esm/create-reader.js +0 -69
- package/esm/create-writer.d.ts +0 -14
- package/esm/create-writer.js +0 -67
- package/esm/helpers.d.ts +0 -21
- package/esm/helpers.js +0 -4
- package/esm/static-pages.d.ts +0 -54
- package/esm/static-pages.js +0 -62
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
3
|
## 7.0.0
|
|
4
|
-
- New `CreateReader` and `CreateWriter` utilities available as `route.from` and `route.to` prop values.
|
|
5
4
|
- Reverted `route.to` callbacks to work as it was in `v4.0.0`. The function is called per doucument, recieving the current page data.
|
|
6
5
|
- Type defintion improvements.
|
|
7
6
|
- Test suite switched from tap to mocha for now.
|
package/README.md
CHANGED
|
@@ -17,60 +17,35 @@ Yes! Because I browsed the whole jamstack scene, but could not find one which
|
|
|
17
17
|
|
|
18
18
|
And because I wrote a ton of custom static generators before; I tought I can improve the concepts to a point where its (hopefully) useful for others.
|
|
19
19
|
|
|
20
|
+
This project is structured as a toolkit split to many packages, published under the [@static-pages](https://www.npmjs.com/search?q=%40static-pages) namespace on NPM.
|
|
21
|
+
In most cases you should not use this core package directly, but the [@static-pages/starter](https://www.npmjs.com/package/@static-pages/starter) is a good point to begin with.
|
|
22
|
+
|
|
20
23
|
## Where should I use this?
|
|
21
24
|
|
|
22
25
|
This project targets small and medium sized websites. The rendering process tries to be as fast as possible so its also useful when you need performance.
|
|
23
26
|
|
|
24
|
-
## Documentation
|
|
25
|
-
|
|
26
|
-
For detailed information, visit the [project page](https://staticpagesjs.github.io/).
|
|
27
|
-
|
|
28
27
|
## Usage
|
|
29
28
|
|
|
30
29
|
```js
|
|
30
|
+
import fs from 'node:fs';
|
|
31
|
+
import path from 'node:path';
|
|
31
32
|
import staticPages from '@static-pages/core';
|
|
32
|
-
import twig from '@static-pages/twig';
|
|
33
|
-
|
|
34
|
-
// Default options for every `Route` via .with() call.
|
|
35
|
-
const generate = staticPages.with({
|
|
36
|
-
to: {
|
|
37
|
-
render: twig({
|
|
38
|
-
viewsDir: 'path/to/views/folder'
|
|
39
|
-
}),
|
|
40
|
-
},
|
|
41
|
-
controller(data) {
|
|
42
|
-
// adds a 'now' variable to the template context
|
|
43
|
-
data.now = new Date().toJSON();
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
return data;
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// Generate every document type as a page.
|
|
51
|
-
// One route equals one batch of similar pages.
|
|
52
|
-
generate({
|
|
53
|
-
from: {
|
|
54
|
-
cwd: 'pages',
|
|
55
|
-
pattern: '**/*.md',
|
|
56
|
-
},
|
|
57
|
-
}, {
|
|
58
|
-
// Any Iterable or AsyncIterable also accepted eg. an array
|
|
34
|
+
staticPages({
|
|
59
35
|
from: [
|
|
60
|
-
{ title: 'About', url: 'about',
|
|
61
|
-
{ title: 'Privacy', url: 'privacy',
|
|
36
|
+
{ title: 'About', url: 'about', content: 'About us content' },
|
|
37
|
+
{ title: 'Privacy', url: 'privacy', content: 'Privacy content' },
|
|
62
38
|
],
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
pattern: '*.yaml',
|
|
67
|
-
},
|
|
68
|
-
to: {
|
|
69
|
-
render: twig({
|
|
70
|
-
view: 'home.html.twig',
|
|
71
|
-
viewsDir: 'path/to/views/folder',
|
|
72
|
-
}),
|
|
39
|
+
controller(data) {
|
|
40
|
+
data.now = new Date().toJSON();
|
|
41
|
+
return data;
|
|
73
42
|
},
|
|
43
|
+
to({ title, url, content, now }) {
|
|
44
|
+
const fileName = path.join('public', url + '.html');
|
|
45
|
+
const fileContent = `<html><body><h1>${title}</h1><p>${content}</p><p>generated: ${now}</p></body></html>`;
|
|
46
|
+
fs.mkdirSync(path.dirname(fileName), { recursive: true });
|
|
47
|
+
fs.writeFileSync(fileName, fileContent);
|
|
48
|
+
}
|
|
74
49
|
})
|
|
75
50
|
.catch(error => {
|
|
76
51
|
console.error('Error:', error);
|
|
@@ -80,126 +55,25 @@ generate({
|
|
|
80
55
|
|
|
81
56
|
### Notes
|
|
82
57
|
|
|
83
|
-
> The
|
|
84
|
-
|
|
85
|
-
> The `from` parameter can also recieve an `Iterable` or an `AsyncIterable` type!
|
|
58
|
+
> The usage example above does a rough presentation only and not considered to be a production ready snippet. Helpers to read and write documents are provided in separate packages, eg. [@static-pages/io](https://www.npmjs.com/package/@static-pages/io).
|
|
86
59
|
|
|
87
|
-
|
|
60
|
+
## Documentation
|
|
88
61
|
|
|
62
|
+
For detailed information, visit the [project page](https://staticpagesjs.github.io/).
|
|
89
63
|
|
|
90
|
-
|
|
64
|
+
### `staticPages(...routes: Route[]): Promise<void>`
|
|
91
65
|
|
|
92
66
|
Each route consists of a `from`, `to` and a `controller` property matching the definition below.
|
|
93
67
|
|
|
94
68
|
```ts
|
|
95
69
|
interface Route<F, T> {
|
|
96
|
-
from
|
|
97
|
-
to
|
|
98
|
-
controller?(data: F):
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
102
|
-
|
|
103
|
-
interface CreateReaderOptions<T> {
|
|
104
|
-
// Handles file operations, defaults to nodejs `fs` module
|
|
105
|
-
fs?: Filesystem;
|
|
106
|
-
// Current working directory
|
|
107
|
-
cwd?: string;
|
|
108
|
-
// File patterns to include
|
|
109
|
-
pattern?: string | string[];
|
|
110
|
-
// File patterns to exclude
|
|
111
|
-
ignore?: string | string[];
|
|
112
|
-
// Callback to parse a file content into an object
|
|
113
|
-
parse?(content: Uint8Array | string, filename: string): MaybePromise<T>;
|
|
114
|
-
// Called on error
|
|
115
|
-
onError?(error: unknown): MaybePromise<void>;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
interface CreateWriterOptions<T> {
|
|
119
|
-
// Handles file operations, defaults to nodejs `fs` module
|
|
120
|
-
fs?: Filesystem;
|
|
121
|
-
// Current working directory
|
|
122
|
-
cwd?: string;
|
|
123
|
-
// Callback that renders the document into a page
|
|
124
|
-
render?(data: T): MaybePromise<Uint8Array | string>;
|
|
125
|
-
// Callback that retrieves the filename (URL) of a page
|
|
126
|
-
name?(data: T): MaybePromise<string>;
|
|
127
|
-
// Called on error
|
|
128
|
-
onError?(error: unknown): MaybePromise<void>;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
interface Filesystem {
|
|
132
|
-
stat(
|
|
133
|
-
path: string | URL,
|
|
134
|
-
callback: (err: Error | null, stats: { isFile(): boolean; isDirectory(): boolean; }) => void
|
|
135
|
-
): void;
|
|
136
|
-
|
|
137
|
-
readdir(
|
|
138
|
-
path: string | URL,
|
|
139
|
-
options: {
|
|
140
|
-
encoding: 'utf8';
|
|
141
|
-
withFileTypes: false;
|
|
142
|
-
recursive: boolean;
|
|
143
|
-
},
|
|
144
|
-
callback: (err: Error | null, files: string[]) => void,
|
|
145
|
-
): void;
|
|
146
|
-
|
|
147
|
-
mkdir(
|
|
148
|
-
path: string | URL,
|
|
149
|
-
options: {
|
|
150
|
-
recursive: true;
|
|
151
|
-
},
|
|
152
|
-
callback: (err: Error | null, path?: string) => void
|
|
153
|
-
): void;
|
|
154
|
-
|
|
155
|
-
readFile(
|
|
156
|
-
path: string | URL,
|
|
157
|
-
callback: (err: Error | null, data: Uint8Array) => void
|
|
158
|
-
): void;
|
|
159
|
-
|
|
160
|
-
writeFile(
|
|
161
|
-
path: string | URL,
|
|
162
|
-
data: string | Uint8Array,
|
|
163
|
-
callback: (err: Error | null) => void
|
|
164
|
-
): void;
|
|
70
|
+
from: Iterable<F> | AsyncIterable<F>;
|
|
71
|
+
to(data: T): void | Promise<void>;
|
|
72
|
+
controller?(data: F): undefined | T | Iterable<T> | AsyncIterable<T> | Promise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
165
73
|
}
|
|
166
74
|
```
|
|
167
75
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
When you use the `createReader` and `createWriter` interfaces to read and write documents, you can provide a `Filesystem` implementation. This interface is a minimal subset of the [NodeJS FS API](https://nodejs.org/api/fs.html). By default we use the built-in `node:fs` module.
|
|
171
|
-
|
|
172
|
-
### `CreateReaderOptions` default parameters
|
|
173
|
-
- `fs`: the nodejs `fs` module
|
|
174
|
-
- `cwd`: `'pages'`
|
|
175
|
-
- `parse`: *see About the default `parse` function*
|
|
176
|
-
- `onError`: `(err) => { throw err; }`
|
|
177
|
-
|
|
178
|
-
### `CreateWriterOptions` default parameters
|
|
179
|
-
- `fs`: the nodejs `fs` module
|
|
180
|
-
- `cwd`: `'public'`
|
|
181
|
-
- `name`: `(data) => data.url`
|
|
182
|
-
- `render`: `(data) => data.content`
|
|
183
|
-
- `onError`: `(err) => { throw err; }`
|
|
184
|
-
|
|
185
|
-
### About the default `parse` function
|
|
186
|
-
|
|
187
|
-
When using the default parser, a file type will be guessed by the file extension.
|
|
188
|
-
These could be `json`, `yaml`, `yml`, `md` or `markdown`.
|
|
189
|
-
- `json` will be parsed with `JSON.parse`
|
|
190
|
-
- `yaml` and `yml` will be parsed with the `yaml` package
|
|
191
|
-
- `md` and `markdown` will be parsed with the `gray-matter` package
|
|
192
|
-
|
|
193
|
-
When the document does not contain an `url` property, this function will create one containing the filename without extension.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
## `staticPages.with(defaults: Partial<Route>): { (...routes: Partial<Route>[]): Promise<void>; }`
|
|
197
|
-
|
|
198
|
-
Preconfigures a separate instance of the `staticPages` call with the given default parameters.
|
|
199
|
-
These only works as fallback values, you can override every value later.
|
|
200
|
-
|
|
201
|
-
If a `from` or `to` parameter is a plain object in both defaults and later at the route definition they will be merged (see usage example).
|
|
202
|
-
|
|
76
|
+
The `controller` may return with multiple documents, each will be rendered as a separate page. Alternatively it may return `undefined` to prevent the rendering of the current document.
|
|
203
77
|
|
|
204
78
|
## Missing a feature?
|
|
205
79
|
Create an issue describing your needs!
|
package/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export interface Route<F = unknown, T = unknown> {
|
|
2
|
+
from: Iterable<F> | AsyncIterable<F>;
|
|
3
|
+
to(data: T): void | Promise<void>;
|
|
4
|
+
controller?(data: F): undefined | T | Iterable<T> | AsyncIterable<T> | Promise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
5
|
+
}
|
|
6
|
+
export declare function staticPages<F1, T1>(...route: [Route<F1, T1>]): Promise<void>;
|
|
7
|
+
export declare function staticPages<F1, T1, F2, T2>(...route: [Route<F1, T1>, Route<F2, T2>]): Promise<void>;
|
|
8
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>]): Promise<void>;
|
|
9
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>, Route<F4, T4>]): Promise<void>;
|
|
10
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>, Route<F4, T4>, Route<F5, T5>]): Promise<void>;
|
|
11
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5, F6, T6>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>, Route<F4, T4>, Route<F5, T5>, Route<F6, T6>]): Promise<void>;
|
|
12
|
+
export declare function staticPages(...routes: Route[]): Promise<void>;
|
|
13
|
+
export default staticPages;
|
package/cjs/index.js
CHANGED
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
exports.staticPages = void 0;
|
|
4
|
+
const isIterable = (x) => !!x && typeof x === 'object' && Symbol.iterator in x && typeof x[Symbol.iterator] === 'function';
|
|
5
|
+
const isAsyncIterable = (x) => !!x && typeof x === 'object' && Symbol.asyncIterator in x && typeof x[Symbol.asyncIterator] === 'function';
|
|
6
|
+
const getType = (x) => typeof x === 'object' ? (x ? (Array.isArray(x) ? 'array' : 'object') : 'null') : typeof x;
|
|
7
|
+
async function staticPages(...routes) {
|
|
8
|
+
for (const route of routes) {
|
|
9
|
+
if (typeof route !== 'object' || !route)
|
|
10
|
+
throw new TypeError(`Expected 'object', recieved '${getType(route)}'.`);
|
|
11
|
+
let { from, to, controller } = route;
|
|
12
|
+
if (!isIterable(from) && !isAsyncIterable(from))
|
|
13
|
+
throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at 'from' property.`);
|
|
14
|
+
if (typeof to !== 'function')
|
|
15
|
+
throw new TypeError(`Expected 'function', recieved '${getType(to)}' at 'to' property.`);
|
|
16
|
+
if (typeof controller !== 'undefined' && typeof controller !== 'function')
|
|
17
|
+
throw new TypeError(`Expected 'function', recieved '${getType(controller)}' at 'controller' property.`);
|
|
18
|
+
for await (const item of from) {
|
|
19
|
+
const data = controller ? await controller(item) : item;
|
|
20
|
+
if (isIterable(data) || isAsyncIterable(data)) {
|
|
21
|
+
for await (const item of data) {
|
|
22
|
+
await to(item);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else if (typeof data !== 'undefined') {
|
|
26
|
+
await to(data);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.staticPages = staticPages;
|
|
32
|
+
exports.default = staticPages;
|
package/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export interface Route<F = unknown, T = unknown> {
|
|
2
|
+
from: Iterable<F> | AsyncIterable<F>;
|
|
3
|
+
to(data: T): void | Promise<void>;
|
|
4
|
+
controller?(data: F): undefined | T | Iterable<T> | AsyncIterable<T> | Promise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
5
|
+
}
|
|
6
|
+
export declare function staticPages<F1, T1>(...route: [Route<F1, T1>]): Promise<void>;
|
|
7
|
+
export declare function staticPages<F1, T1, F2, T2>(...route: [Route<F1, T1>, Route<F2, T2>]): Promise<void>;
|
|
8
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>]): Promise<void>;
|
|
9
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>, Route<F4, T4>]): Promise<void>;
|
|
10
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>, Route<F4, T4>, Route<F5, T5>]): Promise<void>;
|
|
11
|
+
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5, F6, T6>(...route: [Route<F1, T1>, Route<F2, T2>, Route<F3, T3>, Route<F4, T4>, Route<F5, T5>, Route<F6, T6>]): Promise<void>;
|
|
12
|
+
export declare function staticPages(...routes: Route[]): Promise<void>;
|
|
13
|
+
export default staticPages;
|
package/esm/index.js
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const isIterable = (x) => !!x && typeof x === 'object' && Symbol.iterator in x && typeof x[Symbol.iterator] === 'function';
|
|
2
|
+
const isAsyncIterable = (x) => !!x && typeof x === 'object' && Symbol.asyncIterator in x && typeof x[Symbol.asyncIterator] === 'function';
|
|
3
|
+
const getType = (x) => typeof x === 'object' ? (x ? (Array.isArray(x) ? 'array' : 'object') : 'null') : typeof x;
|
|
4
|
+
export async function staticPages(...routes) {
|
|
5
|
+
for (const route of routes) {
|
|
6
|
+
if (typeof route !== 'object' || !route)
|
|
7
|
+
throw new TypeError(`Expected 'object', recieved '${getType(route)}'.`);
|
|
8
|
+
let { from, to, controller } = route;
|
|
9
|
+
if (!isIterable(from) && !isAsyncIterable(from))
|
|
10
|
+
throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at 'from' property.`);
|
|
11
|
+
if (typeof to !== 'function')
|
|
12
|
+
throw new TypeError(`Expected 'function', recieved '${getType(to)}' at 'to' property.`);
|
|
13
|
+
if (typeof controller !== 'undefined' && typeof controller !== 'function')
|
|
14
|
+
throw new TypeError(`Expected 'function', recieved '${getType(controller)}' at 'controller' property.`);
|
|
15
|
+
for await (const item of from) {
|
|
16
|
+
const data = controller ? await controller(item) : item;
|
|
17
|
+
if (isIterable(data) || isAsyncIterable(data)) {
|
|
18
|
+
for await (const item of data) {
|
|
19
|
+
await to(item);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else if (typeof data !== 'undefined') {
|
|
23
|
+
await to(data);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export default staticPages;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@static-pages/core",
|
|
3
|
-
"version": "7.0.0-
|
|
3
|
+
"version": "7.0.0-beta.0",
|
|
4
4
|
"description": "General purpose static pages renderer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "cjs/index.js",
|
|
@@ -29,20 +29,13 @@
|
|
|
29
29
|
"test": "mocha",
|
|
30
30
|
"coverage": "c8 -r text -r text-summary -r lcov --include \"esm/*\" npm test"
|
|
31
31
|
},
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"gray-matter": "^4.0.3",
|
|
34
|
-
"yaml": "^2.3.4"
|
|
35
|
-
},
|
|
36
32
|
"devDependencies": {
|
|
37
|
-
"@types/micromatch": "^4.0.4",
|
|
38
33
|
"@types/node": "^20.10.5",
|
|
39
|
-
"@types/picomatch": "^2.3.2",
|
|
40
34
|
"@typescript-eslint/eslint-plugin": "^6.15.0",
|
|
41
35
|
"@typescript-eslint/parser": "^6.15.0",
|
|
42
36
|
"c8": "^8.0.0",
|
|
43
37
|
"eslint": "^8.1.0",
|
|
44
38
|
"mocha": "^10.2.0",
|
|
45
|
-
"picomatch": "^3.0.1",
|
|
46
39
|
"rimraf": "^5.0.1",
|
|
47
40
|
"typescript": "^5.1.6"
|
|
48
41
|
},
|
package/cjs/autoparse.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function autoparse(content: string | Uint8Array, filename: string): any;
|
package/cjs/autoparse.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.autoparse = void 0;
|
|
7
|
-
const yaml_1 = require("yaml");
|
|
8
|
-
const gray_matter_1 = __importDefault(require("gray-matter"));
|
|
9
|
-
function autoparse(content, filename) {
|
|
10
|
-
const dot = filename.lastIndexOf('.');
|
|
11
|
-
if (dot < 1) {
|
|
12
|
-
throw new Error(`Could not parse document without an extension.`);
|
|
13
|
-
}
|
|
14
|
-
const extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
|
|
15
|
-
let doc;
|
|
16
|
-
switch (extension) {
|
|
17
|
-
case 'json':
|
|
18
|
-
doc = JSON.parse(content.toString());
|
|
19
|
-
break;
|
|
20
|
-
case 'yaml':
|
|
21
|
-
case 'yml':
|
|
22
|
-
doc = (0, yaml_1.parse)(content.toString());
|
|
23
|
-
break;
|
|
24
|
-
case 'md':
|
|
25
|
-
case 'markdown':
|
|
26
|
-
const { data, content: markdownContent } = (0, gray_matter_1.default)(content.toString());
|
|
27
|
-
doc = { ...data, content: markdownContent };
|
|
28
|
-
break;
|
|
29
|
-
default:
|
|
30
|
-
throw new Error(`Could not parse document with '${extension}' extension.`);
|
|
31
|
-
}
|
|
32
|
-
if (doc && typeof doc === 'object' && !('url' in doc)) {
|
|
33
|
-
doc.url = filename.substring(0, dot).replace(/\\/g, '/');
|
|
34
|
-
}
|
|
35
|
-
return doc;
|
|
36
|
-
}
|
|
37
|
-
exports.autoparse = autoparse;
|
package/cjs/create-reader.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise, Filesystem } from './helpers.js';
|
|
2
|
-
export declare namespace createReader {
|
|
3
|
-
interface Options<T> {
|
|
4
|
-
fs?: Filesystem;
|
|
5
|
-
cwd?: string;
|
|
6
|
-
pattern?: string | string[];
|
|
7
|
-
ignore?: string | string[];
|
|
8
|
-
parse?(content: Uint8Array | string, filename: string): MaybePromise<T>;
|
|
9
|
-
onError?(error: unknown): MaybePromise<void>;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export declare function createReader<T>({ fs, cwd, pattern, ignore, parse, onError, }?: createReader.Options<T>): AsyncGenerator<Awaited<T>, void, unknown>;
|
|
13
|
-
export declare namespace createReader {
|
|
14
|
-
var isOptions: <T>(x: unknown) => x is Options<T>;
|
|
15
|
-
}
|
package/cjs/create-reader.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createReader = void 0;
|
|
7
|
-
const picomatch_1 = __importDefault(require("picomatch"));
|
|
8
|
-
const autoparse_js_1 = require("./autoparse.js");
|
|
9
|
-
const helpers_js_1 = require("./helpers.js");
|
|
10
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
|
-
async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, ignore, parse = autoparse_js_1.autoparse, onError = (error) => { throw error; }, } = {}) {
|
|
12
|
-
if (!(0, helpers_js_1.isFilesystem)(fs))
|
|
13
|
-
throw new TypeError(`Expected Node FS compatible implementation at 'fs' property.`);
|
|
14
|
-
if (typeof cwd !== 'string')
|
|
15
|
-
throw new TypeError(`Expected 'string', recieved '${(0, helpers_js_1.getType)(cwd)}' at 'cwd' property.`);
|
|
16
|
-
if (!cwd)
|
|
17
|
-
throw new TypeError(`Expected non-empty string at 'cwd'.`);
|
|
18
|
-
if (typeof pattern !== 'undefined' && typeof pattern !== 'string' && !Array.isArray(pattern))
|
|
19
|
-
throw new TypeError(`Expected 'string' or 'string[]', recieved '${(0, helpers_js_1.getType)(pattern)}' at 'pattern' property.`);
|
|
20
|
-
if (typeof ignore !== 'undefined' && typeof ignore !== 'string' && !Array.isArray(ignore))
|
|
21
|
-
throw new TypeError(`Expected 'string' or 'string[]', recieved '${(0, helpers_js_1.getType)(ignore)}' at 'ignore' property.`);
|
|
22
|
-
if (typeof parse !== 'function')
|
|
23
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(parse)}' at 'parse' property.`);
|
|
24
|
-
if (typeof onError !== 'function')
|
|
25
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(onError)}' at 'onError' property.`);
|
|
26
|
-
let filenames = await new Promise((resolve, reject) => {
|
|
27
|
-
fs.readdir(cwd, { recursive: true, withFileTypes: false, encoding: 'utf8' }, (err, entries) => {
|
|
28
|
-
if (err)
|
|
29
|
-
return reject(err);
|
|
30
|
-
let filtered = [];
|
|
31
|
-
let processed = 0;
|
|
32
|
-
for (const entry of entries) {
|
|
33
|
-
fs.stat(cwd + '/' + entry, (err, stats) => {
|
|
34
|
-
if (err)
|
|
35
|
-
return reject(err);
|
|
36
|
-
if (stats.isFile()) {
|
|
37
|
-
filtered.push(entry);
|
|
38
|
-
}
|
|
39
|
-
if (++processed === entries.length) {
|
|
40
|
-
return resolve(filtered);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
|
|
47
|
-
const filteredFilenames = [];
|
|
48
|
-
const isMatch = (0, picomatch_1.default)(pattern !== null && pattern !== void 0 ? pattern : '**/*', { ignore });
|
|
49
|
-
for (const filename of filenames) {
|
|
50
|
-
if (isMatch(filename)) {
|
|
51
|
-
filteredFilenames.push(filename);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
filenames = filteredFilenames;
|
|
55
|
-
}
|
|
56
|
-
for (const filename of filenames) {
|
|
57
|
-
try {
|
|
58
|
-
const content = await new Promise((resolve, reject) => {
|
|
59
|
-
fs.readFile(cwd + '/' + filename, (err, data) => {
|
|
60
|
-
if (err)
|
|
61
|
-
reject(err);
|
|
62
|
-
else
|
|
63
|
-
resolve(data);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
yield await parse(content, filename);
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
await onError(error);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.createReader = createReader;
|
|
74
|
-
createReader.isOptions = (x) => {
|
|
75
|
-
return x == undefined || (!!x && typeof x === 'object' && !(0, helpers_js_1.isIterable)(x) && !(0, helpers_js_1.isAsyncIterable)(x));
|
|
76
|
-
};
|
package/cjs/create-writer.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise, Filesystem } from './helpers.js';
|
|
2
|
-
export declare namespace createWriter {
|
|
3
|
-
interface Options<T> {
|
|
4
|
-
fs?: Filesystem;
|
|
5
|
-
cwd?: string;
|
|
6
|
-
name?(data: T): MaybePromise<string>;
|
|
7
|
-
render?(data: T): MaybePromise<Uint8Array | string>;
|
|
8
|
-
onError?(error: unknown): MaybePromise<void>;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (data: T) => Promise<void>;
|
|
12
|
-
export declare namespace createWriter {
|
|
13
|
-
var isOptions: <T>(x: unknown) => x is Options<T>;
|
|
14
|
-
}
|
package/cjs/create-writer.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.createWriter = void 0;
|
|
27
|
-
const helpers_js_1 = require("./helpers.js");
|
|
28
|
-
const nodeFs = __importStar(require("node:fs"));
|
|
29
|
-
const node_path_1 = require("node:path");
|
|
30
|
-
const defaultNamer = (data) => {
|
|
31
|
-
if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
|
|
32
|
-
return data.url.concat('.html');
|
|
33
|
-
}
|
|
34
|
-
throw new Error(`Missing 'url' field in the document.`);
|
|
35
|
-
};
|
|
36
|
-
const defaultRenderer = (data) => {
|
|
37
|
-
if (!!data && typeof data === 'object' && 'content' in data) {
|
|
38
|
-
return '' + data.content;
|
|
39
|
-
}
|
|
40
|
-
throw new Error(`Missing 'content' field in the document.`);
|
|
41
|
-
};
|
|
42
|
-
function createWriter({ fs = nodeFs, cwd = 'public', name = defaultNamer, render = defaultRenderer, onError = (error) => { throw error; }, } = {}) {
|
|
43
|
-
if (!(0, helpers_js_1.isFilesystem)(fs))
|
|
44
|
-
throw new TypeError(`Expected Node FS compatible implementation at 'fs' property.`);
|
|
45
|
-
if (typeof cwd !== 'string')
|
|
46
|
-
throw new TypeError(`Expected 'string', recieved '${(0, helpers_js_1.getType)(cwd)}' at 'cwd' property.`);
|
|
47
|
-
if (!cwd)
|
|
48
|
-
throw new TypeError(`Expected non-empty string at 'cwd'.`);
|
|
49
|
-
if (typeof render !== 'function')
|
|
50
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(render)}' at 'render' property.`);
|
|
51
|
-
if (typeof name !== 'function')
|
|
52
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(name)}' at 'name' property.`);
|
|
53
|
-
if (typeof onError !== 'function')
|
|
54
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(onError)}' at 'onError' property.`);
|
|
55
|
-
return async function (data) {
|
|
56
|
-
try {
|
|
57
|
-
const filepath = cwd + '/' + await name(data);
|
|
58
|
-
const dirpath = (0, node_path_1.dirname)(filepath);
|
|
59
|
-
await new Promise((resolve, reject) => {
|
|
60
|
-
fs.stat(dirpath, (err, stats) => {
|
|
61
|
-
if (err) {
|
|
62
|
-
fs.mkdir(dirpath, { recursive: true }, (err) => {
|
|
63
|
-
if (err) {
|
|
64
|
-
reject(err);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
resolve(undefined);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
resolve(undefined);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
const content = await render(data);
|
|
77
|
-
await new Promise((resolve, reject) => {
|
|
78
|
-
fs.writeFile(filepath, content, (err) => {
|
|
79
|
-
if (err)
|
|
80
|
-
reject(err);
|
|
81
|
-
else
|
|
82
|
-
resolve(undefined);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
await onError(error);
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
exports.createWriter = createWriter;
|
|
92
|
-
createWriter.isOptions = (x) => {
|
|
93
|
-
return x == undefined || (!!x && typeof x === 'object');
|
|
94
|
-
};
|
package/cjs/helpers.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
-
export interface Filesystem {
|
|
3
|
-
stat(path: string | URL, callback: (err: Error | null, stats: {
|
|
4
|
-
isFile(): boolean;
|
|
5
|
-
isDirectory(): boolean;
|
|
6
|
-
}) => void): void;
|
|
7
|
-
readdir(path: string | URL, options: {
|
|
8
|
-
encoding: 'utf8';
|
|
9
|
-
withFileTypes: false;
|
|
10
|
-
recursive: boolean;
|
|
11
|
-
}, callback: (err: Error | null, files: string[]) => void): void;
|
|
12
|
-
mkdir(path: string | URL, options: {
|
|
13
|
-
recursive: true;
|
|
14
|
-
}, callback: (err: Error | null, path?: string) => void): void;
|
|
15
|
-
readFile(path: string | URL, callback: (err: Error | null, data: Uint8Array) => void): void;
|
|
16
|
-
writeFile(path: string | URL, data: string | Uint8Array, callback: (err: Error | null) => void): void;
|
|
17
|
-
}
|
|
18
|
-
export declare const isFilesystem: (x: unknown) => x is Filesystem;
|
|
19
|
-
export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
|
|
20
|
-
export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
|
|
21
|
-
export declare const getType: (x: unknown) => string;
|
package/cjs/helpers.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getType = exports.isAsyncIterable = exports.isIterable = exports.isFilesystem = void 0;
|
|
4
|
-
const isFilesystem = (x) => !!x && typeof x === 'object' && 'stat' in x && 'mkdir' in x && 'readFile' in x && 'writeFile' in x;
|
|
5
|
-
exports.isFilesystem = isFilesystem;
|
|
6
|
-
const isIterable = (x) => !!x && typeof x === 'object' && Symbol.iterator in x && typeof x[Symbol.iterator] === 'function';
|
|
7
|
-
exports.isIterable = isIterable;
|
|
8
|
-
const isAsyncIterable = (x) => !!x && typeof x === 'object' && Symbol.asyncIterator in x && typeof x[Symbol.asyncIterator] === 'function';
|
|
9
|
-
exports.isAsyncIterable = isAsyncIterable;
|
|
10
|
-
const getType = (x) => typeof x === 'object' ? (x ? (Array.isArray(x) ? 'array' : 'object') : 'null') : typeof x;
|
|
11
|
-
exports.getType = getType;
|
package/cjs/static-pages.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise } from './helpers.js';
|
|
2
|
-
import { createReader } from './create-reader.js';
|
|
3
|
-
import { createWriter } from './create-writer.js';
|
|
4
|
-
export declare namespace staticPages {
|
|
5
|
-
interface Route<F = unknown, T = unknown> {
|
|
6
|
-
from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
|
|
7
|
-
to?: {
|
|
8
|
-
(data: T): MaybePromise<void>;
|
|
9
|
-
} | createWriter.Options<T>;
|
|
10
|
-
controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export declare function staticPages<F1, T1>(...route: [
|
|
14
|
-
staticPages.Route<F1, T1>
|
|
15
|
-
]): Promise<void>;
|
|
16
|
-
export declare function staticPages<F1, T1, F2, T2>(...route: [
|
|
17
|
-
staticPages.Route<F1, T1>,
|
|
18
|
-
staticPages.Route<F2, T2>
|
|
19
|
-
]): Promise<void>;
|
|
20
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3>(...route: [
|
|
21
|
-
staticPages.Route<F1, T1>,
|
|
22
|
-
staticPages.Route<F2, T2>,
|
|
23
|
-
staticPages.Route<F3, T3>
|
|
24
|
-
]): Promise<void>;
|
|
25
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4>(...route: [
|
|
26
|
-
staticPages.Route<F1, T1>,
|
|
27
|
-
staticPages.Route<F2, T2>,
|
|
28
|
-
staticPages.Route<F3, T3>,
|
|
29
|
-
staticPages.Route<F4, T4>
|
|
30
|
-
]): Promise<void>;
|
|
31
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5>(...route: [
|
|
32
|
-
staticPages.Route<F1, T1>,
|
|
33
|
-
staticPages.Route<F2, T2>,
|
|
34
|
-
staticPages.Route<F3, T3>,
|
|
35
|
-
staticPages.Route<F4, T4>,
|
|
36
|
-
staticPages.Route<F5, T5>
|
|
37
|
-
]): Promise<void>;
|
|
38
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5, F6, T6>(...route: [
|
|
39
|
-
staticPages.Route<F1, T1>,
|
|
40
|
-
staticPages.Route<F2, T2>,
|
|
41
|
-
staticPages.Route<F3, T3>,
|
|
42
|
-
staticPages.Route<F4, T4>,
|
|
43
|
-
staticPages.Route<F5, T5>,
|
|
44
|
-
staticPages.Route<F6, T6>
|
|
45
|
-
]): Promise<void>;
|
|
46
|
-
export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
|
|
47
|
-
export declare namespace staticPages {
|
|
48
|
-
var _a: ({ from, to, controller }: NullablePartialRoute) => typeof staticPages;
|
|
49
|
-
export { _a as with };
|
|
50
|
-
}
|
|
51
|
-
type NullablePartialRoute = {
|
|
52
|
-
[P in keyof staticPages.Route]?: null | staticPages.Route[P];
|
|
53
|
-
};
|
|
54
|
-
export {};
|
package/cjs/static-pages.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.staticPages = void 0;
|
|
4
|
-
const helpers_js_1 = require("./helpers.js");
|
|
5
|
-
const create_reader_js_1 = require("./create-reader.js");
|
|
6
|
-
const create_writer_js_1 = require("./create-writer.js");
|
|
7
|
-
async function staticPages(...routes) {
|
|
8
|
-
for (const route of routes) {
|
|
9
|
-
if (typeof route !== 'object' || !route)
|
|
10
|
-
throw new TypeError(`Expected 'object', recieved '${(0, helpers_js_1.getType)(route)}'.`);
|
|
11
|
-
let { from, to, controller } = route;
|
|
12
|
-
if (create_reader_js_1.createReader.isOptions(from))
|
|
13
|
-
from = (0, create_reader_js_1.createReader)(from);
|
|
14
|
-
if (create_writer_js_1.createWriter.isOptions(to))
|
|
15
|
-
to = (0, create_writer_js_1.createWriter)(to);
|
|
16
|
-
if (!(0, helpers_js_1.isIterable)(from) && !(0, helpers_js_1.isAsyncIterable)(from))
|
|
17
|
-
throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at 'from' property.`);
|
|
18
|
-
if (typeof to !== 'function')
|
|
19
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(to)}' at 'to' property.`);
|
|
20
|
-
if (typeof controller !== 'undefined' && typeof controller !== 'function')
|
|
21
|
-
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(controller)}' at 'controller' property.`);
|
|
22
|
-
for await (const data of from) {
|
|
23
|
-
const finalData = controller ? await controller(data) : data;
|
|
24
|
-
if ((0, helpers_js_1.isIterable)(finalData) || (0, helpers_js_1.isAsyncIterable)(finalData)) {
|
|
25
|
-
for await (const finalDataItem of finalData) {
|
|
26
|
-
await to(finalDataItem);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
else if (typeof finalData !== 'undefined') {
|
|
30
|
-
await to(finalData);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.staticPages = staticPages;
|
|
36
|
-
staticPages.with = ({ from, to, controller }) => {
|
|
37
|
-
const withFunction = (newValue) => staticPages.with({
|
|
38
|
-
from: determineFrom(from, newValue.from),
|
|
39
|
-
to: determineTo(to, newValue.to),
|
|
40
|
-
controller: typeof newValue.controller !== 'undefined' ? newValue.controller : controller,
|
|
41
|
-
});
|
|
42
|
-
function modifiedStaticPages(...routes) {
|
|
43
|
-
return staticPages(...routes.map(route => {
|
|
44
|
-
var _a;
|
|
45
|
-
return ({
|
|
46
|
-
from: determineFrom(from, route.from),
|
|
47
|
-
to: determineTo(to, route.to),
|
|
48
|
-
controller: (_a = (typeof route.controller !== 'undefined' ? route.controller : controller)) !== null && _a !== void 0 ? _a : undefined,
|
|
49
|
-
});
|
|
50
|
-
}));
|
|
51
|
-
}
|
|
52
|
-
modifiedStaticPages.with = withFunction;
|
|
53
|
-
return modifiedStaticPages;
|
|
54
|
-
};
|
|
55
|
-
function determineFrom(oldValue, newValue) {
|
|
56
|
-
if (newValue && typeof newValue === 'object' && !(0, helpers_js_1.isIterable)(newValue) && !(0, helpers_js_1.isAsyncIterable)(newValue) &&
|
|
57
|
-
oldValue && typeof oldValue === 'object' && !(0, helpers_js_1.isIterable)(oldValue) && !(0, helpers_js_1.isAsyncIterable)(oldValue))
|
|
58
|
-
return { ...oldValue, ...newValue };
|
|
59
|
-
return typeof newValue !== 'undefined' ? newValue : oldValue;
|
|
60
|
-
}
|
|
61
|
-
function determineTo(oldValue, newValue) {
|
|
62
|
-
if (newValue && typeof newValue === 'object' &&
|
|
63
|
-
oldValue && typeof oldValue === 'object')
|
|
64
|
-
return { ...oldValue, ...newValue };
|
|
65
|
-
return typeof newValue !== 'undefined' ? newValue : oldValue;
|
|
66
|
-
}
|
package/esm/autoparse.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function autoparse(content: string | Uint8Array, filename: string): any;
|
package/esm/autoparse.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { parse as parseYAML } from 'yaml';
|
|
2
|
-
import parseMarkdown from 'gray-matter';
|
|
3
|
-
export function autoparse(content, filename) {
|
|
4
|
-
const dot = filename.lastIndexOf('.');
|
|
5
|
-
if (dot < 1) {
|
|
6
|
-
throw new Error(`Could not parse document without an extension.`);
|
|
7
|
-
}
|
|
8
|
-
const extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
|
|
9
|
-
let doc;
|
|
10
|
-
switch (extension) {
|
|
11
|
-
case 'json':
|
|
12
|
-
doc = JSON.parse(content.toString());
|
|
13
|
-
break;
|
|
14
|
-
case 'yaml':
|
|
15
|
-
case 'yml':
|
|
16
|
-
doc = parseYAML(content.toString());
|
|
17
|
-
break;
|
|
18
|
-
case 'md':
|
|
19
|
-
case 'markdown':
|
|
20
|
-
const { data, content: markdownContent } = parseMarkdown(content.toString());
|
|
21
|
-
doc = { ...data, content: markdownContent };
|
|
22
|
-
break;
|
|
23
|
-
default:
|
|
24
|
-
throw new Error(`Could not parse document with '${extension}' extension.`);
|
|
25
|
-
}
|
|
26
|
-
if (doc && typeof doc === 'object' && !('url' in doc)) {
|
|
27
|
-
doc.url = filename.substring(0, dot).replace(/\\/g, '/');
|
|
28
|
-
}
|
|
29
|
-
return doc;
|
|
30
|
-
}
|
package/esm/create-reader.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise, Filesystem } from './helpers.js';
|
|
2
|
-
export declare namespace createReader {
|
|
3
|
-
interface Options<T> {
|
|
4
|
-
fs?: Filesystem;
|
|
5
|
-
cwd?: string;
|
|
6
|
-
pattern?: string | string[];
|
|
7
|
-
ignore?: string | string[];
|
|
8
|
-
parse?(content: Uint8Array | string, filename: string): MaybePromise<T>;
|
|
9
|
-
onError?(error: unknown): MaybePromise<void>;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export declare function createReader<T>({ fs, cwd, pattern, ignore, parse, onError, }?: createReader.Options<T>): AsyncGenerator<Awaited<T>, void, unknown>;
|
|
13
|
-
export declare namespace createReader {
|
|
14
|
-
var isOptions: <T>(x: unknown) => x is Options<T>;
|
|
15
|
-
}
|
package/esm/create-reader.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import picomatch from 'picomatch';
|
|
2
|
-
import { autoparse } from './autoparse.js';
|
|
3
|
-
import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
|
|
4
|
-
import nodeFs from 'node:fs';
|
|
5
|
-
export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignore, parse = autoparse, onError = (error) => { throw error; }, } = {}) {
|
|
6
|
-
if (!isFilesystem(fs))
|
|
7
|
-
throw new TypeError(`Expected Node FS compatible implementation at 'fs' property.`);
|
|
8
|
-
if (typeof cwd !== 'string')
|
|
9
|
-
throw new TypeError(`Expected 'string', recieved '${getType(cwd)}' at 'cwd' property.`);
|
|
10
|
-
if (!cwd)
|
|
11
|
-
throw new TypeError(`Expected non-empty string at 'cwd'.`);
|
|
12
|
-
if (typeof pattern !== 'undefined' && typeof pattern !== 'string' && !Array.isArray(pattern))
|
|
13
|
-
throw new TypeError(`Expected 'string' or 'string[]', recieved '${getType(pattern)}' at 'pattern' property.`);
|
|
14
|
-
if (typeof ignore !== 'undefined' && typeof ignore !== 'string' && !Array.isArray(ignore))
|
|
15
|
-
throw new TypeError(`Expected 'string' or 'string[]', recieved '${getType(ignore)}' at 'ignore' property.`);
|
|
16
|
-
if (typeof parse !== 'function')
|
|
17
|
-
throw new TypeError(`Expected 'function', recieved '${getType(parse)}' at 'parse' property.`);
|
|
18
|
-
if (typeof onError !== 'function')
|
|
19
|
-
throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
|
|
20
|
-
let filenames = await new Promise((resolve, reject) => {
|
|
21
|
-
fs.readdir(cwd, { recursive: true, withFileTypes: false, encoding: 'utf8' }, (err, entries) => {
|
|
22
|
-
if (err)
|
|
23
|
-
return reject(err);
|
|
24
|
-
let filtered = [];
|
|
25
|
-
let processed = 0;
|
|
26
|
-
for (const entry of entries) {
|
|
27
|
-
fs.stat(cwd + '/' + entry, (err, stats) => {
|
|
28
|
-
if (err)
|
|
29
|
-
return reject(err);
|
|
30
|
-
if (stats.isFile()) {
|
|
31
|
-
filtered.push(entry);
|
|
32
|
-
}
|
|
33
|
-
if (++processed === entries.length) {
|
|
34
|
-
return resolve(filtered);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
|
|
41
|
-
const filteredFilenames = [];
|
|
42
|
-
const isMatch = picomatch(pattern !== null && pattern !== void 0 ? pattern : '**/*', { ignore });
|
|
43
|
-
for (const filename of filenames) {
|
|
44
|
-
if (isMatch(filename)) {
|
|
45
|
-
filteredFilenames.push(filename);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
filenames = filteredFilenames;
|
|
49
|
-
}
|
|
50
|
-
for (const filename of filenames) {
|
|
51
|
-
try {
|
|
52
|
-
const content = await new Promise((resolve, reject) => {
|
|
53
|
-
fs.readFile(cwd + '/' + filename, (err, data) => {
|
|
54
|
-
if (err)
|
|
55
|
-
reject(err);
|
|
56
|
-
else
|
|
57
|
-
resolve(data);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
yield await parse(content, filename);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
await onError(error);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
createReader.isOptions = (x) => {
|
|
68
|
-
return x == undefined || (!!x && typeof x === 'object' && !isIterable(x) && !isAsyncIterable(x));
|
|
69
|
-
};
|
package/esm/create-writer.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise, Filesystem } from './helpers.js';
|
|
2
|
-
export declare namespace createWriter {
|
|
3
|
-
interface Options<T> {
|
|
4
|
-
fs?: Filesystem;
|
|
5
|
-
cwd?: string;
|
|
6
|
-
name?(data: T): MaybePromise<string>;
|
|
7
|
-
render?(data: T): MaybePromise<Uint8Array | string>;
|
|
8
|
-
onError?(error: unknown): MaybePromise<void>;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (data: T) => Promise<void>;
|
|
12
|
-
export declare namespace createWriter {
|
|
13
|
-
var isOptions: <T>(x: unknown) => x is Options<T>;
|
|
14
|
-
}
|
package/esm/create-writer.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { getType, isFilesystem } from './helpers.js';
|
|
2
|
-
import * as nodeFs from 'node:fs';
|
|
3
|
-
import { dirname } from 'node:path';
|
|
4
|
-
const defaultNamer = (data) => {
|
|
5
|
-
if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
|
|
6
|
-
return data.url.concat('.html');
|
|
7
|
-
}
|
|
8
|
-
throw new Error(`Missing 'url' field in the document.`);
|
|
9
|
-
};
|
|
10
|
-
const defaultRenderer = (data) => {
|
|
11
|
-
if (!!data && typeof data === 'object' && 'content' in data) {
|
|
12
|
-
return '' + data.content;
|
|
13
|
-
}
|
|
14
|
-
throw new Error(`Missing 'content' field in the document.`);
|
|
15
|
-
};
|
|
16
|
-
export function createWriter({ fs = nodeFs, cwd = 'public', name = defaultNamer, render = defaultRenderer, onError = (error) => { throw error; }, } = {}) {
|
|
17
|
-
if (!isFilesystem(fs))
|
|
18
|
-
throw new TypeError(`Expected Node FS compatible implementation at 'fs' property.`);
|
|
19
|
-
if (typeof cwd !== 'string')
|
|
20
|
-
throw new TypeError(`Expected 'string', recieved '${getType(cwd)}' at 'cwd' property.`);
|
|
21
|
-
if (!cwd)
|
|
22
|
-
throw new TypeError(`Expected non-empty string at 'cwd'.`);
|
|
23
|
-
if (typeof render !== 'function')
|
|
24
|
-
throw new TypeError(`Expected 'function', recieved '${getType(render)}' at 'render' property.`);
|
|
25
|
-
if (typeof name !== 'function')
|
|
26
|
-
throw new TypeError(`Expected 'function', recieved '${getType(name)}' at 'name' property.`);
|
|
27
|
-
if (typeof onError !== 'function')
|
|
28
|
-
throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
|
|
29
|
-
return async function (data) {
|
|
30
|
-
try {
|
|
31
|
-
const filepath = cwd + '/' + await name(data);
|
|
32
|
-
const dirpath = dirname(filepath);
|
|
33
|
-
await new Promise((resolve, reject) => {
|
|
34
|
-
fs.stat(dirpath, (err, stats) => {
|
|
35
|
-
if (err) {
|
|
36
|
-
fs.mkdir(dirpath, { recursive: true }, (err) => {
|
|
37
|
-
if (err) {
|
|
38
|
-
reject(err);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
resolve(undefined);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
resolve(undefined);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
const content = await render(data);
|
|
51
|
-
await new Promise((resolve, reject) => {
|
|
52
|
-
fs.writeFile(filepath, content, (err) => {
|
|
53
|
-
if (err)
|
|
54
|
-
reject(err);
|
|
55
|
-
else
|
|
56
|
-
resolve(undefined);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
await onError(error);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
createWriter.isOptions = (x) => {
|
|
66
|
-
return x == undefined || (!!x && typeof x === 'object');
|
|
67
|
-
};
|
package/esm/helpers.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
-
export interface Filesystem {
|
|
3
|
-
stat(path: string | URL, callback: (err: Error | null, stats: {
|
|
4
|
-
isFile(): boolean;
|
|
5
|
-
isDirectory(): boolean;
|
|
6
|
-
}) => void): void;
|
|
7
|
-
readdir(path: string | URL, options: {
|
|
8
|
-
encoding: 'utf8';
|
|
9
|
-
withFileTypes: false;
|
|
10
|
-
recursive: boolean;
|
|
11
|
-
}, callback: (err: Error | null, files: string[]) => void): void;
|
|
12
|
-
mkdir(path: string | URL, options: {
|
|
13
|
-
recursive: true;
|
|
14
|
-
}, callback: (err: Error | null, path?: string) => void): void;
|
|
15
|
-
readFile(path: string | URL, callback: (err: Error | null, data: Uint8Array) => void): void;
|
|
16
|
-
writeFile(path: string | URL, data: string | Uint8Array, callback: (err: Error | null) => void): void;
|
|
17
|
-
}
|
|
18
|
-
export declare const isFilesystem: (x: unknown) => x is Filesystem;
|
|
19
|
-
export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
|
|
20
|
-
export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
|
|
21
|
-
export declare const getType: (x: unknown) => string;
|
package/esm/helpers.js
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export const isFilesystem = (x) => !!x && typeof x === 'object' && 'stat' in x && 'mkdir' in x && 'readFile' in x && 'writeFile' in x;
|
|
2
|
-
export const isIterable = (x) => !!x && typeof x === 'object' && Symbol.iterator in x && typeof x[Symbol.iterator] === 'function';
|
|
3
|
-
export const isAsyncIterable = (x) => !!x && typeof x === 'object' && Symbol.asyncIterator in x && typeof x[Symbol.asyncIterator] === 'function';
|
|
4
|
-
export const getType = (x) => typeof x === 'object' ? (x ? (Array.isArray(x) ? 'array' : 'object') : 'null') : typeof x;
|
package/esm/static-pages.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { MaybePromise } from './helpers.js';
|
|
2
|
-
import { createReader } from './create-reader.js';
|
|
3
|
-
import { createWriter } from './create-writer.js';
|
|
4
|
-
export declare namespace staticPages {
|
|
5
|
-
interface Route<F = unknown, T = unknown> {
|
|
6
|
-
from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
|
|
7
|
-
to?: {
|
|
8
|
-
(data: T): MaybePromise<void>;
|
|
9
|
-
} | createWriter.Options<T>;
|
|
10
|
-
controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export declare function staticPages<F1, T1>(...route: [
|
|
14
|
-
staticPages.Route<F1, T1>
|
|
15
|
-
]): Promise<void>;
|
|
16
|
-
export declare function staticPages<F1, T1, F2, T2>(...route: [
|
|
17
|
-
staticPages.Route<F1, T1>,
|
|
18
|
-
staticPages.Route<F2, T2>
|
|
19
|
-
]): Promise<void>;
|
|
20
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3>(...route: [
|
|
21
|
-
staticPages.Route<F1, T1>,
|
|
22
|
-
staticPages.Route<F2, T2>,
|
|
23
|
-
staticPages.Route<F3, T3>
|
|
24
|
-
]): Promise<void>;
|
|
25
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4>(...route: [
|
|
26
|
-
staticPages.Route<F1, T1>,
|
|
27
|
-
staticPages.Route<F2, T2>,
|
|
28
|
-
staticPages.Route<F3, T3>,
|
|
29
|
-
staticPages.Route<F4, T4>
|
|
30
|
-
]): Promise<void>;
|
|
31
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5>(...route: [
|
|
32
|
-
staticPages.Route<F1, T1>,
|
|
33
|
-
staticPages.Route<F2, T2>,
|
|
34
|
-
staticPages.Route<F3, T3>,
|
|
35
|
-
staticPages.Route<F4, T4>,
|
|
36
|
-
staticPages.Route<F5, T5>
|
|
37
|
-
]): Promise<void>;
|
|
38
|
-
export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5, F6, T6>(...route: [
|
|
39
|
-
staticPages.Route<F1, T1>,
|
|
40
|
-
staticPages.Route<F2, T2>,
|
|
41
|
-
staticPages.Route<F3, T3>,
|
|
42
|
-
staticPages.Route<F4, T4>,
|
|
43
|
-
staticPages.Route<F5, T5>,
|
|
44
|
-
staticPages.Route<F6, T6>
|
|
45
|
-
]): Promise<void>;
|
|
46
|
-
export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
|
|
47
|
-
export declare namespace staticPages {
|
|
48
|
-
var _a: ({ from, to, controller }: NullablePartialRoute) => typeof staticPages;
|
|
49
|
-
export { _a as with };
|
|
50
|
-
}
|
|
51
|
-
type NullablePartialRoute = {
|
|
52
|
-
[P in keyof staticPages.Route]?: null | staticPages.Route[P];
|
|
53
|
-
};
|
|
54
|
-
export {};
|
package/esm/static-pages.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { getType, isIterable, isAsyncIterable } from './helpers.js';
|
|
2
|
-
import { createReader } from './create-reader.js';
|
|
3
|
-
import { createWriter } from './create-writer.js';
|
|
4
|
-
export async function staticPages(...routes) {
|
|
5
|
-
for (const route of routes) {
|
|
6
|
-
if (typeof route !== 'object' || !route)
|
|
7
|
-
throw new TypeError(`Expected 'object', recieved '${getType(route)}'.`);
|
|
8
|
-
let { from, to, controller } = route;
|
|
9
|
-
if (createReader.isOptions(from))
|
|
10
|
-
from = createReader(from);
|
|
11
|
-
if (createWriter.isOptions(to))
|
|
12
|
-
to = createWriter(to);
|
|
13
|
-
if (!isIterable(from) && !isAsyncIterable(from))
|
|
14
|
-
throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at 'from' property.`);
|
|
15
|
-
if (typeof to !== 'function')
|
|
16
|
-
throw new TypeError(`Expected 'function', recieved '${getType(to)}' at 'to' property.`);
|
|
17
|
-
if (typeof controller !== 'undefined' && typeof controller !== 'function')
|
|
18
|
-
throw new TypeError(`Expected 'function', recieved '${getType(controller)}' at 'controller' property.`);
|
|
19
|
-
for await (const data of from) {
|
|
20
|
-
const finalData = controller ? await controller(data) : data;
|
|
21
|
-
if (isIterable(finalData) || isAsyncIterable(finalData)) {
|
|
22
|
-
for await (const finalDataItem of finalData) {
|
|
23
|
-
await to(finalDataItem);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
else if (typeof finalData !== 'undefined') {
|
|
27
|
-
await to(finalData);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
staticPages.with = ({ from, to, controller }) => {
|
|
33
|
-
const withFunction = (newValue) => staticPages.with({
|
|
34
|
-
from: determineFrom(from, newValue.from),
|
|
35
|
-
to: determineTo(to, newValue.to),
|
|
36
|
-
controller: typeof newValue.controller !== 'undefined' ? newValue.controller : controller,
|
|
37
|
-
});
|
|
38
|
-
function modifiedStaticPages(...routes) {
|
|
39
|
-
return staticPages(...routes.map(route => {
|
|
40
|
-
var _a;
|
|
41
|
-
return ({
|
|
42
|
-
from: determineFrom(from, route.from),
|
|
43
|
-
to: determineTo(to, route.to),
|
|
44
|
-
controller: (_a = (typeof route.controller !== 'undefined' ? route.controller : controller)) !== null && _a !== void 0 ? _a : undefined,
|
|
45
|
-
});
|
|
46
|
-
}));
|
|
47
|
-
}
|
|
48
|
-
modifiedStaticPages.with = withFunction;
|
|
49
|
-
return modifiedStaticPages;
|
|
50
|
-
};
|
|
51
|
-
function determineFrom(oldValue, newValue) {
|
|
52
|
-
if (newValue && typeof newValue === 'object' && !isIterable(newValue) && !isAsyncIterable(newValue) &&
|
|
53
|
-
oldValue && typeof oldValue === 'object' && !isIterable(oldValue) && !isAsyncIterable(oldValue))
|
|
54
|
-
return { ...oldValue, ...newValue };
|
|
55
|
-
return typeof newValue !== 'undefined' ? newValue : oldValue;
|
|
56
|
-
}
|
|
57
|
-
function determineTo(oldValue, newValue) {
|
|
58
|
-
if (newValue && typeof newValue === 'object' &&
|
|
59
|
-
oldValue && typeof oldValue === 'object')
|
|
60
|
-
return { ...oldValue, ...newValue };
|
|
61
|
-
return typeof newValue !== 'undefined' ? newValue : oldValue;
|
|
62
|
-
}
|