|
24 | 24 | * [`stripTag`](#striptag)
|
25 | 25 | * [`stripTagsInFile`](#striptagsinfile)
|
26 | 26 | * [`writeFile`](#writefile)
|
| 27 | +* [How To Make Custom Tags](#how-to-make-custom-tags) |
| 28 | + * [Tutorial](#tutorial) |
27 | 29 | * [How It Works](#how-it-works)
|
28 | 30 |
|
29 | 31 |
|
@@ -537,6 +539,55 @@ markdownInclude.writeFile('contents').then(function (data) {
|
537 | 539 | ```
|
538 | 540 |
|
539 | 541 | ---
|
| 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 | + |
540 | 591 | # How It Works
|
541 | 592 |
|
542 | 593 | 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:
|
|
0 commit comments