I am trying to use PageDown on the client side as an editor, and on the server side to then parse that Markdown to HTML.
It seems to work fine on the client side, but on the server side, tickmarks are only "codifying" the character that follows, not the word that it wraps. So if I do this:
test `test` test
I expect this, and this is indeed what I get on the client side:
test <code>test</code> test
But on the server side, I end up getting this instead:
test <code>t</code>est<code> </code>test
I've created a file called pageDown.js
, which is simply Markdown.Converter.js
and Markdown.Sanitizer.js
combined into a single file, with this function added:
function getSanitizedHtml(pagedown){
var converter = new Markdown.getSanitizingConverter();
return converter.makeHtml(pagedown);
}
On the client side, I can use this file like so:
<!DOCTYPE html>
<html>
<head>
<script src="pageDown.js"></script>
<script>
function convert(){
var html = getSanitizedHtml("test `test` test");
console.log(html);
document.getElementById("content").innerHTML = html;
}
</script>
</head>
<body onload="convert()">
<p id="content"></p>
</body>
</html>
That correctly displays: <p>test <code>test</code> test</p>
On the (Java) server side, I use this same exact file, through Java's ScriptEngineManager
and Invocable
:
import java.io.InputStreamReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class PageDownTest{
public static void main(String... args){
try{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(new InputStreamReader(PageDownTest.class.getResourceAsStream("pageDown.js")));
Invocable inv = (Invocable) engine;
String s = String.valueOf(inv.invokeFunction("getSanitizedHtml", "test `test` test"));
System.out.println(s);
}
catch(Exception e){
e.printStackTrace();
}
}
}
That program prints out this: <p>test <code>t</code>est<code></code>test</p>
I see similar problems with other markdown: test **test** test
simply ignores the **
part. However, ##test
correctly returns as <h2>test</h2>
.
This all works fine if I go to the JavaScript directly through HTML, but not when I go through Java. What's going on here? Should I be handling Markdown on the server differently?