Skip to content

Commit 4193298

Browse files
Villanuevandjorgeucano
authored andcommitted
docs(scully): add registerPlugin explanation (#208)
#127
1 parent 78da93b commit 4193298

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

docs/plugins.md

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,69 @@
33
Scully uses a plugin system to allow users to define new ways for Scully to pre-render your app. There are three main
44
types of plugins:
55

6+
1. [Register Plugin](#register-plugin)
67
1. [Router Plugins](#router-plugin)
7-
2. [Render Plugins](#render-plugin)
8-
3. [File Handler Plugins](#file-plugin)
8+
1. [Render Plugins](#render-plugin)
9+
1. [File Handler Plugins](#file-plugin)
910

1011
See our [Recommended Plugins](recommended-plugins.md) page to find a list of available plugins.
1112

1213
---
1314

15+
## Register Plugin
16+
17+
The `registerPlugin` is the method created for add new plugins to scully. This method has 4 parameters:
18+
19+
- type
20+
- name
21+
- plugin
22+
- validator
23+
24+
### type: PluginTypes
25+
26+
`type` is a reference the to the type of plugin. It could be `render`, `router` or `fileHandler`.
27+
28+
### name: string
29+
30+
`name` is a reference to the name of the plugin.
31+
32+
### plugin: any
33+
34+
`plugin` is a reference to the plugin function.
35+
36+
### validator: function
37+
38+
`validator` is a reference to the validations function. It should return an array of errors.
39+
40+
> Cool tip: you can add color to the validator errors using the colors inside the `log.ts` file.
41+
42+
##### Example
43+
44+
```typescript
45+
import {yellow} from '@scullyio/scully/utils/log';
46+
47+
// Omited code ...
48+
49+
const validator = async options => {
50+
const errors = [];
51+
52+
if (options.numberOfPages && typeof options.numberOfPages !== 'number') {
53+
errors.push(
54+
`my-custom-pluging plugin numberOfPages should be a number, not a ${yellow(
55+
typeof options.numberOfPages
56+
)}`
57+
);
58+
}
59+
60+
return errors;
61+
};
62+
```
63+
64+
### IMPORTANT
65+
66+
Scully plugins are files with `.js` extension, which should be exported and used in `scully.config.js`
67+
file using the `require()` method.
68+
1469
## <a name="router-plugin"></a> Router Plugins
1570

1671
### <a name="router-plugin-what-is"></a> What is a Router Plugin?
@@ -105,7 +160,9 @@ The `HandledRoute[]` gets added into the `scully-routes.json` that is generated
105160
In our previous example of an app with the route `/user/:userId` where we have five distinct userIds, here is a **router
106161
plugin** that would turn the raw route into five distinct HandledRoutes.
107162

108-
```typescript
163+
```javascript
164+
const {registerPlugin} = require('@scullyio/scully');
165+
109166
function userIdPlugin(route: string, config = {}): Promise<HandledRoute[]> {
110167
return Promise.resolve([
111168
{route: '/user/1'},
@@ -116,7 +173,8 @@ function userIdPlugin(route: string, config = {}): Promise<HandledRoute[]> {
116173
]);
117174
}
118175
// DON'T FORGET THIS STEP
119-
registerPlugin('router', 'userIds', userIdPlugin);
176+
const validator = async conf => [];
177+
registerPlugin('router', 'userIds', userIdPlugin, validator);
120178
```
121179

122180
Once we have built a plugin, we can configure our `scully.config.js` to use our plugin.
@@ -219,6 +277,8 @@ function exampleContentPlugin(HTML: string, route: HandledRoute): Promise<string
219277
The following is a sample **render plugin** that adds a title to the head of a page if it doesn't find one.
220278

221279
```typescript
280+
const {registerPlugin} = require('@scullyio/scully');
281+
222282
function defaultTitlePlugin(html, route) {
223283
// If no title in the document
224284
if (html.indexOf('<title') < 0) {
@@ -230,7 +290,10 @@ function defaultTitlePlugin(html, route) {
230290
return Promise.resolve(html);
231291
}
232292
// DON'T FORGET THIS STEP
233-
registerPlugin('render', 'defaultTitle', defaultTitlePlugin);
293+
const validator = async conf => [];
294+
registerPlugin('render', 'defaultTitle', defaultTitlePlugin, validator);
295+
296+
module.exports.defaultTitlePlugin = defaultTitlePlugin;
234297
```
235298

236299
In this example, the HTML that the Angular app rendered is transformed to include a title (if one wasn't found). This
@@ -239,11 +302,16 @@ is the primary function of a render plugin.
239302
Here is another example that would replace any instances of `:)` with a smiley emoji:
240303

241304
```typescript
305+
const {registerPlugin} = require('@scullyio/scully');
306+
242307
function smileEmojiPlugin(html, route) {
243308
return Promise.resolve(html.replace(/\:\)/g, '😊'));
244309
}
245310
// This registers your plugin as
246-
registerPlugin('render', 'smiles', smileEmojiPlugin);
311+
const validator = async conf => [];
312+
registerPlugin('render', 'smiles', smileEmojiPlugin, validator);
313+
314+
module.exports.smileEmojiPlugin = smileEmojiPlugin;
247315
```
248316

249317
### <a name="render-plugin-configure"></a> Render Plugin Examples
@@ -295,6 +363,8 @@ function csvFilePlugin(raw) {
295363
}
296364
// This registers your plugin
297365
registerPlugin('fileHandler', 'csv', {handler: csvFilePlugin});
366+
367+
module.exports.csvFilePlugin = csvFilePlugin;
298368
```
299369

300370
### <a name="file-plugin-configure"></a> File Handler Plugin Examples

0 commit comments

Comments
 (0)