You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
13
+
14
+
### Adding Plugins
15
+
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:
17
+
18
+
-`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.
20
+
21
+
For example:
22
+
23
+
```
24
+
{
25
+
...
26
+
"plugins" : [
27
+
"plugin1",
28
+
"plugin2",
29
+
],
30
+
"context" : {
31
+
"plugin1" : "Input Data",
32
+
"plugin2" : ["Input", "Data"]
33
+
}
34
+
}
35
+
```
36
+
37
+
### Writing Plugins
38
+
39
+
MarkBind plugins allow the user to mutate the page data (`html`) during the page compilation process.
MarkBind provides two entry points for modifying the page:
44
+
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.
47
+
48
+
These are controlled by specifying the `preRender` and `postRender` functions in the plugin `js`. Each function takes in two parameters:
49
+
50
+
-`content`: The intermediate content of the page data represented as a string.
51
+
- For `preRender`, `content` will be the raw markdown of a `.md` file.
52
+
- For `postRender`, `content` will be the compiled `.html` file.
53
+
-`siteContext`: Any other data to be provided to the plugin. This can be specified in the `site.json`.
54
+
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.
56
+
57
+
A plugin will typically follow a format similar to this one:
58
+
59
+
```js
60
+
// myPlugin.js
61
+
62
+
constcheerio=module.parent.require('cheerio');
63
+
64
+
module.exports= {
65
+
postRender: (content, siteContext) => {
66
+
const$=cheerio.load(content, { xmlMode:false });
67
+
// Modify the page...
68
+
$('#my-div').append(siteContext["post"]);
69
+
return$.html();
70
+
},
71
+
};
72
+
```
73
+
74
+
```js
75
+
// site.json
76
+
77
+
{
78
+
...
79
+
"plugins": [
80
+
"myPlugin"
81
+
],
82
+
"context": {
83
+
"plugin1": {
84
+
"post":"<p>Hello</p>"
85
+
}
86
+
}
87
+
}
88
+
```
89
+
90
+
```md
91
+
// index.md
92
+
93
+
...
94
+
<div id="my-div"></div>
95
+
```
96
+
97
+
The above plugin appends a paragraph of text to a div in the `index.md`.
<h2id="panel-content-inside-unexpanded-panel-should-not-appear-in-search-data">Panel content inside unexpanded panel should not appear in search data<aclass="fa fa-anchor" href="#panel-content-inside-unexpanded-panel-should-not-appear-in-search-data"></a></h2>
296
296
</panel>
297
297
</panel>
298
+
<h1id="div-for-plugins">Div for plugins<aclass="fa fa-anchor" href="#div-for-plugins"></a></h1>
0 commit comments