Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ var out = shortcode.parse(str, {
});
```

Configuring shortcode tags:

```javascript
shortcode.setTags('{{', '}}');
```

## API

### shortcode.setTags(open, close)

Changes the shortcode open and close tags to the values passed in.

### shortcode.add(name, callback)

Adds a handler to the shortcode `name`. The handler receives `(str, params, data)`. When using an enclosing
Expand Down
30 changes: 28 additions & 2 deletions lib/shortcode-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ var SHORTCODE_CLOSE = /\[\s*\/\s*%s\s*\]/.toString().slice(1,-1);
var SHORTCODE_CONTENT = /(.|\n|)*?/.toString().slice(1,-1);
var SHORTCODE_SPACE = /\s*/.toString().slice(1,-1);



function escapeShortCodeTag(tag){
return tag.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};

function setShortCodeOpen(tag){
tag = escapeShortCodeTag(tag);
SHORTCODE_OPEN = new RegExp(tag+'\s*%s').toString().slice(1,-1);
}

function setShortCodeClose(open, close){
var escOpen = escapeShortCodeTag(open);
var escClose = escapeShortCodeTag(close);

SHORTCODE_CLOSE = new RegExp(escOpen+'\s*\/\s*%s\s*'+escClose).toString().slice(1,-1);
SHORTCODE_RIGHT_BRACKET = '\\'+close;
}

function typecast(val) {
val = val.trim().replace(/(^['"]|['"]$)/g, '');
if (/^\d+$/.test(val)) {
Expand All @@ -32,7 +51,7 @@ function typecast(val) {
}

function closeTagString(name) {
return /^[^a-z0-9]/.test(name) ? util.format('[%s]?%s', name[0].replace('$', '\\$'), name.slice(1)) : name;
return (/^[^a-z0-9]/).test(name) ? util.format('[%s]?%s', name[0].replace('$', '\\$'), name.slice(1)) : name;
}

function parseShortcode(name, buf, inline) {
Expand Down Expand Up @@ -81,7 +100,14 @@ function parseShortcode(name, buf, inline) {
module.exports = {

_shortcodes: shortcodes,


setTags: function(open, close){
open = open.toString();
close = close.toString();
setShortCodeOpen(open);
setShortCodeClose(open, close);
},

add: function (name, callback) {
if (typeof name == 'object') {
var ob = name;
Expand Down
19 changes: 18 additions & 1 deletion test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,24 @@ vows.describe("Shortcode Parser").addBatch({
});
assert.strictEqual(out, 'This is [bold size=2em font="Helvetica"]Some Text[/bold] and <u>SOME MORE TEXT</u> and <u>Something</u>...');
},

'Able to change shortcode tags': function() {
// Set short code tags to {{ and }}
shortcode.setTags('{{', '}}');

var out, str = '... {{#data_test}}{{/data_test}} ...';
var context = {
'#data_test' : function(buf, params, data) {
return '<!-- ' + JSON.stringify(data) + '-->';
}
}
out = shortcode.parseInContext(str, context, {user: 'john', blah: true});

assert.equal(out, '... <!-- {"user":"john","blah":true}--> ...');

out = shortcode.parseInContext(str,context);

assert.equal(out, '... <!-- {}--> ...'); // Ensure data is provided as object;
},
'Provides data object to handlers': function() {

var out, str = '... [#data_test][/data_test] ...'; // NOTE: Testing ability to skip first char of shortcode tag
Expand Down