Skip to content

Commit 5cdc325

Browse files
Autoloader: Improved path detection and other minor improvements (#2245)
1 parent a197cfc commit 5cdc325

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

Diff for: plugins/autoloader/prism-autoloader.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
return;
44
}
55

6-
// The dependencies map is built automatically with gulp
6+
/**
7+
* The dependencies map is built automatically with gulp.
8+
*
9+
* @type {Object<string, string | string[]>}
10+
*/
711
var lang_dependencies = /*dependencies_placeholder[*/{
812
"javascript": "clike",
913
"actionscript": "javascript",
@@ -200,19 +204,21 @@
200204

201205
var script = Prism.util.currentScript();
202206
if (script) {
203-
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js$/i;
204-
var prismFile = /[\w-]+\.(?:min\.)js$/i;
205-
if (script.hasAttribute('data-autoloader-path')) {
207+
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js(?:\?[^\r\n/]*)?$/i;
208+
var prismFile = /(^|\/)[\w-]+\.(?:min\.)js(?:\?[^\r\n/]*)?$/i;
209+
210+
var autoloaderPath = script.getAttribute('data-autoloader-path');
211+
if (autoloaderPath != null) {
206212
// data-autoloader-path is set, so just use it
207-
languages_path = script.getAttribute('data-autoloader-path').trim().replace(/\/?$/, '/');
213+
languages_path = autoloaderPath.trim().replace(/\/?$/, '/');
208214
} else {
209215
var src = script.src;
210216
if (autoloaderFile.test(src)) {
211217
// the script is the original autoloader script in the usual Prism project structure
212218
languages_path = src.replace(autoloaderFile, 'components/');
213219
} else if (prismFile.test(src)) {
214220
// the script is part of a bundle like a custom prism.js from the download page
215-
languages_path = src.replace(prismFile, 'components/');
221+
languages_path = src.replace(prismFile, '$1components/');
216222
}
217223
}
218224
}
@@ -270,19 +276,15 @@
270276
}
271277

272278
// Look for additional dependencies defined on the <code> or <pre> tags
273-
var deps = elt.getAttribute('data-dependencies');
274-
var parent = elt.parentElement;
275-
if (!deps && parent && parent.tagName.toLowerCase() === 'pre') {
276-
deps = parent.getAttribute('data-dependencies');
277-
}
278-
279-
if (deps) {
280-
deps = deps.split(/\s*,\s*/g);
281-
} else {
282-
deps = [];
279+
var deps = (elt.getAttribute('data-dependencies') || '').trim();
280+
if (!deps) {
281+
var parent = elt.parentElement;
282+
if (parent && parent.tagName.toLowerCase() === 'pre') {
283+
deps = (parent.getAttribute('data-dependencies') || '').trim();
284+
}
283285
}
284286

285-
loadLanguages(deps, function () {
287+
loadLanguages(deps ? deps.split(/\s*,\s*/g) : [], function () {
286288
loadLanguage(lang, function () {
287289
Prism.highlightElement(elt);
288290
});

Diff for: plugins/autoloader/prism-autoloader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)