Last active
April 12, 2017 03:44
-
-
Save ariutta/67f36fca118ce99ae5b87eb52825dcf0 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(/(KS\[)(.*)(\])(\(.*\))/g, function(match, titleDelimStart, title, titleDelimEnd, exprs, 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 = 'KS[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