Last active
April 12, 2017 03:44
-
-
Save ariutta/53544531d60b632c417c90eee41aea37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parse = require('s-expression'); | |
var showdown = require('showdown'); | |
function zoomOn(nodes) { | |
console.log('zoomOn'); | |
console.log(`nodes: ${nodes}`); | |
} | |
function toggleHighlight(node, color) { | |
console.log('toggleHighlight'); | |
console.log(`node: ${node}`); | |
console.log(`color: ${color}`); | |
} | |
var manipulations = { | |
zoomOn: zoomOn, | |
toggleHighlight: toggleHighlight, | |
}; | |
var combinations = new Map(); | |
showdown.extension('myExtension', function() { | |
return { | |
type: 'lang', | |
filter: function (text, converter, options) { | |
return text.replace(/(\[)(.*)(\])(!!)(.*)(!!)/g, function(match, titleDelimStart, title, titleDelimEnd, exprDelimStart, exprs, exprDelimEnd, str) { | |
var eventArr = parse(exprs) | |
.reduce(function(acc, expr) { | |
if (typeof expr === 'string') { | |
acc.push({ | |
fn: expr, | |
args: [] | |
}); | |
} else { | |
acc[acc.length - 1].args.push(expr); | |
} | |
return acc; | |
}, []); | |
var eventArrStr = JSON.stringify(eventArr).replace(/[^a-zA-Z0-9]/g, ''); | |
combinations.set(eventArrStr, function() { | |
eventArr.forEach(function(x) { | |
manipulations[x.fn].apply(undefined, x.args); | |
}); | |
}); | |
return `<p onClick="combinations.get(${eventArrStr})">${title}</p>`; | |
}); | |
} | |
}; | |
}); | |
var converter = new showdown.Converter({ extensions: ['myExtension'] }); | |
const input = '[some text]!!(zoomOn (node1 node2 node3) toggleHighlight (node2 red) (node3 yellow))!!'; | |
var html = converter.makeHtml(input); | |
console.log(html); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment