mirror of
https://github.com/TiddlyWiki/TiddlyWiki5.git
synced 2026-04-27 18:54:38 +00:00
codeblock as a widget and plugin for highlight code blocks
This commit is contained in:
@@ -23,14 +23,15 @@ exports.types = {block: true};
|
||||
|
||||
exports.init = function(parser) {
|
||||
this.parser = parser;
|
||||
// Regexp to match
|
||||
this.matchRegExp = /```\r?\n/mg;
|
||||
// Regexp to match and get language if defined
|
||||
this.matchRegExp = /```([\w-]*)\r?\n/mg;
|
||||
};
|
||||
|
||||
exports.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),
|
||||
@@ -43,14 +44,14 @@ exports.parse = function() {
|
||||
text = this.parser.source.substr(this.parser.pos);
|
||||
this.parser.pos = this.parser.sourceLength;
|
||||
}
|
||||
// Return the pre element
|
||||
// Return the $codeblock widget
|
||||
return [{
|
||||
type: "element",
|
||||
tag: "pre",
|
||||
children: [{
|
||||
type: "text",
|
||||
text: text
|
||||
}]
|
||||
type: "element",
|
||||
tag: "$codeblock",
|
||||
attributes: {
|
||||
code: {tye: "string", value: text},
|
||||
language: {tye: "string", value: this.match[1]}
|
||||
}
|
||||
}];
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user