Skip to content

Commit a277f09

Browse files
author
Sethen Maleno
committed
Made registerPlugin for making plugins, fixed documentation to show right info
1 parent b8259cd commit a277f09

File tree

8 files changed

+104
-62
lines changed

8 files changed

+104
-62
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* [`stripTag`](#striptag)
2525
* [`stripTagsInFile`](#striptagsinfile)
2626
* [`writeFile`](#writefile)
27-
* [How To Make Custom Tags](#how-to-make-custom-tags)
27+
* [How To Make Plugins](#how-to-make-plugins)
2828
* [Tutorial](#tutorial)
2929
* [How It Works](#how-it-works)
3030

@@ -76,6 +76,8 @@ For each heading that you would like to be included in a table of contents just
7676

7777
markdown-include is available on npm for easy installation:
7878

79+
fuck!
80+
7981
```
8082
npm install markdown-include
8183
```
@@ -541,45 +543,48 @@ markdownInclude.writeFile('contents').then(function (data) {
541543
```
542544

543545
---
544-
# How To Make Custom Tags
546+
# How To Make Plugins
545547

546-
Custom tags are now supported as of 0.4.0 of markdown-include. Adding custom tags to your documentation is quite easy to do.
548+
Plugins are now supported as of 0.4.0 of markdown-include. Adding plugins to markdown-include to facilitate the transformation of custom tags is quite trivial.
547549

548-
Custom tags can only be used when markdown-include is being required as a module. If you wish to make this available via the command line, you must require markdown-include in a node module and call it from the command line.
550+
Plugins are best used when markdown-include is being required as a module. If you wish to make this available via the command line, you must require markdown-include in a node module and call it from the command line.
549551

550552
## Tutorial
551553

552-
Let's pretend we want to add a custom tag called `!myTag` that follows the pattern of `#phrase !myTag`. We need to register the custom tag with markdown-include in it's `customTags` array.
554+
Let's pretend we want to add a custom tag called `!myTag` that follows the pattern of `#phrase !myTag`. All we need to do is register the plugin with markdown-include
553555

554556
First, require markdown-include:
555557

556558
```javascript
557559
var markdownInclude = require('markdown-include');
558560
```
559561

560-
Second, register your tag with your desired replacement. You can replace your tag with either another string or use a function to do your desired work. This is done with objects added to an array, like so:
562+
Second, register your plugin with with your desired pattern to match and desired replacement. You can replace your tag with another string to do your desired work:
561563

562564
```javascript
563565
var markdownInclude = require('markdown-include');
564-
markdownInclude.customTags.push({
566+
markdownInclude.registerPlugin({
565567
pattern: /^#.+ !myTag/gm,
566-
replacement: 'myString!'
568+
replace: 'myString!'
567569
});
568570
```
569571

570-
`pattern` is the regular expression that should be looked for. `replacement` is your desired replacement for the tag once it's found. In the example above, we're just replacing our tag with a string. If you would rather use a function, you can do this like so:
572+
In the example above, we're just replacing our tag with a string. If you would rather use a function, you can do this like so (you must return a value to replace with):
571573

572574
```javascript
573575
var markdownInclude = require('markdown-include');
574-
markdownInclude.customTags.push({
576+
markdownInclude.registerPlugin({
575577
pattern: /^#.+ !myTag/gm,
576-
replacement: function (tag) {
578+
replace: function (tag) {
577579
// do something with tag...
580+
return 'myString!'
578581
}
579582
});
580583
```
581584

582-
This gives you free range to do whatever you want with the tag in question. Once the tag is encountered markdown-include will run the function.
585+
`pattern` is the regular expression that should be looked for. `replace` is your desired replacement for the tag once it's found.
586+
587+
This gives you free range to do whatever you want with the tag you want to replace. Once the tag is encountered markdown-include will run the function.
583588

584589
After the tag and it's replacement is registed, it's business as usual:
585590

@@ -589,6 +594,14 @@ markdownInclude.compileFiles('../path/to/markdown.json').then(function () {
589594
});
590595
```
591596

597+
You can also use another form of registering a plugins if it fits your coding style better:
598+
599+
```
600+
markdownInclude.registerPlugin(/^#.+ !myTag/gm, function (tag) {
601+
return 'my replacement!';
602+
});
603+
```
604+
592605

593606
# How It Works
594607

bin/cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
var markdownJson = process.argv[2];
77
var markdownInclude = require('../markdown-include');
88

9+
markdownInclude.registerPlugin(/^#.+ !myTag/gm, function () {
10+
return 'fuck!';
11+
});
12+
913
markdownInclude.compileFiles(markdownJson).then(function () {
1014
console.info(markdownInclude.options.build + ' have been built successfully');
1115
});

docs/_README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
#include "docs/markdown-json.md"
55
#include "docs/how_to_use_module.md"
66
#include "docs/api/_README.md"
7-
#include "docs/how_to_make_custom_tags.md"
7+
#include "docs/how_to_make_plugins.md"
88
#include "docs/how_it_works.md"

docs/how_to_install.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
markdown-include is available on npm for easy installation:
44

5+
#something !myTag
6+
57
```
68
npm install markdown-include
79
```

docs/how_to_make_custom_tags.md

Lines changed: 0 additions & 48 deletions
This file was deleted.

docs/how_to_make_plugins.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# How To Make Plugins !heading
2+
3+
Plugins are now supported as of 0.4.0 of markdown-include. Adding plugins to markdown-include to facilitate the transformation of custom tags is quite trivial.
4+
5+
Plugins are best used when markdown-include is being required as a module. If you wish to make this available via the command line, you must require markdown-include in a node module and call it from the command line.
6+
7+
## Tutorial !heading
8+
9+
Let's pretend we want to add a custom tag called `!myTag` that follows the pattern of `#phrase !myTag`. All we need to do is register the plugin with markdown-include
10+
11+
First, require markdown-include:
12+
13+
```javascript
14+
var markdownInclude = require('markdown-include');
15+
```
16+
17+
Second, register your plugin with with your desired pattern to match and desired replacement. You can replace your tag with another string to do your desired work:
18+
19+
```javascript
20+
var markdownInclude = require('markdown-include');
21+
markdownInclude.registerPlugin({
22+
pattern: /^#.+ !myTag/gm,
23+
replace: 'myString!'
24+
});
25+
```
26+
27+
In the example above, we're just replacing our tag with a string. If you would rather use a function, you can do this like so (you must return a value to replace with):
28+
29+
```javascript
30+
var markdownInclude = require('markdown-include');
31+
markdownInclude.registerPlugin({
32+
pattern: /^#.+ !myTag/gm,
33+
replace: function (tag) {
34+
// do something with tag...
35+
return 'myString!'
36+
}
37+
});
38+
```
39+
40+
`pattern` is the regular expression that should be looked for. `replace` is your desired replacement for the tag once it's found.
41+
42+
This gives you free range to do whatever you want with the tag you want to replace. Once the tag is encountered markdown-include will run the function.
43+
44+
After the tag and it's replacement is registed, it's business as usual:
45+
46+
```javascript
47+
markdownInclude.compileFiles('../path/to/markdown.json').then(function () {
48+
// do something after compiling
49+
});
50+
```
51+
52+
You can also use another form of registering a plugins if it fits your coding style better:
53+
54+
```
55+
markdownInclude.registerPlugin(/^#.+ !myTag/gm, function (tag) {
56+
return 'my replacement!';
57+
});
58+
```
59+

markdown-include.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ exports.processIncludeTags = function (file, currentFile, tags) {
305305
return collection;
306306
};
307307

308+
exports.registerPlugin = function () {
309+
if (arguments[0].pattern && arguments[0].replace) {
310+
this.customTags.push(arguments[0]);
311+
}
312+
else {
313+
this.customTags.push({
314+
pattern: arguments[0],
315+
replace: arguments[1]
316+
});
317+
}
318+
};
319+
308320
/**
309321
* Replaces include tags with actual content from files
310322
* @param {String} file File content

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-include",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "Include markdown files into other markdown files with C style syntax.",
55
"main": "markdown-include.js",
66
"repository": {

0 commit comments

Comments
 (0)