@sphido/core 2.0.9 → 2.0.12

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/readme.md +37 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sphido/core",
3
- "version": "2.0.9",
3
+ "version": "2.0.12",
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": "5ede811c6442f334f5dd04f0d146b49fc7a785ad"
48
+ "gitHead": "77075be0178da85fc883a6232fb5d4d569ccd9d3"
49
49
  }
package/readme.md CHANGED
@@ -41,12 +41,12 @@ for (const page of allPages(pages)) {
41
41
  }
42
42
  ```
43
43
 
44
- ## Extend
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
- ### *callback* extenders
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
- ### *object* extenders
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
- // callback extender will be called during iteration
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
- function getHtml({name, content, path}) {
133
- return `<!DOCTYPE html>
134
- <html lang="cs" dir="ltr">
135
- <head>
136
- <meta charset="UTF-8">
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', dirname(page.path)), slugify(page.name) + '.html');
153
+ page.output = join('public', relative('content', page.dir), page.slug);
149
154
  page.content = marked(await readFile(page.path));
150
- await writeFile(page.output, getHtml(page));
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)