1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-26 18:10:27 +00:00
TiddlyWiki5/plugins/tiddlywiki/markdown/wrapper.js
Jermolene 9e6dab06cc Fix problem with Maruku Markdown metadata
Fixes #855 - albeit it’s a bit of a kluge. The internal format returned
by `markdown.toHTMLTree()` isn’t sufficiently well documented for me to
be confident that the new code is resilient to all the things that
might be returned.
2014-09-19 15:03:52 +01:00

78 lines
1.8 KiB
JavaScript

/*\
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");
var CONFIG_DIALECT_TIDDLER = "$:/config/markdown/dialect",
DEFAULT_DIALECT = "Gruber";
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++));
// Massage images into the image widget
if(widget.tag === "img") {
widget.type = "image";
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;
}
}
return widget;
} else {
return {type: "text", text: node};
}
}
var MarkdownParser = function(type,text,options) {
var dialect = options.wiki.getTiddlerText(CONFIG_DIALECT_TIDDLER,DEFAULT_DIALECT),
markdownTree = markdown.toHTMLTree(text,dialect),
node = $tw.utils.isArray(markdownTree[1]) ? markdownTree.slice(1) : markdownTree.slice(2);
this.tree = transformNodes(node);
};
/*
[ 'html',
[ 'p', 'something' ],
[ 'h1',
'heading and ',
[ 'strong', 'other' ] ] ]
*/
exports["text/x-markdown"] = MarkdownParser;
})();