Middleware in my Node.js
functions.editorPreview = function (req, res) {
var data = req.body.text;
fs.writeFile(__dirname + '/message.txt', data, function (err) {
if (err) throw err;
child = exec('kramdown ' + __dirname + '/message.txt', function (error, stdout, stderr) {
if (error) {
return res.json(500, error.message);
}
return res.json(200, {
text: stdout
});
});
});
};
$.post method in index.html
$(function() {
$('#kramdown-input').keyup(function() {
$.post('/editor/preview', { text: $(this).val() }, function(data) {
$('#output').html(data.text);
$('pre code').each(function(i, e) { hljs.highlightBlock(e) });
MathJax.Hub.Queue(['Typeset', MathJax.Hub]);
});
});
$('#kramdown-input').trigger('keyup');
});
So I'm writing my open-source CMS in Node.js called Spectrum, and I was looking for a good Markdown editor and none of them that really impress me that written in JavaScript.
I have come across this one Kramdown and I really liked it.
I have zero knowledge about Ruby, but I got it to the point where it is now working with my CMS, but one problem that I'm concern here.
The code above I have make Kramdown to interact on the preview panel, but is this really efficient?
Because this is like we are sending a GET/POST request to the server every single time the user time a character.
I was trying to check if there is a better way to parse the data without having to write the file and then execute Kramdown to parse the file then sending data back.
Is there a better way to do this? I don't really feel good with my solution here :(