diff --git a/build.sh b/build.sh index cec678d..d2170fc 100755 --- a/build.sh +++ b/build.sh @@ -6,4 +6,3 @@ cp node_modules/markdown-it/dist/markdown-it.min.js files/ cp node_modules/markdown-it-task-checkbox/index.js files/markdown-it-checkbox.min.js cp node_modules/highlightjs/highlight.pack.min.js files/ cp node_modules/highlightjs/styles/github.css files/ -cp node_modules/htmlparser/lib/htmlparser.min.js files/ diff --git a/files/htmlparser.min.js b/files/htmlparser.min.js deleted file mode 100644 index c1281c5..0000000 --- a/files/htmlparser.min.js +++ /dev/null @@ -1,22 +0,0 @@ -/*********************************************** -Copyright 2010, 2011, Chris Winberry . All rights reserved. -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. -***********************************************/ -/* v1.7.6 */ -(function(){function e(a,c){this._options=c?c:{};void 0==this._options.includeLocation&&(this._options.includeLocation=!1);this.validateHandler(a);this._handler=a;this.reset()}function j(a){j.super_.call(this,a,{ignoreWhitespace:!0,verbose:!1,enforceEmptyTags:!1})}function g(a,c){this.reset();this._options=c?c:{};void 0==this._options.ignoreWhitespace&&(this._options.ignoreWhitespace=!1);void 0==this._options.verbose&&(this._options.verbose=!0);void 0==this._options.enforceEmptyTags&&(this._options.enforceEmptyTags= !0);"function"==typeof a&&(this._callback=a)}if(!("function"==typeof require&&"object"==typeof exports&&"object"==typeof module&&"string"==typeof __filename&&"string"==typeof __dirname)){if(this.Tautologistics){if(this.Tautologistics.NodeHtmlParser)return}else this.Tautologistics={};this.Tautologistics.NodeHtmlParser={};exports=this.Tautologistics.NodeHtmlParser}var d={Text:"text",Directive:"directive",Comment:"comment",Script:"script",Style:"style",Tag:"tag"};e._reTrim=/(^\s+|\s+$)/g;e._reTrimComment= /(^\!--|--$)/g;e._reWhitespace=/\s/g;e._reTagName=/^\s*(\/?)\s*([^\s\/]+)/;e._reAttrib=/([^=<>\"\'\s]+)\s*=\s*"([^"]*)"|([^=<>\"\'\s]+)\s*=\s*'([^']*)'|([^=<>\"\'\s]+)\s*=\s*([^'"\s]+)|([^=<>\"\'\s\/]+)/g;e._reTags=/[\<\>]/g;e.prototype.parseComplete=function(a){this.reset();this.parseChunk(a);this.done()};e.prototype.parseChunk=function(a){this._done&&this.handleError(Error("Attempted to parse chunk after parsing already done"));this._buffer+=a;this.parseTags()};e.prototype.done=function(){if(!this._done){this._done= !0;if(this._buffer.length){var a=this._buffer;this._buffer="";a={raw:a,data:this._parseState==d.Text?a:a.replace(e._reTrim,""),type:this._parseState};if(this._parseState==d.Tag||this._parseState==d.Script||this._parseState==d.Style)a.name=this.parseTagName(a.data);this.parseAttribs(a);this._elements.push(a)}this.writeHandler();this._handler.done()}};e.prototype.reset=function(){this._buffer="";this._done=!1;this._elements=[];this._next=this._current=this._elementsCurrent=0;this._location={row:0,col:0, charOffset:0,inBuffer:0};this._parseState=d.Text;this._prevTagSep="";this._tagStack=[];this._handler.reset()};e.prototype._options=null;e.prototype._handler=null;e.prototype._buffer=null;e.prototype._done=!1;e.prototype._elements=null;e.prototype._elementsCurrent=0;e.prototype._current=0;e.prototype._next=0;e.prototype._location=null;e.prototype._parseState=d.Text;e.prototype._prevTagSep="";e.prototype._tagStack=null;e.prototype.parseTagAttribs=function(a){for(var c=a.length,b=0;bc.length)){var b;for(e._reAttrib.lastIndex=0;b=e._reAttrib.exec(c);)void 0==a.attribs&&(a.attribs={}),"string"==typeof b[1]&&b[1].length?a.attribs[b[1]]=b[2]:"string"==typeof b[3]&&b[3].length?a.attribs[b[3].toString()]=b[4].toString():"string"==typeof b[5]&& b[5].length?a.attribs[b[5]]=b[6]:"string"==typeof b[7]&&b[7].length&&(a.attribs[b[7]]=b[7])}}};e.prototype.parseTagName=function(a){if(null==a||""==a)return"";a=e._reTagName.exec(a);return!a?"":(a[1]?"/":"")+a[2]};e.prototype.parseTags=function(){for(var a=this._buffer.length-1;e._reTags.test(this._buffer);){this._next=e._reTags.lastIndex-1;var c=this._buffer.charAt(this._next),b=this._buffer.substring(this._current,this._next),b={raw:b,data:this._parseState==d.Text?b:b.replace(e._reTrim,""),type:this._parseState}, f=this.parseTagName(b.data);if(this._tagStack.length)if(this._tagStack[this._tagStack.length-1]==d.Script)if("/script"==f.toLowerCase())this._tagStack.pop();else{if(0!=b.raw.indexOf("!--")&&(b.type=d.Text,this._elements.length&&this._elements[this._elements.length-1].type==d.Text)){var h=this._elements[this._elements.length-1];h.raw=h.data=h.raw+this._prevTagSep+b.raw;b.raw=b.data=""}}else this._tagStack[this._tagStack.length-1]==d.Style?"/style"==f.toLowerCase()?this._tagStack.pop():0!=b.raw.indexOf("!--")&& (b.type=d.Text,this._elements.length&&this._elements[this._elements.length-1].type==d.Text?(h=this._elements[this._elements.length-1],""!=b.raw?(h.raw=h.data=h.raw+this._prevTagSep+b.raw,b.raw=b.data=""):h.raw=h.data=h.raw+this._prevTagSep):""!=b.raw&&(b.raw=b.data=b.raw)):this._tagStack[this._tagStack.length-1]==d.Comment&&(h=b.raw.length,"-"==b.raw.charAt(h-2)&&"-"==b.raw.charAt(h-1)&&">"==c?(this._tagStack.pop(),this._elements.length&&this._elements[this._elements.length-1].type==d.Comment?(h= this._elements[this._elements.length-1],h.raw=h.data=(h.raw+b.raw).replace(e._reTrimComment,""),b.raw=b.data="",b.type=d.Text):b.type=d.Comment):(b.type=d.Comment,this._elements.length&&this._elements[this._elements.length-1].type==d.Comment?(h=this._elements[this._elements.length-1],h.raw=h.data=h.raw+b.raw+c,b.raw=b.data="",b.type=d.Text):b.raw=b.data=b.raw+c));if(b.type==d.Tag&&(b.name=f,f=f.toLowerCase(),0==b.raw.indexOf("!--")?(b.type=d.Comment,delete b.name,h=b.raw.length,"-"==b.raw.charAt(h- 1)&&"-"==b.raw.charAt(h-2)&&">"==c?b.raw=b.data=b.raw.replace(e._reTrimComment,""):(b.raw+=c,this._tagStack.push(d.Comment))):0==b.raw.indexOf("!")||0==b.raw.indexOf("?")?b.type=d.Directive:"script"==f?(b.type=d.Script,"/"!=b.data.charAt(b.data.length-1)&&this._tagStack.push(d.Script)):"/script"==f?b.type=d.Script:"style"==f?(b.type=d.Style,"/"!=b.data.charAt(b.data.length-1)&&this._tagStack.push(d.Style)):"/style"==f&&(b.type=d.Style),b.name&&"/"==b.name.charAt(0)))b.data=b.name;if(""!=b.raw||b.type!= d.Text)this._options.includeLocation&&!b.location&&(b.location=this.getLocation(b.type==d.Tag)),this.parseAttribs(b),this._elements.push(b),b.type!=d.Text&&b.type!=d.Comment&&b.type!=d.Directive&&"/"==b.data.charAt(b.data.length-1)&&this._elements.push({raw:"/"+b.name,data:"/"+b.name,name:"/"+b.name,type:b.type});this._parseState="<"==c?d.Tag:d.Text;this._current=this._next+1;this._prevTagSep=c}this._options.includeLocation&&(this.getLocation(),this._location.row+=this._location.inBuffer,this._location.inBuffer= 0,this._location.charOffset=0);this._buffer=this._current<=a?this._buffer.substring(this._current):"";this._current=0;this.writeHandler()};e.prototype.getLocation=function(a){for(var c=this._location,b=this._current-(a?1:0),d=a&&0==c.charOffset&&0==this._current;c.charOffset=d)return g;if(b&&c.children)c= c.children;else if(!(c instanceof Array))return g;for(i=0;i=d);i++);return g},getElementById:function(a,c,b){a=f.getElements({id:a},c,b,1);return a.length?a[0]:null},getElementsByTagName:function(a,c,b,d){return f.getElements({tag_name:a},c,b,d)},getElementsByTagType:function(a,c,b,d){return f.getElements({tag_type:a},c,b,d)}};exports.Parser=e;exports.DefaultHandler=g;exports.RssHandler=j;exports.ElementType=d;exports.DomUtils=f})(); \ No newline at end of file diff --git a/files/node-htmlparser.min.js b/files/node-htmlparser.min.js deleted file mode 100644 index 27d5eea..0000000 --- a/files/node-htmlparser.min.js +++ /dev/null @@ -1,6 +0,0 @@ -var htmlparser = require("./htmlparser.min"); -exports.Parser = htmlparser.Parser; -exports.DefaultHandler = htmlparser.DefaultHandler; -exports.RssHandler = htmlparser.RssHandler; -exports.ElementType = htmlparser.ElementType; -exports.DomUtils = htmlparser.DomUtils; diff --git a/files/tiddlywiki.files b/files/tiddlywiki.files index 241d8fb..acefad8 100644 --- a/files/tiddlywiki.files +++ b/files/tiddlywiki.files @@ -31,14 +31,6 @@ "title": "$:/plugins/anstosa/tw5-markdown/markdown-checkbox.js", "module-type": "library" } - }, - { - "file": "htmlparser.min.js", - "fields": { - "type": "application/javascript", - "title": "$:/plugins/anstosa/tw5-markdown/htmlparser.js", - "module-type": "library" - } } ] } diff --git a/package.json b/package.json index 3586615..b889df5 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "tw5-markdown", - "version": "1.1.1", + "version": "1.1.4", "private": true, "scripts": { "build": "./build.sh" }, "dependencies": { "highlightjs": "^9.10.0", - "htmlparser": "^1.7.7", "markdown-it": "^8.4.0", "markdown-it-task-checkbox": "^1.0.4" } diff --git a/plugin.info b/plugin.info index e56cd17..92b7573 100644 --- a/plugin.info +++ b/plugin.info @@ -2,7 +2,7 @@ "title": "$:/plugins/anstosa/tw5-markdown", "description": "Markdown", "author": "Ansel Santosa", - "version": "1.1.3", + "version": "1.1.4", "core-version": ">=5.0.0", "source": "https://github.com/anstosa/tw5-markdown", "plugin-type": "plugin", diff --git a/wrapper.js b/wrapper.js index 64842c1..96eac8e 100644 --- a/wrapper.js +++ b/wrapper.js @@ -10,8 +10,11 @@ Wraps up the markdown-it parser for use in TiddlyWiki5 const TYPE_WIKI = 'text/vnd.tiddlywiki'; + let dom; + if (typeof(window) !== 'undefined') { + dom = new DOMParser(); + } const highlight = require('$:/plugins/anstosa/tw5-markdown/highlight.js'); - const htmlparser = require('$:/plugins/anstosa/tw5-markdown/htmlparser.js'); const MarkdownIt = require('$:/plugins/anstosa/tw5-markdown/markdown.js'); const markdown = new MarkdownIt({ highlight: (source, syntax) => { @@ -34,10 +37,10 @@ Wraps up the markdown-it parser for use in TiddlyWiki5 }); const tiddlify = (node, forceText) => { - if (node.type === 'text') { + if (node.nodeType === Node.TEXT_NODE) { let subtree; try { - const children = new $tw.Wiki.parsers[TYPE_WIKI](TYPE_WIKI, node.data, {}).tree[0].children; + const children = new $tw.Wiki.parsers[TYPE_WIKI](TYPE_WIKI, node.textContent, {}).tree[0].children; if (children.length === 0) { subtree = null; } @@ -60,32 +63,25 @@ Wraps up the markdown-it parser for use in TiddlyWiki5 } else { return { - text: node.data, + text: node.textContent, type: 'text' }; } } - if (node.name) { + if (node.tagName) { const widget = { attributes: {}, - tag: node.name, + tag: node.tagName.toLowerCase(), type: 'element' }; - $tw.utils.each(node.attribs, (value, attribute) => { - widget.attributes[attribute] = { + $tw.utils.each(node.attributes, (attribute) => { + widget.attributes[attribute.nodeName] = { type: 'string', - value: value, + value: attribute.nodeValue }; }); - if (widget.tag === 'a') { - widget.attributes.class = widget.attributes.class || { - type: 'string', - value: '', - }; - widget.attributes.class.value += ' tc-tiddlylink'; - } widget.children = []; - node.children.forEach((child) => { + node.childNodes.forEach((child) => { const isPlainText = ( forceText || widget.tag === 'code' || @@ -100,17 +96,8 @@ Wraps up the markdown-it parser for use in TiddlyWiki5 class MarkdownParser { constructor(type, text, options) { const source = markdown.render(text); - const handler = new Tautologistics.NodeHtmlParser.DefaultHandler((error, dom) => { - if (error) { console.error(error); } - }, {verbose: true}); - const parser = new Tautologistics.NodeHtmlParser.Parser(handler); - parser.parseComplete(source); - this.tree = tiddlify({ - children: handler.dom, - name: 'body', - type: 'tag', - }).children; - console.log(this.tree); + const tree = dom.parseFromString(source, 'text/html'); + this.tree = tiddlify(tree.body).children; } }