1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-09-07 05:18:03 +00:00

codeblock as a widget and plugin for highlight code blocks

This commit is contained in:
João Bolila
2014-01-07 22:57:46 +00:00
parent 4181de5b74
commit 82a48cf85c
5 changed files with 101 additions and 51 deletions

View File

@@ -1,53 +1,28 @@
/*\
title: $:/plugins/tiddlywiki/highlight/highlightblock.js
type: application/javascript
module-type: parser
module-type: widget
Wraps up the fenced code blocks parser for highlight and use in TiddlyWiki5
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js").hljs,
WikiParser = require("$:/core/modules/parsers/wikiparser/wikiparser.js")["text/vnd.tiddlywiki"],
BlockParsers = $tw.modules.createClassesFromModules("wikirule", "block", $tw.WikiRuleBase);
var CodeBlockWidget = require("$:/core/modules/widgets/codeblock.js").codeblock;
BlockParsers.codeblock.prototype.parse = function() {
var reEnd = /(\r?\n```$)/mg;
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Look for the end of the block
reEnd.lastIndex = this.parser.pos;
var match = reEnd.exec(this.parser.source),
text;
// Process the block
if (match) {
text = this.parser.source.substring(this.parser.pos, match.index);
this.parser.pos = match.index + match[0].length;
} else {
text = this.parser.source.substr(this.parser.pos);
this.parser.pos = this.parser.sourceLength;
}
CodeBlockWidget.prototype.postRender = function() {
var self = this,
lang = this.domNodes[0].getElementsByTagName('code')[0].className,
hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js").hljs;
// Return the pre element
return [{
type: "element",
tag: "pre",
children: [{
type: "element",
tag: "code",
children: [{
type: "raw",
html: hljs.highlightAuto(text).value
}]
}]
}];
};
WikiParser.prototype.blockRuleClasses = BlockParsers;
if ($tw.browser && lang !== 'no-highlight') {
hljs.tabReplace = ' ';
hljs.highlightBlock(this.domNodes[0]);
}
};
})();