Add start and end properties to AST nodes for list, codeblock, and all other elements

This commit is contained in:
Gk0Wk 2023-11-29 15:48:38 +08:00
parent 5dc468f1ea
commit 4200495055
1 changed files with 30 additions and 5 deletions

View File

@ -209,8 +209,12 @@ WikiParser.prototype.parsePragmas = function() {
break; break;
} }
// Process the pragma rule // Process the pragma rule
var start = this.pos;
var subTree = nextMatch.rule.parse(); var subTree = nextMatch.rule.parse();
if(subTree.length > 0) { if(subTree.length > 0) {
// Set the start and end positions of the pragma rule if
if (subTree[0].start === undefined) subTree[0].start = start;
if (subTree[subTree.length - 1].end === undefined) subTree[subTree.length - 1].end = this.pos;
// Quick hack; we only cope with a single parse tree node being returned, which is true at the moment // Quick hack; we only cope with a single parse tree node being returned, which is true at the moment
currentTreeBranch.push.apply(currentTreeBranch,subTree); currentTreeBranch.push.apply(currentTreeBranch,subTree);
subTree[0].children = []; subTree[0].children = [];
@ -233,7 +237,14 @@ WikiParser.prototype.parseBlock = function(terminatorRegExpString) {
// Look for a block rule that applies at the current position // Look for a block rule that applies at the current position
var nextMatch = this.findNextMatch(this.blockRules,this.pos); var nextMatch = this.findNextMatch(this.blockRules,this.pos);
if(nextMatch && nextMatch.matchIndex === this.pos) { if(nextMatch && nextMatch.matchIndex === this.pos) {
return nextMatch.rule.parse(); var start = this.pos;
var subTree = nextMatch.rule.parse();
// Set the start and end positions of the first and last blocks if they're not already set
if (subTree.length > 0) {
if (subTree[0].start === undefined) subTree[0].start = start;
if (subTree[subTree.length - 1].end === undefined) subTree[subTree.length - 1].end = this.pos;
}
return subTree;
} }
// Treat it as a paragraph if we didn't find a block rule // Treat it as a paragraph if we didn't find a block rule
var start = this.pos; var start = this.pos;
@ -330,7 +341,15 @@ WikiParser.prototype.parseInlineRunUnterminated = function(options) {
this.pos = nextMatch.matchIndex; this.pos = nextMatch.matchIndex;
} }
// Process the run rule // Process the run rule
tree.push.apply(tree,nextMatch.rule.parse()); var start = this.pos;
var subTree = nextMatch.rule.parse();
// Set the start and end positions of the first and last child if they're not already set
if (subTree.length > 0) {
// Set the start and end positions of the first and last child if they're not already set
if (subTree[0].start === undefined) subTree[0].start = start;
if (subTree[subTree.length - 1].end === undefined) subTree[subTree.length - 1].end = this.pos;
}
tree.push.apply(tree,subTree);
// Look for the next run rule // Look for the next run rule
nextMatch = this.findNextMatch(this.inlineRules,this.pos); nextMatch = this.findNextMatch(this.inlineRules,this.pos);
} }
@ -381,7 +400,14 @@ WikiParser.prototype.parseInlineRunTerminatedExtended = function(terminatorRegEx
this.pos = inlineRuleMatch.matchIndex; this.pos = inlineRuleMatch.matchIndex;
} }
// Process the inline rule // Process the inline rule
tree.push.apply(tree,inlineRuleMatch.rule.parse()); var start = this.pos;
var subTree = inlineRuleMatch.rule.parse();
// Set the start and end positions of the first and last child if they're not already set
if (subTree.length > 0) {
if (subTree[0].start === undefined) subTree[0].start = start;
if (subTree[subTree.length - 1].end === undefined) subTree[subTree.length - 1].end = this.pos;
}
tree.push.apply(tree,subTree);
// Look for the next inline rule // Look for the next inline rule
inlineRuleMatch = this.findNextMatch(this.inlineRules,this.pos); inlineRuleMatch = this.findNextMatch(this.inlineRules,this.pos);
// Look for the next terminator match // Look for the next terminator match
@ -407,7 +433,7 @@ WikiParser.prototype.pushTextWidget = function(array,text,start,end) {
text = $tw.utils.trim(text); text = $tw.utils.trim(text);
} }
if(text) { if(text) {
array.push({type: "text", text: text, start: start, end: end}); array.push({type: "text", text: text, start: start, end: end});
} }
}; };
@ -460,4 +486,3 @@ WikiParser.prototype.amendRules = function(type,names) {
exports["text/vnd.tiddlywiki"] = WikiParser; exports["text/vnd.tiddlywiki"] = WikiParser;
})(); })();