reactor the highlighting code to avoid duplication and make it more coherent

pull/21833/head
Matthew Hodgson 2015-11-29 13:00:58 +00:00
parent ca6cdfafd3
commit bed7d50ab8
1 changed files with 24 additions and 27 deletions

View File

@ -65,20 +65,8 @@ module.exports = {
while ((offset = safeSnippet.indexOf(safeHighlight, lastOffset)) >= 0) { while ((offset = safeSnippet.indexOf(safeHighlight, lastOffset)) >= 0) {
// handle preamble // handle preamble
if (offset > lastOffset) { if (offset > lastOffset) {
if (highlights[1]) { nodes = nodes.concat(this._applySubHighlightsInRange(safeSnippet, lastOffset, offset, highlights, html, k));
// recurse into the preamble to check for the next highlights k += nodes.length;
var subnodes = this._applyHighlights( safeSnippet.substring(lastOffset, offset), highlights.slice(1), html, k );
nodes = nodes.concat(subnodes);
k += subnodes.length;
}
else {
if (html) {
nodes.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeSnippet.substring(lastOffset, offset) }} />);
}
else {
nodes.push(<span key={ k++ }>{ safeSnippet.substring(lastOffset, offset) }</span>);
}
}
} }
// do highlight // do highlight
@ -94,18 +82,27 @@ module.exports = {
// handle postamble // handle postamble
if (lastOffset != safeSnippet.length) { if (lastOffset != safeSnippet.length) {
if (highlights[1]) { nodes = nodes.concat(this._applySubHighlightsInRange(safeSnippet, lastOffset, undefined, highlights, html, k));
var subnodes = this._applyHighlights( safeSnippet.substring(lastOffset), highlights.slice(1), html, k ) k += nodes.length;
nodes = nodes.concat( subnodes ); }
k += subnodes.length; return nodes;
},
_applySubHighlightsInRange: function(safeSnippet, lastOffset, offset, highlights, html, k) {
var nodes = [];
if (highlights[1]) {
// recurse into this range to check for the next set of highlight matches
var subnodes = this._applyHighlights( safeSnippet.substring(lastOffset, offset), highlights.slice(1), html, k );
nodes = nodes.concat(subnodes);
k += subnodes.length;
}
else {
// no more highlights to be found, just return the unhighlighted string
if (html) {
nodes.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeSnippet.substring(lastOffset, offset) }} />);
} }
else { else {
if (html) { nodes.push(<span key={ k++ }>{ safeSnippet.substring(lastOffset, offset) }</span>);
nodes.push(<span className="markdown-body" key={ k++ } dangerouslySetInnerHTML={{ __html: safeSnippet.substring(lastOffset) }} />);
}
else {
nodes.push(<span className="markdown-body" key={ k++ }>{ safeSnippet.substring(lastOffset) }</span>);
}
} }
} }
return nodes; return nodes;