mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 10:20:58 -06:00
optimize rule lookup
we have tons of bbcode rules, might as well speed them up
This commit is contained in:
parent
0ba39109a0
commit
0650c8dbab
@ -48,8 +48,24 @@ class Ruler {
|
||||
return this.rules;
|
||||
}
|
||||
|
||||
getRuleForTag(tag) {
|
||||
this.ensureCache();
|
||||
return this.cache[tag];
|
||||
}
|
||||
|
||||
ensureCache() {
|
||||
if (this.cache) { return; }
|
||||
|
||||
this.cache = {};
|
||||
for(let i=this.rules.length-1;i>=0;i--) {
|
||||
let info = this.rules[i];
|
||||
this.cache[info.rule.tag] = info;
|
||||
}
|
||||
}
|
||||
|
||||
push(name, rule) {
|
||||
this.rules.push({name, rule});
|
||||
this.cache = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ export function parseBBCodeTag(src, start, max, multiline) {
|
||||
|
||||
function applyBBCode(state, startLine, endLine, silent, md) {
|
||||
|
||||
var i, nextLine,
|
||||
var nextLine,
|
||||
old_parent, old_line_max, rule,
|
||||
start = state.bMarks[startLine] + state.tShift[startLine],
|
||||
initial = start,
|
||||
@ -126,18 +126,10 @@ function applyBBCode(state, startLine, endLine, silent, md) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let rules = md.block.bbcode_ruler.getRules();
|
||||
let ruleInfo = md.block.bbcode_ruler.getRuleForTag(info.tag);
|
||||
if (!ruleInfo) { return false; }
|
||||
|
||||
for(i=0;i<rules.length;i++) {
|
||||
let r = rules[i].rule;
|
||||
|
||||
if (r.tag === info.tag) {
|
||||
rule = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rule) { return false; }
|
||||
rule = ruleInfo.rule;
|
||||
|
||||
// Since start is found, we can report success here in validation mode
|
||||
if (silent) { return true; }
|
||||
|
@ -15,20 +15,12 @@ function tokanizeBBCode(state, silent, ruler) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let rules = ruler.getRules();
|
||||
let rule, i;
|
||||
|
||||
for (i=0; i<rules.length; i++) {
|
||||
let r = rules[i].rule;
|
||||
if (r.tag === tagInfo.tag) {
|
||||
rule = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let ruleInfo = ruler.getRuleForTag(tagInfo.tag);
|
||||
if (!ruleInfo) { return false; }
|
||||
rule = ruleInfo.rule;
|
||||
|
||||
if (!rule) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rule.replace) {
|
||||
// special handling for replace
|
||||
|
Loading…
Reference in New Issue
Block a user