Skip to content

Commit ced05ad

Browse files
committed
Make PR fixes
1 parent 7dfb7d9 commit ced05ad

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

docs/userGuide/usingPlugins.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
A plugin is a user defined extension that can add custom features to MarkBind. Markbind plugins are `js` scripts that are loaded and run during the page generation.
1313

14+
**WARNING:** Plugins are executable programs. This means that they might contain malicious code that may damage your computer.
15+
16+
Only run plugins from people that you trust. Do not run the plugin if the source/origin of the plugin cannot be ascertained.
17+
1418
### Adding Plugins
1519

16-
Plugins are stored in the `_markbind/plugins` folder which is generated on `init`. To use a plugin, place the `js` file in the `_markbind/plugins` folder and add the following options to the site configuration:
20+
Plugins are stored in the `_markbind/plugins` folder which is generated on `init`. To use a plugin, place the `js` source of the plugin in the `_markbind/plugins` folder and add the following options to `site.json`:
1721

1822
- `plugins`: An array of plugin names to use
19-
- `context`: An object mapping plugin name to data provided to the plugin. Each plugin can have its own custom data.
23+
- `context`: An object containing lists of parameters passed to each individual plugin.
2024

2125
For example:
2226

@@ -42,8 +46,8 @@ MarkBind plugins allow the user to mutate the page data (`html`) during the page
4246

4347
MarkBind provides two entry points for modifying the page:
4448

45-
- Prerender: Called before any markbind rendering is done on the site. Markbind rendering refers to the conversion of markdown to valid HTML.
46-
- Postrender: Called after all markbind rendering is done on the site.
49+
- Prerender: Called before MarkBind converts/renders the source from markdown to HTML.
50+
- Postrender: Called after the HTML is rendered.
4751

4852
These are controlled by specifying the `preRender` and `postRender` functions in the plugin `js`. Each function takes in two parameters:
4953

@@ -52,9 +56,9 @@ These are controlled by specifying the `preRender` and `postRender` functions in
5256
- For `postRender`, `content` will be the compiled `.html` file.
5357
- `siteContext`: Any other data to be provided to the plugin. This can be specified in the `site.json`.
5458

55-
Markbind will call these two functions with the respective content, and retrieve a string data that is used for the next step of the compilation process.
59+
MarkBind will call these two functions with the respective content, and retrieve a string data that is used for the next step of the compilation process.
5660

57-
A plugin will typically follow a format similar to this one:
61+
An example of a plugin is shown below. The plugin appends a paragraph of text to a specific div in the markdown files:
5862

5963
```js
6064
// myPlugin.js
@@ -80,7 +84,7 @@ module.exports = {
8084
"myPlugin"
8185
],
8286
"context" : {
83-
"plugin1" : {
87+
"myPlugin" : {
8488
"post" : "<p>Hello</p>"
8589
}
8690
}
@@ -94,6 +98,4 @@ module.exports = {
9498
<div id="my-div"></div>
9599
```
96100

97-
The above plugin appends a paragraph of text to a div in the `index.md`.
98-
99101
</div>

lib/Site.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,13 @@ Site.prototype.collectPlugins = function () {
585585
return;
586586
}
587587
this.siteConfig.plugins.forEach((plugin) => {
588+
const pluginPath = path.join(this.rootPath, PLUGIN_FOLDER_PATH, `${plugin}.js`);
589+
if (!fs.existsSync(pluginPath)) {
590+
logger.warn(`Unable to load plugin ${plugin}, skipping`);
591+
return;
592+
}
588593
// eslint-disable-next-line global-require, import/no-dynamic-require
589-
this.loadedPlugins[plugin] = require(path.join(this.rootPath, PLUGIN_FOLDER_PATH, `${plugin}.js`));
594+
this.loadedPlugins[plugin] = require(pluginPath);
590595
});
591596
};
592597

test/test_site/expected/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ <h2 id="panel-content-inside-unexpanded-panel-should-not-appear-in-search-data">
297297
</panel>
298298
<h1 id="div-for-plugins">Div for plugins<a class="fa fa-anchor" href="#div-for-plugins"></a></h1>
299299
<div id="test-plugins">
300-
<p>Prerender</p>
300+
<h2>Prerender<a class="fa fa-anchor" href="#undefined"></a></h2>
301301
<p>Postrender</p>
302302
</div>
303303
</div>

test/test_site/expected/siteData.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"panel-with-src-from-another-markbind-site": "Panel with src from another Markbind site",
5656
"modal-with-panel-inside": "Modal with panel inside",
5757
"unexpanded-panel": "Unexpanded panel",
58-
"div-for-plugins": "Div for plugins"
58+
"div-for-plugins": "Div for plugins",
59+
"undefined": "Prerender"
5960
},
6061
"title": "Hello World",
6162
"footer": "footer.md",

test/test_site/site.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
],
4141
"context" : {
4242
"testPlugin" : {
43-
"pre": "<p>Prerender</p>",
43+
"pre": "<h2>Prerender</h2>",
4444
"post": "<p>Postrender</p>"
4545
}
4646
}

0 commit comments

Comments
 (0)