Skip to content

Instantly share code, notes, and snippets.

@ChapelR ChapelR/match-macro.js
Last active Mar 5, 2018

Embed
What would you like to do?
function match (id, target, contents, macroName) {
return $(document.createElement('span'))
.attr({'id' : id, 'data-term' : target })
.addClass('macro-' + macroName)
.wiki(contents);
}
function change (id, replacement) {
var $el = $(id),
text = $el.text(),
target = $el.attr('data-term'),
$content;
target = new RegExp(target, 'g');
text = text.replace(target, replacement);
$content = $el.empty().wiki(text);
}
Macro.add('match', {
tags : null,
handler : function () {
if (this.args.length < 2 || this.args.length > 2) {
return this.error('incorrect number of arguments');
}
var id = Util.slugify(this.args[0]),
target = this.args[1], $content;
$content = match(id, target, this.payload[0].contents, this.name);
$content.appendTo(this.output);
}
});
Macro.add('change', {
handler : function () {
if (this.args.length < 2 || this.args.length > 2) {
return this.error('incorrect number of arguments');
}
var id = Util.slugify(this.args[0]),
replacement = this.args[1];
change('#' + id, replacement);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.