@static-pages/core 2.0.1 → 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/README.md +6 -21
- package/cjs/index.d.ts +2 -3
- package/cjs/index.js +6 -3
- package/esm/index.d.ts +2 -3
- package/esm/index.js +4 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Yes! Because I browsed the whole jamstack scene, but could not find one which
|
|
|
15
15
|
3. can render with any template engine (Twig, ejs, Pug, Mustache etc.)
|
|
16
16
|
4. supports incremental builds
|
|
17
17
|
5. has a flexible CLI tool (see [@static-pages/cli](https://www.npmjs.com/package/@static-pages/cli) on npm)
|
|
18
|
-
6. has a Docker image (see [
|
|
18
|
+
6. has a Docker image (see [staticpages/cli](https://hub.docker.com/repository/docker/staticpages/cli) on dockerhub)
|
|
19
19
|
7. written in JS (preferably TypeScript)
|
|
20
20
|
8. easy to extend with JS code
|
|
21
21
|
9. learning and using is easy (Gatsby, Hugo, Jekyll, Eleventy etc. are so cool but harder to learn and configure)
|
|
@@ -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({
|
|
@@ -84,22 +84,7 @@ type Data = Record<string, unknown>;
|
|
|
84
84
|
type Data = { [k: string]: unknown };
|
|
85
85
|
```
|
|
86
86
|
|
|
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.
|
|
90
|
-
|
|
91
|
-
```js
|
|
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
|
-
```
|
|
87
|
+
> When `Options` is not passed as an array, it is wrapped into an array.
|
|
103
88
|
|
|
104
89
|
## Missing a feature?
|
|
105
90
|
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
|
-
declare const
|
|
10
|
-
export default
|
|
8
|
+
export declare const staticPages: (routes: Route | Route[]) => Promise<void>;
|
|
9
|
+
export default staticPages;
|
package/cjs/index.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.staticPages = void 0;
|
|
3
4
|
const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
|
|
4
5
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
6
|
const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
|
|
6
7
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
8
|
const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
|
|
8
|
-
|
|
9
|
+
const staticPages = async (routes) => {
|
|
9
10
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
10
11
|
if (typeof route !== 'object' || !route)
|
|
11
12
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
12
|
-
const { from, to, controller
|
|
13
|
+
const { from, to, controller } = route;
|
|
13
14
|
if (!isIterable(from) && !isAsyncIterable(from))
|
|
14
15
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
15
16
|
if (typeof to !== 'function')
|
|
@@ -18,7 +19,7 @@ exports.default = async (routes) => {
|
|
|
18
19
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
19
20
|
const isController = typeof controller === 'function';
|
|
20
21
|
for await (const data of from) {
|
|
21
|
-
const results = isController ? await controller
|
|
22
|
+
const results = isController ? await controller(data) : data;
|
|
22
23
|
if (typeof results === 'object' && results) {
|
|
23
24
|
if (Array.isArray(results)) {
|
|
24
25
|
for (const result of results) {
|
|
@@ -32,3 +33,5 @@ exports.default = async (routes) => {
|
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
};
|
|
36
|
+
exports.staticPages = staticPages;
|
|
37
|
+
exports.default = exports.staticPages;
|
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
|
-
declare const
|
|
10
|
-
export default
|
|
8
|
+
export declare const staticPages: (routes: Route | Route[]) => Promise<void>;
|
|
9
|
+
export default staticPages;
|
package/esm/index.js
CHANGED
|
@@ -3,11 +3,11 @@ const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof
|
|
|
3
3
|
const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
5
|
const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
|
|
6
|
-
export
|
|
6
|
+
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 default 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) {
|
|
@@ -30,3 +30,4 @@ export default async (routes) => {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
+
export default staticPages;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@static-pages/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "General purpose static pages renderer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./cjs/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"homepage": "https://staticpagesjs.github.io/",
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@types/node": "^
|
|
48
|
+
"@types/node": "^14.18.9",
|
|
49
49
|
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
|
50
50
|
"@typescript-eslint/parser": "^5.3.0",
|
|
51
51
|
"coveralls": "^3.1.1",
|