@static-pages/core 2.1.0 → 3.0.2
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/README.md +20 -34
- package/cjs/index.d.ts +0 -1
- package/cjs/index.js +2 -2
- package/esm/index.d.ts +0 -1
- package/esm/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,10 +32,10 @@ This project targets small and medium sized projects. The rendering process trie
|
|
|
32
32
|
__Readers__ provides an iterable list of page data. __Controllers__ can manipulate and extend each data object. __Writers__ render the final output for you.
|
|
33
33
|
|
|
34
34
|
```js
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
import staticPages from '@static-pages/core';
|
|
36
|
+
import markdownReader from '@static-pages/markdown-reader';
|
|
37
|
+
import yamlReader from '@static-pages/yaml-reader';
|
|
38
|
+
import twigWriter from '@static-pages/twig-writer';
|
|
39
39
|
|
|
40
40
|
staticPages([{
|
|
41
41
|
from: markdownReader({
|
|
@@ -46,11 +46,11 @@ staticPages([{
|
|
|
46
46
|
viewsDir: "path/to/views/folder",
|
|
47
47
|
outDir: "path/to/output/folder",
|
|
48
48
|
}),
|
|
49
|
-
controller
|
|
49
|
+
controller(data) {
|
|
50
50
|
data.timestamp = new Date().toJSON(); // adds a "timestamp" variable
|
|
51
51
|
return data; // returning the data is required if you want to send it to the renderer
|
|
52
52
|
}
|
|
53
|
-
},{
|
|
53
|
+
}, {
|
|
54
54
|
from: yamlReader({ // assume we have the home page data in yaml format.
|
|
55
55
|
pattern: "home/*.yaml" // <-- reads home/en.yaml, home/de.yaml, home/fr.yaml etc.
|
|
56
56
|
}),
|
|
@@ -59,47 +59,33 @@ staticPages([{
|
|
|
59
59
|
viewsDir: "path/to/views/folder",
|
|
60
60
|
outDir: "path/to/output/folder",
|
|
61
61
|
}),
|
|
62
|
-
controller
|
|
62
|
+
controller(data) {
|
|
63
63
|
data.commitHash = yourGetCommitHashFn();
|
|
64
64
|
return data;
|
|
65
65
|
}
|
|
66
|
-
}])
|
|
66
|
+
}])
|
|
67
|
+
.catch(error => {
|
|
68
|
+
console.error('Error:', error);
|
|
69
|
+
console.error(error.stack);
|
|
70
|
+
});
|
|
67
71
|
```
|
|
68
72
|
|
|
69
|
-
##
|
|
70
|
-
The
|
|
71
|
-
Each item should
|
|
73
|
+
## `staticPages(routes: Route | Route[])`
|
|
74
|
+
The function expects the `routes` parameter to be an array. It is wrapped into an array automatically when other type is provided.
|
|
75
|
+
Each item in this array should be an object containing a `from`, `to` and optionally a `controller` property matching the definition below.
|
|
72
76
|
|
|
73
77
|
```ts
|
|
74
|
-
type
|
|
78
|
+
type Data = Record<string, unknown>;
|
|
79
|
+
type Route = {
|
|
75
80
|
from: Iterable<Data> | AsyncIterable<Data>;
|
|
76
81
|
to: (data: Data) => void | Promise<void>;
|
|
77
82
|
controller?: (data: Data) => undefined | Data | Data[] | Promise<undefined | Data | Data[]>;
|
|
78
|
-
|
|
79
|
-
}[];
|
|
80
|
-
|
|
81
|
-
// Where `Data` is:
|
|
82
|
-
type Data = Record<string, unknown>;
|
|
83
|
-
// Or the same definition in a more known form:
|
|
84
|
-
type Data = { [k: string]: unknown };
|
|
83
|
+
};
|
|
85
84
|
```
|
|
86
85
|
|
|
87
|
-
|
|
88
|
-
You can pass additional configuration options to your controller under the `variables` property.
|
|
89
|
-
These variables are accessible through the `this` context variable of the controller.
|
|
86
|
+
> Tip: Controllers may return an array of `Data` objects. Each data object will be rendered as a separate page.
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
require("@static-pages/core").default([{
|
|
93
|
-
from: ...,
|
|
94
|
-
to: ...,
|
|
95
|
-
controller: function(data) {
|
|
96
|
-
this.myProp; // <-- 123
|
|
97
|
-
},
|
|
98
|
-
variables: {
|
|
99
|
-
myProp: 123,
|
|
100
|
-
},
|
|
101
|
-
}]);
|
|
102
|
-
```
|
|
88
|
+
> Tip: Controllers may return `undefined` to prevent the rendering of the current data object.
|
|
103
89
|
|
|
104
90
|
## Missing a feature?
|
|
105
91
|
Create an issue describing your needs. If it fits the scope of the project I will implement it or you can implement it your own and submit a pull request.
|
package/cjs/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export declare type Route = {
|
|
|
4
4
|
from: Iterable<Data> | AsyncIterable<Data>;
|
|
5
5
|
to: (data: Data) => void | Promise<void>;
|
|
6
6
|
controller?: Controller;
|
|
7
|
-
variables?: Record<string, unknown>;
|
|
8
7
|
};
|
|
9
8
|
export declare const staticPages: (routes: Route | Route[]) => Promise<void>;
|
|
10
9
|
export default staticPages;
|
package/cjs/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const staticPages = async (routes) => {
|
|
|
10
10
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
11
11
|
if (typeof route !== 'object' || !route)
|
|
12
12
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
13
|
-
const { from, to, controller
|
|
13
|
+
const { from, to, controller } = route;
|
|
14
14
|
if (!isIterable(from) && !isAsyncIterable(from))
|
|
15
15
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
16
16
|
if (typeof to !== 'function')
|
|
@@ -19,7 +19,7 @@ const staticPages = async (routes) => {
|
|
|
19
19
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
20
20
|
const isController = typeof controller === 'function';
|
|
21
21
|
for await (const data of from) {
|
|
22
|
-
const results = isController ? await controller
|
|
22
|
+
const results = isController ? await controller(data) : data;
|
|
23
23
|
if (typeof results === 'object' && results) {
|
|
24
24
|
if (Array.isArray(results)) {
|
|
25
25
|
for (const result of results) {
|
package/esm/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export declare type Route = {
|
|
|
4
4
|
from: Iterable<Data> | AsyncIterable<Data>;
|
|
5
5
|
to: (data: Data) => void | Promise<void>;
|
|
6
6
|
controller?: Controller;
|
|
7
|
-
variables?: Record<string, unknown>;
|
|
8
7
|
};
|
|
9
8
|
export declare const staticPages: (routes: Route | Route[]) => Promise<void>;
|
|
10
9
|
export default staticPages;
|
package/esm/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export const staticPages = async (routes) => {
|
|
|
7
7
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
8
8
|
if (typeof route !== 'object' || !route)
|
|
9
9
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
10
|
-
const { from, to, controller
|
|
10
|
+
const { from, to, controller } = route;
|
|
11
11
|
if (!isIterable(from) && !isAsyncIterable(from))
|
|
12
12
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
13
13
|
if (typeof to !== 'function')
|
|
@@ -16,7 +16,7 @@ export const staticPages = async (routes) => {
|
|
|
16
16
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
17
17
|
const isController = typeof controller === 'function';
|
|
18
18
|
for await (const data of from) {
|
|
19
|
-
const results = isController ? await controller
|
|
19
|
+
const results = isController ? await controller(data) : data;
|
|
20
20
|
if (typeof results === 'object' && results) {
|
|
21
21
|
if (Array.isArray(results)) {
|
|
22
22
|
for (const result of results) {
|