UNPKG

highlight-ts

Version:

Highlight.JS in TypeScript (and ES6).

332 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var languages_1 = require("./languages"); /* Utility functions */ function testRe(re, lexeme) { var match = re && re.exec(lexeme); return match && match.index === 0 || false; } /* Core highlighting function. Accepts a language name, or an alias, and a string with the code to highlight. Returns an object with the following properties: - relevance (int) - value (an HTML string with highlighting markup) */ function highlight(options, render, lang, value, ignore_illegals, continuation) { var output = [{ content: [] }]; function outContent(content) { var cont = output[0].content; // optimization for sequential strings outputs if (typeof content == 'string' && cont.length && typeof cont[cont.length - 1] == 'string') { cont[cont.length - 1] += content; } else { cont.push(content); } } function outText(text) { outContent(render.text(text)); } ; function openSpan(className, noPrefix) { if (!noPrefix) className = options.classPrefix + className; output.unshift({ className: className, content: [] }); } ; function wrapSpan(className) { className = options.classPrefix + className; output.push({ className: className, content: [] }); } ; function closeSpan() { if (output.length < 2) throw "unbalanced"; var _a = output.shift(), className = _a.className, content = _a.content; var output_ = render.join(content); outContent(className ? render.wrap(className, output_) : output_); } ; function endOfMode(mode, lexeme) { if (testRe(mode.endRe, lexeme)) { for (; mode.endsParent && mode.parent; mode = mode.parent) ; return mode; } if (mode.endsWithParent && mode.parent) { return endOfMode(mode.parent, lexeme); } } function processKeywords() { if (!top.keywords) { outText(mode_buffer); return; } var last_index = 0; top.lexemesRe.lastIndex = 0; var match = top.lexemesRe.exec(mode_buffer); while (match) { outText(mode_buffer.substring(last_index, match.index)); // match keyword var match_str = language.case_insensitive ? match[0].toLowerCase() : match[0]; var keyword_match = top.keywords.hasOwnProperty(match_str) && top.keywords[match_str]; if (keyword_match) { relevance += keyword_match[1]; openSpan(keyword_match[0], false); outText(match[0]); closeSpan(); } else { outText(match[0]); } last_index = top.lexemesRe.lastIndex; match = top.lexemesRe.exec(mode_buffer); } outText(mode_buffer.substr(last_index)); } function processSubLanguage(subLanguage) { var explicitLanguage = subLanguage.length == 1 && subLanguage[0]; if (explicitLanguage && !languages_1.getLanguage(explicitLanguage)) { outText(mode_buffer); return; } var result = explicitLanguage ? highlight(options, render, explicitLanguage, mode_buffer, true, continuations[explicitLanguage]) : highlightAuto(options, render, mode_buffer, subLanguage.length ? top.subLanguage : undefined); // Counting embedded language score towards the host language may be disabled // with zeroing the containing mode relevance. Usecase in point is Markdown that // allows XML everywhere and makes every XML snippet to have a much larger Markdown // score. if (top.relevance > 0) { relevance += result.relevance; } if (explicitLanguage && result.top) { continuations[explicitLanguage] = result.top; } openSpan(result.language, true); outContent(result.value); closeSpan(); } function processBuffer() { if (top.subLanguage != null) processSubLanguage(top.subLanguage); else processKeywords(); mode_buffer = ''; } function startNewMode(mode) { if (mode.className) { openSpan(mode.className, false); } top = Object.create(mode, { parent: { value: top } }); } function processLexeme(buffer, lexeme) { mode_buffer += buffer; if (lexeme == null) { processBuffer(); return 0; } var new_mode; // subMode(top, lexeme) for (var _i = 0, _a = top.contains; _i < _a.length; _i++) { var sub = _a[_i]; if (testRe(sub.beginRe, lexeme)) { new_mode = sub; break; } } if (new_mode) { if (new_mode.skip) { mode_buffer += lexeme; } else { if (new_mode.excludeBegin) { mode_buffer += lexeme; } processBuffer(); if (!new_mode.returnBegin && !new_mode.excludeBegin) { mode_buffer = lexeme; } } startNewMode(new_mode /*, lexeme*/); return new_mode.returnBegin ? 0 : lexeme.length; } var end_mode = endOfMode(top, lexeme); if (end_mode) { var origin_1 = top; if (origin_1.skip) { mode_buffer += lexeme; } else { if (!(origin_1.returnEnd || origin_1.excludeEnd)) { mode_buffer += lexeme; } processBuffer(); if (origin_1.excludeEnd) { mode_buffer = lexeme; } } do { if (top.className) { closeSpan(); } if (!top.skip && !top.subLanguage) { relevance += top.relevance; } top = top.parent; } while (top !== end_mode.parent); if (end_mode.starts) { startNewMode(end_mode.starts /*, ''*/); } return origin_1.returnEnd ? 0 : lexeme.length; } // is illegal if (!ignore_illegals && testRe(top.illegalRe, lexeme)) { throw new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.className || '<unnamed>') + '"'); } /* Parser should not reach this point as all types of lexemes should be caught earlier, but if it does due to some bug make sure it advances at least one character forward to prevent infinite looping. */ mode_buffer += lexeme; return lexeme.length || 1; } var language = languages_1.getLanguage(lang); if (!language) throw new Error("Unknown language: \"" + lang + "\""); var top = continuation || language; var continuations = {}; // keep continuations for sub-languages var current; for (current = top; current && current !== language; current = current.parent) { if (current.className) { wrapSpan(current.className); } } var mode_buffer = ''; var relevance = 0; try { var match = void 0, count = void 0, index = 0; while (true) { top.terminators.lastIndex = index; match = top.terminators.exec(value); if (!match) break; count = processLexeme(value.substring(index, match.index), match[0]); index = match.index + count; } processLexeme(value.substr(index)); for (current = top; current.parent; current = current.parent) { // close dangling modes if (current.className) { closeSpan(); } } if (output.length != 1) throw "unbalanced"; var _a = output[0], className = _a.className, content = _a.content; var output_ = render.join(content); var result = className ? render.wrap(className, output_) : output_; return { language: lang, relevance: relevance, value: result, top: top }; } catch (e) { if (e.message && e.message.indexOf('Illegal') !== -1) { return { language: lang, relevance: 0, value: render.text(value) }; } else { throw e; } } } exports.highlight = highlight; /* Highlighting with language detection. Accepts a string with the code to highlight. Returns an object with the following properties: - language (detected language) - relevance (int) - value (an HTML string with highlighting markup) - second_best (object with the same structure for second-best heuristically detected language, may be absent) */ function highlightAuto(options, render, text, languageSubset) { if (languageSubset === void 0) { languageSubset = options.languages || languages_1.listLanguages(); } var result = { language: '', relevance: 0, value: render.text(text) }; if (text != '') { var second_best = result; var languages = languageSubset.filter(languages_1.getLanguage); for (var _i = 0, languages_2 = languages; _i < languages_2.length; _i++) { var lang = languages_2[_i]; var current = highlight(options, render, lang, text, false); if (current.relevance > second_best.relevance) { second_best = current; } if (current.relevance > result.relevance) { second_best = result; result = current; } } if (second_best.language) { result.second_best = second_best; } } return result; } exports.highlightAuto = highlightAuto; // Regular expressions used throughout the highlight.js library. var fixMarkupRe = /((^(<[^>]+>|\t|)+|(?:\n)))/gm; /* Post-processing of the highlighted markup: - replace TABs with something more useful - replace real line-breaks with '<br>' for non-pre containers */ function fixMarkup(options, value) { return !(options.tabReplace || options.useBr) ? value : value.replace(fixMarkupRe, function (match, p1) { if (options.useBr && match === '\n') { return '<br>'; } else if (options.tabReplace) { return p1.replace(/\t/g, options.tabReplace); } return ''; }); } exports.fixMarkup = fixMarkup; exports.defaults = { classPrefix: 'hljs-', //tabReplace: undefined, useBr: false, }; function init(render, options) { if (options === void 0) { options = {}; } return { render: render, options: tslib_1.__assign({}, exports.defaults, options) }; } exports.init = init; function process(_a, source, lang) { var render = _a.render, options = _a.options; return typeof lang == 'string' ? highlight(options, render, lang, source, false) : highlightAuto(options, render, source, lang); } exports.process = process; //# sourceMappingURL=process.js.map