meta-shortcodes
Version:
Shortcode parser with support of arguments, key-value attributes and nesting.
114 lines (78 loc) • 2.73 kB
Markdown
Generic shortcodes parser with support of attributes and single/pair tags.
```javascript
var should = require("should");
var ShortcodeParser = require("meta-shortcodes");
var parser = ShortcodeParser();
parser.add("test", function(opts, content){
return content.toUpperCase();
});
parser.add("nested", function(opts, content){
if(!opts.multiply) return "Missing multiply attribute!";
var out = [];
for(var i = 0; i < opts.length; i++)
out.push(opts[i] * parseFloat(opts.multiply));
return out.join(" ");
});
var input = "Sample [test]shortcode content [nested multiply=2 2 4/] is upper[/test] case!";
var output = parser.parse(input);
output.should.eql("Sample SHORTCODE CONTENT 4 8 IS UPPER case!");
```
```
npm install meta-shortcodes
```
```
opts = {
openPattern: '\\[',
closePattern: '\\]'
}
```
Registers new shortcode
| Param | Type | Description |
| --------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
| shortcodeName | string | Name of shortcode tag |
| handlerFunction | function | Function that returns replacement for shortcode. Accepts two arguments - `options` object and `content` string. |
Registers new shortcode
| Param | Type | Description |
| -------- | ------ | ------------------------------------------------- |
| inputStr | string | Input string where shortcodes should be replaced. |
```
[]
[]content[/name]
[]
[]
\\[this shortcode is not processed /]
```
```
npm install --dev
npm test
```
**Current code-coverage 97%**
```javascript
var should = require("should");
var ShortcodeParser = require("meta-shortcodes");
var parser = ShortcodeParser({
openPattern: '\\{{',
closePattern: '\\}}'
});
parser.add("test", function(opts, content){
return content.toUpperCase();
});
var output = parser.parse("Sample {{test}}upper{{/test}} case!").should.eql("Sample UPPER case!");
```
MIT (c) 2015 [META Platform team](http://www.meta-platform.com), [Jiri Hybek](http://jiri.hybek.cz/)