FEATURE: Allow selection of highlight js languages

PERF: stop loading highlight js on load

To get latest highlight js run bin/rake highlightjs:update
This commit is contained in:
Sam
2015-03-13 16:15:13 +11:00
parent 1e339ad527
commit a82530012a
137 changed files with 277 additions and 26 deletions
@@ -2,13 +2,22 @@
Support for various code blocks
**/
var acceptableCodeClasses =
["auto", "1c", "actionscript", "apache", "applescript", "avrasm", "axapta", "bash", "brainfuck",
"clojure", "cmake", "coffeescript", "cpp", "cs", "css", "d", "delphi", "diff", "xml", "django", "dos",
"erlang-repl", "erlang", "glsl", "go", "handlebars", "haskell", "http", "ini", "java", "javascript",
"json", "lisp", "lua", "markdown", "matlab", "mel", "nginx", "nohighlight", "objectivec", "parser3",
"perl", "php", "profile", "python", "r", "rib", "rsl", "ruby", "rust", "scala", "smalltalk", "sql",
"tex", "text", "vala", "vbscript", "vhdl"];
var acceptableCodeClasses;
function init() {
acceptableCodeClasses = Discourse.SiteSettings.highlighted_languages.split("|");
if (Discourse.SiteSettings.highlighted_languages.length > 0) {
var regexpSource = "^lang-(" + "nohighlight|auto|" + Discourse.SiteSettings.highlighted_languages + ")$";
Discourse.Markdown.whiteListTag('code', 'class', new RegExp(regexpSource, "i"));
}
}
if (Discourse.SiteSettings && Discourse.SiteSettings.highlighted_languages) {
init();
} else {
Discourse.initializer({initialize: init, name: 'load-acceptable-code-classes'});
}
var textCodeClasses = ["text", "pre", "plain"];
@@ -27,6 +36,7 @@ Discourse.Dialect.replaceBlock({
emitter: function(blockContents, matches) {
var klass = Discourse.SiteSettings.default_code_lang;
if (matches[1] && acceptableCodeClasses.indexOf(matches[1]) !== -1) {
klass = matches[1];
}
@@ -69,6 +79,3 @@ Discourse.Dialect.on('parseNode', function (event) {
}
});
// Whitelist the language classes
var regexpSource = "^lang-(" + acceptableCodeClasses.join('|') + ")$";
Discourse.Markdown.whiteListTag('code', 'class', new RegExp(regexpSource, "i"));
@@ -5,6 +5,6 @@ import loadScript from 'discourse/lib/load-script';
export default function highlightSyntax($elem) {
const selector = Discourse.SiteSettings.autohighlight_all_code ? 'pre code' : 'pre code[class]';
$(selector, $elem).each(function(i, e) {
loadScript("/javascripts/highlight.pack.js").then(() => hljs.highlightBlock(e));
loadScript(Discourse.HighlightJSPath).then(() => hljs.highlightBlock(e));
});
}
@@ -31,13 +31,19 @@ export default function loadScript(url, opts) {
resolve();
};
var cdnUrl = url;
if (Discourse.CDN && url[0] === "/") {
cdnUrl = Discourse.CDN + url;
}
// Some javascript depends on the path of where it is loaded (ace editor)
// to dynamically load more JS. In that case, add the `scriptTag: true`
// option.
if (opts.scriptTag) {
loadWithTag(url, cb);
loadWithTag(cdnUrl, cb);
} else {
$.getScript(url).then(cb);
$.getScript(cdnUrl).then(cb);
}
});
}
-5
View File
@@ -4,11 +4,6 @@
// Pagedown customizations
//= require ./pagedown_custom.js
// This is a BUG we should fix
// it is only required here cause preview is not loading it using LAB
//= require highlight.pack.js
//
// Stuff we need to load first
//= require ./discourse/lib/load-script
//= require ./discourse/lib/notification-levels