Skip to content

Instantly share code, notes, and snippets.

@ChapelR
Last active March 5, 2018 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChapelR/803ec192cf5922f498652bd6a20a4bf8 to your computer and use it in GitHub Desktop.
Save ChapelR/803ec192cf5922f498652bd6a20a4bf8 to your computer and use it in GitHub Desktop.
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