1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-06 02:37:14 +00:00

fix: Wiki parser initialize blockRuleClasses only when first new an instance

This commit is contained in:
lin onetwo 2024-10-13 03:13:51 +08:00
parent 1c50996a13
commit f0babe4388
2 changed files with 24 additions and 19 deletions

View File

@ -123,11 +123,28 @@ exports.getParseTreeText = function getParseTreeText(tree) {
return output.join("");
};
exports.getParser = function(type,options) {
options = options || {};
// Select a parser
var Parser = $tw.Wiki.parsers[type];
if(!Parser && $tw.utils.getFileExtensionInfo(type)) {
Parser = $tw.Wiki.parsers[$tw.utils.getFileExtensionInfo(type).type];
}
if(!Parser) {
Parser = $tw.Wiki.parsers[options.defaultType || "text/vnd.tiddlywiki"];
}
if(!Parser) {
return null;
}
return Parser;
};
/*
Utility to get the (similarly but not 1:1 equal) original wikitext of a parse tree node or array of nodes.
Based on `node.rule` metadata added in `wikiparser.js`.
*/
exports.serializeParseTree = function serializeParseTree(tree,tiddlerType) {
exports.serializeParseTree = function serializeParseTree(tree,tiddlerType,options) {
options = options || {};
var output = [];
if($tw.utils.isArray(tree)) {
$tw.utils.each(tree,function(node) {
@ -137,7 +154,11 @@ exports.serializeParseTree = function serializeParseTree(tree,tiddlerType) {
if(tree.type === "text" && !tree.rule) {
output.push(tree.text);
} else {
var Parser = $tw.wiki.getParser(tiddlerType);
var Parser = $tw.utils.getParser(tiddlerType);
if(!Parser.prototype.blockRuleClasses && !Parser.prototype.inlineRuleClasses && !Parser.prototype.pragmaRuleClasses) {
// Wiki parser initialize blockRuleClasses when first new an instance
new Parser(tiddlerType,undefined,{wiki: options.wiki || $tw.wiki});
}
var Rule = Parser.prototype.blockRuleClasses[tree.rule] ||
Parser.prototype.inlineRuleClasses[tree.rule] ||
Parser.prototype.pragmaRuleClasses[tree.rule];

View File

@ -1053,7 +1053,7 @@ Options include:
exports.parseText = function(type,text,options) {
text = text || "";
options = options || {};
var Parser = this.getParser(type,options)
var Parser = $tw.utils.getParser(type,options)
// Return the parser instance
return new Parser(type,text,{
parseAsInline: options.parseAsInline,
@ -1063,22 +1063,6 @@ exports.parseText = function(type,text,options) {
});
};
exports.getParser = function(type,options) {
options = options || {};
// Select a parser
var Parser = $tw.Wiki.parsers[type];
if(!Parser && $tw.utils.getFileExtensionInfo(type)) {
Parser = $tw.Wiki.parsers[$tw.utils.getFileExtensionInfo(type).type];
}
if(!Parser) {
Parser = $tw.Wiki.parsers[options.defaultType || "text/vnd.tiddlywiki"];
}
if(!Parser) {
return null;
}
return Parser;
};
/*
Parse a tiddler according to its MIME type
*/