From b15b51812b30b307edc4295c9140582351211c39 Mon Sep 17 00:00:00 2001 From: Laurent VOULLEMIER Date: Sat, 11 Aug 2018 10:21:29 +0200 Subject: [PATCH] Fix sublanguages highlight --- lib/highlight.js | 23 ++--------------------- test/scripts/highlight.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index f97d73fd..6e7d5fd7 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -1,6 +1,6 @@ 'use strict'; -var hljs = require('highlight.js/lib/highlight'); +var hljs = require('highlight.js'); var Entities = require('html-entities').XmlEntities; var entities = new Entities(); var alias = require('../highlight_alias.json'); @@ -89,30 +89,11 @@ function replaceTabs(str, tab) { }); } -function loadLanguage(lang) { - hljs.registerLanguage(lang, require('highlight.js/lib/languages/' + lang)); -} - -function tryLanguage(lang) { - if (hljs.getLanguage(lang)) return true; - if (!alias.aliases[lang]) return false; - - loadLanguage(alias.aliases[lang]); - return true; -} - -function loadAllLanguages() { - alias.languages.filter(function(lang) { - return !hljs.getLanguage(lang); - }).forEach(loadLanguage); -} - function highlight(str, options) { var lang = options.lang; var autoDetect = options.hasOwnProperty('autoDetect') ? options.autoDetect : false; if (!lang && autoDetect) { - loadAllLanguages(); lang = (function() { var result = hljs.highlightAuto(str); if (result.relevance > 0 && result.language) return result.language; @@ -133,7 +114,7 @@ function highlight(str, options) { return result; } - if (!tryLanguage(result.language)) { + if (!alias.aliases[result.language]) { result.language = 'plain'; return result; } diff --git a/test/scripts/highlight.js b/test/scripts/highlight.js index 5553b23b..40812a32 100644 --- a/test/scripts/highlight.js +++ b/test/scripts/highlight.js @@ -193,6 +193,18 @@ describe('highlight', function() { validateHtmlAsync(result, done); }); + it('highlight sublanguages', function(done) { + var str = ''; + var result = highlight(str, {lang: 'xml'}); + result.should.eql([ + '
', + gutter(1, 1), + code('<node><?php echo "foo"; ?></node>', null), + end + ].join('')); + validateHtmlAsync(result, done); + }); + it('parse multi-line strings correctly', function(done) { var str = [ 'var string = `',