2013-11-01 16:06:08 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/markdown/wrapper.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: parser
|
|
|
|
|
2020-01-20 13:13:36 +00:00
|
|
|
Wraps up the remarkable parser for use as a Parser in TiddlyWiki
|
2013-11-01 16:06:08 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2020-02-04 14:35:21 +00:00
|
|
|
var r = require("$:/plugins/tiddlywiki/markdown/remarkable.js");
|
|
|
|
|
|
|
|
var Remarkable = r.Remarkable,
|
|
|
|
linkify = r.linkify,
|
|
|
|
utils = r.utils;
|
2013-11-01 16:06:08 +00:00
|
|
|
|
2020-01-20 13:13:36 +00:00
|
|
|
///// Set up configuration options /////
|
|
|
|
function parseAsBoolean(tiddlerName) {
|
|
|
|
return $tw.wiki.getTiddlerText(tiddlerName).toLowerCase() === "true";
|
|
|
|
}
|
|
|
|
var pluginOpts = {
|
|
|
|
linkNewWindow: parseAsBoolean("$:/config/markdown/linkNewWindow"),
|
|
|
|
renderWikiText: parseAsBoolean("$:/config/markdown/renderWikiText"),
|
|
|
|
renderWikiTextPragma: $tw.wiki.getTiddlerText("$:/config/markdown/renderWikiTextPragma").trim()
|
|
|
|
};
|
|
|
|
var remarkableOpts = {
|
|
|
|
breaks: parseAsBoolean("$:/config/markdown/breaks"),
|
|
|
|
quotes: $tw.wiki.getTiddlerText("$:/config/markdown/quotes"),
|
|
|
|
typographer: parseAsBoolean("$:/config/markdown/typographer")
|
|
|
|
};
|
2020-06-11 11:09:10 +00:00
|
|
|
var accumulatingTypes = {
|
|
|
|
"text": true,
|
|
|
|
"softbreak": true
|
|
|
|
};
|
2020-01-20 13:13:36 +00:00
|
|
|
|
|
|
|
var md = new Remarkable(remarkableOpts);
|
2014-08-27 10:09:59 +00:00
|
|
|
|
2020-01-20 13:13:36 +00:00
|
|
|
if (parseAsBoolean("$:/config/markdown/linkify")) {
|
|
|
|
md = md.use(linkify);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findTagWithType(nodes, startPoint, type, level) {
|
|
|
|
for (var i = startPoint; i < nodes.length; i++) {
|
|
|
|
if (nodes[i].type === type && nodes[i].level === level) {
|
|
|
|
return i;
|
|
|
|
}
|
2013-11-01 16:06:08 +00:00
|
|
|
}
|
2020-01-20 13:13:36 +00:00
|
|
|
return false;
|
2013-11-01 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 13:13:36 +00:00
|
|
|
/**
|
|
|
|
* Remarkable creates nodes that look like:
|
|
|
|
* [
|
|
|
|
* { type: 'paragraph_open'},
|
|
|
|
* { type: 'inline', content: 'Hello World', children:[{type: 'text', content: 'Hello World'}]},
|
|
|
|
* { type: 'paragraph_close'}
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* But TiddlyWiki wants the Parser (https://tiddlywiki.com/dev/static/Parser.html) to emit nodes like:
|
|
|
|
*
|
|
|
|
* [
|
|
|
|
* { type: 'element', tag: 'p', children: [{type: 'text', text: 'Hello World'}]}
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
function convertNodes(remarkableTree, isStartOfInline) {
|
|
|
|
let out = [];
|
2020-06-11 11:09:10 +00:00
|
|
|
var accumulatedText = '';
|
2020-01-20 13:13:36 +00:00
|
|
|
function wrappedElement(elementTag, currentIndex, currentLevel, closingType, nodes) {
|
|
|
|
var j = findTagWithType(nodes, currentIndex + 1, closingType, currentLevel);
|
|
|
|
if (j === false) {
|
|
|
|
console.error("Failed to find a " + closingType + " node after position " + currentIndex);
|
|
|
|
console.log(nodes);
|
|
|
|
return currentIndex + 1;
|
2013-11-01 16:06:08 +00:00
|
|
|
}
|
2020-01-20 13:13:36 +00:00
|
|
|
let children = convertNodes(nodes.slice(currentIndex + 1, j));
|
|
|
|
|
|
|
|
out.push({
|
|
|
|
type: "element",
|
|
|
|
tag: elementTag,
|
|
|
|
children: children
|
|
|
|
});
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < remarkableTree.length; i++) {
|
|
|
|
var currentNode = remarkableTree[i];
|
2020-06-11 11:09:10 +00:00
|
|
|
switch (currentNode.type) {
|
|
|
|
case "paragraph_open":
|
2020-01-20 13:13:36 +00:00
|
|
|
i = wrappedElement("p", i, currentNode.level, "paragraph_close", remarkableTree);
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "heading_open":
|
2020-01-20 13:13:36 +00:00
|
|
|
i = wrappedElement("h" + currentNode.hLevel, i, currentNode.level, "heading_close", remarkableTree);
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "bullet_list_open":
|
2020-01-20 13:13:36 +00:00
|
|
|
i = wrappedElement("ul", i, currentNode.level, "bullet_list_close", remarkableTree);
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "ordered_list_open":
|
2020-01-20 13:13:36 +00:00
|
|
|
i = wrappedElement('ol', i, currentNode.level,'ordered_list_close', remarkableTree);
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "list_item_open":
|
2020-01-20 13:13:36 +00:00
|
|
|
i = wrappedElement("li", i, currentNode.level, "list_item_close", remarkableTree);
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "link_open":
|
2020-01-20 13:13:36 +00:00
|
|
|
var j = findTagWithType(remarkableTree, i + 1, "link_close", currentNode.level);
|
|
|
|
|
|
|
|
if (currentNode.href[0] !== "#") {
|
|
|
|
// External link
|
|
|
|
var attributes = {
|
|
|
|
href: { type: "string", value: currentNode.href }
|
|
|
|
};
|
|
|
|
if (pluginOpts.linkNewWindow) {
|
|
|
|
attributes.target = { type: "string", value: "_blank" };
|
|
|
|
}
|
|
|
|
out.push({
|
|
|
|
type: "element",
|
|
|
|
tag: "a",
|
|
|
|
attributes: attributes,
|
|
|
|
children: convertNodes(remarkableTree.slice(i + 1, j))
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Internal link
|
|
|
|
out.push({
|
|
|
|
type: "link",
|
|
|
|
attributes: {
|
|
|
|
to: { type: "string", value: decodeURI(currentNode.href.substr(1)) }
|
|
|
|
},
|
|
|
|
children: convertNodes(remarkableTree.slice(i + 1, j))
|
|
|
|
});
|
2014-04-17 15:50:54 +00:00
|
|
|
}
|
2020-01-20 13:13:36 +00:00
|
|
|
i = j;
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "code":
|
2020-01-20 13:13:36 +00:00
|
|
|
out.push({
|
|
|
|
type: "element",
|
|
|
|
tag: currentNode.block ? "pre" : "code",
|
|
|
|
children: [{ type: "text", text: currentNode.content }]
|
|
|
|
});
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "fence":
|
2020-01-20 13:13:36 +00:00
|
|
|
out.push({
|
|
|
|
type: "codeblock",
|
|
|
|
attributes: {
|
|
|
|
language: { type: "string", value: currentNode.params },
|
|
|
|
code: { type: "string", value: currentNode.content }
|
|
|
|
}
|
|
|
|
});
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "image":
|
2020-01-20 13:13:36 +00:00
|
|
|
out.push({
|
|
|
|
type: "image",
|
|
|
|
attributes: {
|
|
|
|
tooltip: { type: "string", value: currentNode.alt },
|
2020-06-11 11:03:16 +00:00
|
|
|
source: { type: "string", value: decodeURIComponent(currentNode.src) }
|
2020-01-20 13:13:36 +00:00
|
|
|
}
|
|
|
|
});
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "softbreak":
|
|
|
|
if (remarkableOpts.breaks) {
|
|
|
|
out.push({
|
|
|
|
type: "element",
|
|
|
|
tag: "br",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
accumulatedText = accumulatedText + '\n';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "hardbreak":
|
2020-01-20 13:13:36 +00:00
|
|
|
out.push({
|
|
|
|
type: "element",
|
|
|
|
tag: "br",
|
|
|
|
});
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "hr":
|
2020-01-20 13:13:36 +00:00
|
|
|
out.push({
|
|
|
|
type: 'element',
|
|
|
|
tag: 'hr',
|
|
|
|
});
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "inline":
|
2020-01-20 13:13:36 +00:00
|
|
|
out = out.concat(convertNodes(currentNode.children, true));
|
2020-06-11 11:09:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "text":
|
|
|
|
// We need to merge this text block with the upcoming text block and parse it all together.
|
|
|
|
accumulatedText = accumulatedText + currentNode.content;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (currentNode.type.substr(currentNode.type.length - 5) === "_open") {
|
|
|
|
var tagName = currentNode.type.substr(0, currentNode.type.length - 5);
|
|
|
|
i = wrappedElement(tagName, i, currentNode.level, tagName + "_close", remarkableTree);
|
|
|
|
} else {
|
|
|
|
console.error("Unknown node type: " + currentNode.type, currentNode);
|
2020-01-20 13:13:36 +00:00
|
|
|
out.push({
|
|
|
|
type: "text",
|
|
|
|
text: currentNode.content
|
|
|
|
});
|
2020-06-11 11:09:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We test to see if we process the block now, or if there's
|
|
|
|
// more to accumulate first.
|
|
|
|
if (accumulatedText
|
|
|
|
&& (
|
|
|
|
remarkableOpts.breaks ||
|
|
|
|
(i+1) >= remarkableTree.length ||
|
|
|
|
!accumulatingTypes[remarkableTree[i+1].type]
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// The Markdown compiler thinks this is just text.
|
|
|
|
// Hand off to the WikiText parser to see if there's more to render
|
|
|
|
// But only if it's configured to, and we have more than whitespace
|
|
|
|
if (!pluginOpts.renderWikiText || accumulatedText.match(/^\s*$/)) {
|
|
|
|
out.push({
|
|
|
|
type: "text",
|
|
|
|
text: accumulatedText
|
|
|
|
});
|
2020-01-20 13:13:36 +00:00
|
|
|
} else {
|
|
|
|
// If we're inside a block element (div, p, td, h1), and this is the first child in the tree,
|
|
|
|
// handle as a block-level parse. Otherwise not.
|
|
|
|
var parseAsInline = !(isStartOfInline && i === 0);
|
2020-06-11 11:09:10 +00:00
|
|
|
var textToParse = accumulatedText;
|
2020-01-20 13:13:36 +00:00
|
|
|
if (pluginOpts.renderWikiTextPragma !== "") {
|
|
|
|
textToParse = pluginOpts.renderWikiTextPragma + "\n" + textToParse;
|
|
|
|
}
|
|
|
|
var wikiParser = $tw.wiki.parseText("text/vnd.tiddlywiki", textToParse, {
|
|
|
|
parseAsInline: parseAsInline
|
|
|
|
});
|
|
|
|
var rs = wikiParser.tree;
|
|
|
|
|
|
|
|
// If we parsed as a block, but the root element the WikiText parser gave is a paragraph,
|
|
|
|
// we should discard the paragraph, since the way Remarkable nests its nodes, this "inline"
|
|
|
|
// node is always inside something else that's a block-level element
|
|
|
|
if (!parseAsInline
|
|
|
|
&& rs.length === 1
|
|
|
|
&& rs[0].type === "element"
|
|
|
|
&& rs[0].tag === "p"
|
|
|
|
) {
|
|
|
|
rs = rs[0].children;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the original text element started with a space, add it back in
|
|
|
|
if (rs.length > 0
|
|
|
|
&& rs[0].type === "text"
|
2020-06-11 11:09:10 +00:00
|
|
|
&& (accumulatedText[0] === " " || accumulatedText[0] === "\n")
|
2020-01-20 13:13:36 +00:00
|
|
|
) {
|
|
|
|
rs[0].text = " " + rs[0].text;
|
|
|
|
}
|
|
|
|
out = out.concat(rs);
|
2015-10-09 13:11:24 +00:00
|
|
|
}
|
2020-06-11 11:09:10 +00:00
|
|
|
accumulatedText = '';
|
2015-10-09 13:11:24 +00:00
|
|
|
}
|
2013-11-01 16:06:08 +00:00
|
|
|
}
|
2020-01-20 13:13:36 +00:00
|
|
|
return out;
|
2013-11-01 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 13:13:36 +00:00
|
|
|
var MarkdownParser = function(type, text, options) {
|
|
|
|
var tree = md.parse(text, {});
|
|
|
|
//console.debug(tree);
|
|
|
|
tree = convertNodes(tree);
|
|
|
|
//console.debug(tree);
|
2013-11-01 16:06:08 +00:00
|
|
|
|
2020-01-20 13:13:36 +00:00
|
|
|
this.tree = tree;
|
|
|
|
};
|
2013-11-01 16:06:08 +00:00
|
|
|
|
|
|
|
exports["text/x-markdown"] = MarkdownParser;
|
|
|
|
|
|
|
|
})();
|