krz/org-live

An org-mode editor with live preview.

clone: git clone https://gitbay.org/krz/org-live.git

main: static/app.js · raw

 1window.onkeyup = keyup;
 2var inputText;
 3
 4function keyup(e) {
 5    // inputText = e.target.value;
 6    inputText = parseOrg();
 7
 8    const preview = document.getElementById("preview");
 9    preview.innerHTML = inputText;
10}
11
12function parseOrg() {
13    var orgCode = document.getElementById("editor").value;
14    var orgParser = new Org.Parser();
15    var orgDocument = orgParser.parse(orgCode);
16    var orgHTMLDocument = orgDocument.convert(Org.ConverterHTML, {
17        headerOffset: 1,
18        exportFromLineNumber: false,
19        suppressSubScriptHandling: false,
20        suppressAutoLink: false,
21    });
22
23    // console.dir(orgHTMLDocument); // => { title, contentHTML, tocHTML, toc }
24    // console.log(orgHTMLDocument.toString()) // => Rendered HTML
25    return orgHTMLDocument;
26}
27
28function saveOrg() {
29    const filename = new Date().toISOString();
30    var data = document.getElementById("editor").value;
31    var file = new Blob([data], { type: Text });
32    var a = document.createElement("a"),
33        url = URL.createObjectURL(file);
34    a.href = url;
35    a.download = filename + ".org";
36    document.body.appendChild(a);
37    a.click();
38    setTimeout(function () {
39        document.body.removeChild(a);
40        window.URL.revokeObjectURL(url);
41    }, 0);
42}
43
44function saveHTML() {
45    const filename = new Date().toISOString();
46    var data = parseOrg();
47    var file = new Blob([data], { type: Text });
48    var a = document.createElement("a"),
49        url = URL.createObjectURL(file);
50    a.href = url;
51    a.download = filename + ".html";
52    document.body.appendChild(a);
53    a.click();
54    setTimeout(function () {
55        document.body.removeChild(a);
56        window.URL.revokeObjectURL(url);
57    }, 0);
58}