Skip to content

Commit

Permalink
Autoloader: Improved path detection and other minor improvements (#2245)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Apr 16, 2020
1 parent a197cfc commit 5cdc325
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
36 changes: 19 additions & 17 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
return;
}

// The dependencies map is built automatically with gulp
/**
* The dependencies map is built automatically with gulp.
*
* @type {Object<string, string | string[]>}
*/
var lang_dependencies = /*dependencies_placeholder[*/{
"javascript": "clike",
"actionscript": "javascript",
Expand Down Expand Up @@ -200,19 +204,21 @@

var script = Prism.util.currentScript();
if (script) {
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js$/i;
var prismFile = /[\w-]+\.(?:min\.)js$/i;
if (script.hasAttribute('data-autoloader-path')) {
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js(?:\?[^\r\n/]*)?$/i;
var prismFile = /(^|\/)[\w-]+\.(?:min\.)js(?:\?[^\r\n/]*)?$/i;

var autoloaderPath = script.getAttribute('data-autoloader-path');
if (autoloaderPath != null) {
// data-autoloader-path is set, so just use it
languages_path = script.getAttribute('data-autoloader-path').trim().replace(/\/?$/, '/');
languages_path = autoloaderPath.trim().replace(/\/?$/, '/');
} else {
var src = script.src;
if (autoloaderFile.test(src)) {
// the script is the original autoloader script in the usual Prism project structure
languages_path = src.replace(autoloaderFile, 'components/');
} else if (prismFile.test(src)) {
// the script is part of a bundle like a custom prism.js from the download page
languages_path = src.replace(prismFile, 'components/');
languages_path = src.replace(prismFile, '$1components/');
}
}
}
Expand Down Expand Up @@ -270,19 +276,15 @@
}

// Look for additional dependencies defined on the <code> or <pre> tags
var deps = elt.getAttribute('data-dependencies');
var parent = elt.parentElement;
if (!deps && parent && parent.tagName.toLowerCase() === 'pre') {
deps = parent.getAttribute('data-dependencies');
}

if (deps) {
deps = deps.split(/\s*,\s*/g);
} else {
deps = [];
var deps = (elt.getAttribute('data-dependencies') || '').trim();
if (!deps) {
var parent = elt.parentElement;
if (parent && parent.tagName.toLowerCase() === 'pre') {
deps = (parent.getAttribute('data-dependencies') || '').trim();
}
}

loadLanguages(deps, function () {
loadLanguages(deps ? deps.split(/\s*,\s*/g) : [], function () {
loadLanguage(lang, function () {
Prism.highlightElement(elt);
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cdc325

Please sign in to comment.