mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-09-10 06:46:06 +00:00
First pass at integrating a Markdown parser
Internal links don't work yet. @natecain - have I included the node module in the right way?
This commit is contained in:
12
plugins/tiddlywiki/markdown/files/tiddlywiki.files
Normal file
12
plugins/tiddlywiki/markdown/files/tiddlywiki.files
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tiddlers": [
|
||||
{
|
||||
"file": "../../../../node_modules/markdown/lib/markdown.js",
|
||||
"fields": {
|
||||
"type": "application/javascript",
|
||||
"title": "$:/plugins/tiddlywiki/markdown/markdown.js",
|
||||
"module-type": "library"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
6
plugins/tiddlywiki/markdown/plugin.info
Normal file
6
plugins/tiddlywiki/markdown/plugin.info
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"title": "$:/plugins/tiddlywiki/markdown",
|
||||
"description": "Markdown plugin wrapping markdown-js",
|
||||
"author": "JeremyRuston",
|
||||
"coreVersion": ">=5.0.0"
|
||||
}
|
60
plugins/tiddlywiki/markdown/wrapper.js
Normal file
60
plugins/tiddlywiki/markdown/wrapper.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/*\
|
||||
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");
|
||||
|
||||
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++));
|
||||
return widget;
|
||||
} else {
|
||||
return {type: "text", text: node};
|
||||
}
|
||||
}
|
||||
|
||||
var MarkdownParser = function(type,text,options) {
|
||||
var markdownTree = markdown.toHTMLTree(text);
|
||||
this.tree = transformNodes(markdownTree.slice(1));
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
[ 'html',
|
||||
[ 'p', 'something' ],
|
||||
[ 'h1',
|
||||
'heading and ',
|
||||
[ 'strong', 'other' ] ] ]
|
||||
|
||||
*/
|
||||
|
||||
exports["text/x-markdown"] = MarkdownParser;
|
||||
|
||||
})();
|
||||
|
Reference in New Issue
Block a user