Skip to content

Commit 92ee523

Browse files
author
Sethen Maleno
committed
Added documentation for custom tags, array for custom tags in markdown-include
1 parent 21403f4 commit 92ee523

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* [`stripTag`](#striptag)
2525
* [`stripTagsInFile`](#striptagsinfile)
2626
* [`writeFile`](#writefile)
27+
* [How To Make Custom Tags](#how-to-make-custom-tags)
28+
* [Tutorial](#tutorial)
2729
* [How It Works](#how-it-works)
2830

2931

@@ -537,6 +539,55 @@ markdownInclude.writeFile('contents').then(function (data) {
537539
```
538540

539541
---
542+
# How To Make Custom Tags
543+
544+
Custom tags are now supported as of 0.3.2 of markdown-include. Adding custom tags to your documentation is quite easy to do.
545+
546+
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.
547+
548+
## Tutorial
549+
550+
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.
551+
552+
First, require markdown-include:
553+
554+
```javascript
555+
var markdownInclude = require('markdown-include');
556+
```
557+
558+
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:
559+
560+
```javascript
561+
var markdownInclude = require('markdown-include');
562+
markdownInclude.customTags.push({
563+
pattern: /^#.+ !myTag/gm,
564+
replacement: 'myString!'
565+
});
566+
```
567+
568+
`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:
569+
570+
```javascript
571+
var markdownInclude = require('markdown-include');
572+
markdownInclude.customTags.push({
573+
pattern: /^#.+ !myTag/gm,
574+
replacement: function (tag) {
575+
// do something with tag...
576+
}
577+
});
578+
```
579+
580+
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.
581+
582+
After the tag and it's replacement is registed, it's business as usual:
583+
584+
```javascript
585+
markdownInclude.compileFiles('../path/to/markdown.json').then(function () {
586+
// do something after compiling
587+
});
588+
```
589+
590+
540591
# How It Works
541592

542593
markdown-include works by recursively going through files based on the tags that are found. For instance, consider the following in a `_README.md` file:

docs/_README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +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"
78
#include "docs/how_it_works.md"

docs/how_to_make_custom_tags.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# How To Make Custom Tags !heading
2+
3+
Custom tags are now supported as of 0.3.2 of markdown-include. Adding custom tags to your documentation is quite easy to do.
4+
5+
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.
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`. We need to register the custom tag with markdown-include in it's `customTags` array.
10+
11+
First, require markdown-include:
12+
13+
```javascript
14+
var markdownInclude = require('markdown-include');
15+
```
16+
17+
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:
18+
19+
```javascript
20+
var markdownInclude = require('markdown-include');
21+
markdownInclude.customTags.push({
22+
pattern: /^#.+ !myTag/gm,
23+
replacement: 'myString!'
24+
});
25+
```
26+
27+
`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:
28+
29+
```javascript
30+
var markdownInclude = require('markdown-include');
31+
markdownInclude.customTags.push({
32+
pattern: /^#.+ !myTag/gm,
33+
replacement: function (tag) {
34+
// do something with tag...
35+
}
36+
});
37+
```
38+
39+
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.
40+
41+
After the tag and it's replacement is registed, it's business as usual:
42+
43+
```javascript
44+
markdownInclude.compileFiles('../path/to/markdown.json').then(function () {
45+
// do something after compiling
46+
});
47+
```
48+

markdown-include.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ this.ignorePattern = new RegExp('^#include\\s"(.+\\/|\\/|\\w|-|\\/)+.md"' + this
1717
this.headingPattern = new RegExp('^#+\\s.+' + this.headingTag, 'gm');
1818
this.tableOfContents = '';
1919
this.build = {};
20+
this.customTags = [];
2021

2122
/**
2223
* Build content item for navigation

0 commit comments

Comments
 (0)