Skip to content

Commit

Permalink
Made registerPlugin for making plugins, fixed documentation to show r…
Browse files Browse the repository at this point in the history
…ight info
  • Loading branch information
Sethen Maleno committed Jul 1, 2015
1 parent b8259cd commit a277f09
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 62 deletions.
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* [`stripTag`](#striptag)
* [`stripTagsInFile`](#striptagsinfile)
* [`writeFile`](#writefile)
* [How To Make Custom Tags](#how-to-make-custom-tags)
* [How To Make Plugins](#how-to-make-plugins)
* [Tutorial](#tutorial)
* [How It Works](#how-it-works)

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

markdown-include is available on npm for easy installation:

fuck!

```
npm install markdown-include
```
Expand Down Expand Up @@ -541,45 +543,48 @@ markdownInclude.writeFile('contents').then(function (data) {
```

---
# How To Make Custom Tags
# How To Make Plugins

Custom tags are now supported as of 0.4.0 of markdown-include. Adding custom tags to your documentation is quite easy to do.
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.

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.
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.

## Tutorial

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.
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

First, require markdown-include:

```javascript
var markdownInclude = require('markdown-include');
```

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:
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:

```javascript
var markdownInclude = require('markdown-include');
markdownInclude.customTags.push({
markdownInclude.registerPlugin({
pattern: /^#.+ !myTag/gm,
replacement: 'myString!'
replace: 'myString!'
});
```

`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:
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):

```javascript
var markdownInclude = require('markdown-include');
markdownInclude.customTags.push({
markdownInclude.registerPlugin({
pattern: /^#.+ !myTag/gm,
replacement: function (tag) {
replace: function (tag) {
// do something with tag...
return 'myString!'
}
});
```

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.
`pattern` is the regular expression that should be looked for. `replace` is your desired replacement for the tag once it's found.

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.

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

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

You can also use another form of registering a plugins if it fits your coding style better:

```
markdownInclude.registerPlugin(/^#.+ !myTag/gm, function (tag) {
return 'my replacement!';
});
```


# How It Works

Expand Down
4 changes: 4 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
var markdownJson = process.argv[2];
var markdownInclude = require('../markdown-include');

markdownInclude.registerPlugin(/^#.+ !myTag/gm, function () {
return 'fuck!';
});

markdownInclude.compileFiles(markdownJson).then(function () {
console.info(markdownInclude.options.build + ' have been built successfully');
});
2 changes: 1 addition & 1 deletion docs/_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#include "docs/markdown-json.md"
#include "docs/how_to_use_module.md"
#include "docs/api/_README.md"
#include "docs/how_to_make_custom_tags.md"
#include "docs/how_to_make_plugins.md"
#include "docs/how_it_works.md"
2 changes: 2 additions & 0 deletions docs/how_to_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

markdown-include is available on npm for easy installation:

#something !myTag

```
npm install markdown-include
```
Expand Down
48 changes: 0 additions & 48 deletions docs/how_to_make_custom_tags.md

This file was deleted.

59 changes: 59 additions & 0 deletions docs/how_to_make_plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# How To Make Plugins !heading

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.

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.

## Tutorial !heading

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

First, require markdown-include:

```javascript
var markdownInclude = require('markdown-include');
```

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:

```javascript
var markdownInclude = require('markdown-include');
markdownInclude.registerPlugin({
pattern: /^#.+ !myTag/gm,
replace: 'myString!'
});
```

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):

```javascript
var markdownInclude = require('markdown-include');
markdownInclude.registerPlugin({
pattern: /^#.+ !myTag/gm,
replace: function (tag) {
// do something with tag...
return 'myString!'
}
});
```

`pattern` is the regular expression that should be looked for. `replace` is your desired replacement for the tag once it's found.

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.

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

```javascript
markdownInclude.compileFiles('../path/to/markdown.json').then(function () {
// do something after compiling
});
```

You can also use another form of registering a plugins if it fits your coding style better:

```
markdownInclude.registerPlugin(/^#.+ !myTag/gm, function (tag) {
return 'my replacement!';
});
```

12 changes: 12 additions & 0 deletions markdown-include.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ exports.processIncludeTags = function (file, currentFile, tags) {
return collection;
};

exports.registerPlugin = function () {
if (arguments[0].pattern && arguments[0].replace) {
this.customTags.push(arguments[0]);
}
else {
this.customTags.push({
pattern: arguments[0],
replace: arguments[1]
});
}
};

/**
* Replaces include tags with actual content from files
* @param {String} file File content
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-include",
"version": "0.4.1",
"version": "0.4.2",
"description": "Include markdown files into other markdown files with C style syntax.",
"main": "markdown-include.js",
"repository": {
Expand Down

0 comments on commit a277f09

Please sign in to comment.