MCPcopy Index your code
hub / github.com/documentcloud/visualsearch / highlightBlock

Function highlightBlock

docs/assets/highlight.js:421–491  ·  view source on GitHub ↗
(block, tabReplace, useBR)

Source from the content-addressed store, hash-verified

419 /* Public library functions */
420
421 function highlightBlock(block, tabReplace, useBR) {
422 initialize();
423
424 var text = blockText(block, useBR);
425 var language = blockLanguage(block);
426 if (language == 'no-highlight')
427 return;
428 if (language) {
429 var result = highlight(language, text);
430 } else {
431 var result = {language: '', keyword_count: 0, relevance: 0, value: escape(text)};
432 var second_best = result;
433 for (var key in languages) {
434 if (!languages.hasOwnProperty(key))
435 continue;
436 var current = highlight(key, text);
437 if (current.keyword_count + current.relevance > second_best.keyword_count + second_best.relevance) {
438 second_best = current;
439 }
440 if (current.keyword_count + current.relevance > result.keyword_count + result.relevance) {
441 second_best = result;
442 result = current;
443 }
444 }
445 }
446
447 var class_name = block.className;
448 if (!class_name.match(result.language)) {
449 class_name = class_name ? (class_name + ' ' + result.language) : result.language;
450 }
451 var original = nodeStream(block);
452 if (original.length) {
453 var pre = document.createElement('pre');
454 pre.innerHTML = result.value;
455 result.value = mergeStreams(original, nodeStream(pre), text);
456 }
457 if (tabReplace) {
458 result.value = result.value.replace(/^((<[^>]+>|\t)+)/gm, function(match, p1, offset, s) {
459 return p1.replace(/\t/g, tabReplace);
460 })
461 }
462 if (useBR) {
463 result.value = result.value.replace(/\n/g, '<br>');
464 }
465 if (/MSIE [678]/.test(navigator.userAgent) && block.tagName == 'CODE' && block.parentNode.tagName == 'PRE') {
466 // This is for backwards compatibility only. IE needs this strange
467 // hack becasue it cannot just cleanly replace <code> block contents.
468 var pre = block.parentNode;
469 var container = document.createElement('div');
470 container.innerHTML = '<pre><code>' + result.value + '</code></pre>';
471 block = container.firstChild.firstChild;
472 container.firstChild.className = pre.className;
473 pre.parentNode.replaceChild(container.firstChild, pre);
474 } else {
475 block.innerHTML = result.value;
476 }
477 block.className = class_name;
478 block.dataset = {};

Callers 1

initHighlightingFunction · 0.85

Calls 7

initializeFunction · 0.85
blockTextFunction · 0.85
blockLanguageFunction · 0.85
highlightFunction · 0.85
nodeStreamFunction · 0.85
mergeStreamsFunction · 0.85
escapeFunction · 0.70

Tested by

no test coverage detected