2013-11-01 16:06:08 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/markdown/wrapper.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: parser
|
|
|
|
|
|
|
|
Wraps up the markdown-js parser for use in TiddlyWiki5
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var markdown = require("$:/plugins/tiddlywiki/markdown/markdown.js");
|
|
|
|
|
2014-08-27 10:09:59 +00:00
|
|
|
var CONFIG_DIALECT_TIDDLER = "$:/config/markdown/dialect",
|
|
|
|
DEFAULT_DIALECT = "Gruber";
|
|
|
|
|
2013-11-01 16:06:08 +00:00
|
|
|
function transformNodes(nodes) {
|
|
|
|
var results = [];
|
|
|
|
for(var index=0; index<nodes.length; index++) {
|
|
|
|
results.push(transformNode(nodes[index]));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function transformNode(node) {
|
|
|
|
if($tw.utils.isArray(node)) {
|
|
|
|
var p = 0,
|
|
|
|
widget = {type: "element", tag: node[p++]};
|
|
|
|
if(!$tw.utils.isArray(node[p]) && typeof(node[p]) === "object") {
|
|
|
|
widget.attributes = {};
|
|
|
|
$tw.utils.each(node[p++],function(value,name) {
|
|
|
|
widget.attributes[name] = {type: "string", value: value};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
widget.children = transformNodes(node.slice(p++));
|
2014-04-17 15:50:54 +00:00
|
|
|
// Massage images into the image widget
|
|
|
|
if(widget.tag === "img") {
|
2014-06-18 13:56:32 +00:00
|
|
|
widget.type = "image";
|
2014-04-17 15:50:54 +00:00
|
|
|
if(widget.attributes.alt) {
|
|
|
|
widget.attributes.tooltip = widget.attributes.alt;
|
|
|
|
delete widget.attributes.alt;
|
|
|
|
}
|
|
|
|
if(widget.attributes.src) {
|
|
|
|
widget.attributes.source = widget.attributes.src;
|
|
|
|
delete widget.attributes.src;
|
|
|
|
}
|
|
|
|
}
|
2015-10-09 13:11:24 +00:00
|
|
|
// Convert internal links to proper wikilinks
|
|
|
|
if (widget.tag === "a" && widget.attributes.href.value[0] === "#") {
|
|
|
|
widget.type = "link";
|
|
|
|
widget.attributes.to = widget.attributes.href;
|
|
|
|
if (widget.attributes.to.type === "string") {
|
|
|
|
//Remove '#' before conversion to wikilink
|
|
|
|
widget.attributes.to.value = widget.attributes.to.value.substr(1);
|
|
|
|
}
|
|
|
|
//Children is fine
|
|
|
|
delete widget.tag;
|
|
|
|
delete widget.attributes.href;
|
|
|
|
}
|
2013-11-01 16:06:08 +00:00
|
|
|
return widget;
|
|
|
|
} else {
|
|
|
|
return {type: "text", text: node};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var MarkdownParser = function(type,text,options) {
|
2014-08-27 10:09:59 +00:00
|
|
|
var dialect = options.wiki.getTiddlerText(CONFIG_DIALECT_TIDDLER,DEFAULT_DIALECT),
|
2014-09-19 14:03:52 +00:00
|
|
|
markdownTree = markdown.toHTMLTree(text,dialect),
|
|
|
|
node = $tw.utils.isArray(markdownTree[1]) ? markdownTree.slice(1) : markdownTree.slice(2);
|
|
|
|
this.tree = transformNodes(node);
|
2013-11-01 16:06:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
[ 'html',
|
|
|
|
[ 'p', 'something' ],
|
|
|
|
[ 'h1',
|
|
|
|
'heading and ',
|
|
|
|
[ 'strong', 'other' ] ] ]
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports["text/x-markdown"] = MarkdownParser;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|