Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoloader: Improved path detection + other minor improvements #2245

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.