Skip to content

Commit 88e46c1

Browse files
Improve plugin downloads
1 parent 9df679d commit 88e46c1

19 files changed

+139
-127
lines changed

README.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"layout": "home.njk",
3-
"resources": ["plugins/keep-markup/prism-keep-markup.js", "https://dev.prismjs.com/components/prism-bash.js"]
3+
"resources": [
4+
"/dist/plugins/keep-markup.js { type=module }",
5+
"/languages/bash.js { type=module }"
6+
]
47
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Prism is used on several websites, small and large. Some of them are:
2323
# Examples
2424

2525
The Prism source, highlighted with Prism (don’t you just love how meta this is?):
26-
<pre data-src="https://dev.prismjs.com/prism.js"></pre>
26+
<pre data-src="https://v2.dev.prismjs.com/dist/prism.js"></pre>
2727

2828
This page’s CSS code, highlighted with Prism:
2929

@@ -56,7 +56,7 @@ If you’re still not sold, you can [view more examples](examples.html) or [try
5656
- Highlights embedded languages (e.g. CSS inside HTML, JavaScript inside HTML).
5757
- Highlights inline code as well, not just code blocks.
5858
- It doesn’t force you to use any Prism-specific markup, not even a Prism-specific class name, only standard markup you should be using anyway. So, you can just try it for a while, remove it if you don’t like it and leave no traces behind.
59-
- Highlight specific lines and/or line ranges (requires [plugin](plugins/line-highlight/)).
59+
- Highlight specific lines and/or line ranges (requires [plugin](plugins/line-highlight/index.html)).
6060
- Show invisible characters like tabs, line breaks etc (requires [plugin](plugins/show-invisibles/)).
6161
- Autolink URLs and emails, use Markdown links in comments (requires [plugin](plugins/autolinker/)).
6262

@@ -221,7 +221,7 @@ This is the list of all {{ languages | length }} languages currently supported b
221221
<ul id="languages-list">
222222
{% for id, language in languages -%}
223223
<li data-id="{{ id }}">
224-
{{ language.title }}&nbsp;—
224+
{{ language.title }}&nbsp;—
225225
{%- for alias in language.alias -%}
226226
<code>{{ alias }}</code>{{ ", " if not loop.last }}
227227
{%- endfor %}

_data/eleventyComputed.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313

1414
for (let id in languages) {
1515
let ret = [id];
16-
let alias = languages[id].alias;
16+
let alias = Object.keys(languages[id].aliasTitles ?? {});
1717
if (alias) {
1818
ret = ret.concat(Array.isArray(alias) ? alias : [alias]);
1919
}
@@ -59,10 +59,10 @@ export default {
5959
}
6060

6161
// We are working with plugin resources
62-
ret.push(`./prism-${id}.js { type="module" }`);
62+
ret.push(`/dist/plugins/${id}.js { type="module" }`);
6363

6464
if (!data.noCSS) {
65-
ret.push(`./prism-${id}.css`);
65+
ret.push(`/dist/plugins/${id}.css`);
6666
}
6767

6868
return ret;

_layouts/page.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<meta charset="utf-8" />
1515
<link rel="icon" href="{{ base_url }}/assets/logo.svg" />
1616
<link rel="stylesheet" href="{{ base_url }}/assets/style.css" />
17-
<link rel="stylesheet" href="https://dev.prismjs.com/themes/prism.css" />
17+
<link rel="stylesheet" href="/themes/prism.css" />
1818
<script>var _gaq = [["_setAccount", "UA-33746269-1"], ["_trackPageview"]];</script>
1919
<script src="https://www.google-analytics.com/ga.js" async></script>
2020

@@ -89,7 +89,7 @@
8989
</nav>
9090
</footer>
9191

92-
<script src="https://dev.prismjs.com/prism.js"></script>
92+
<script src="https://v2.dev.prismjs.com/dist/prism.js"></script>
9393
{% if theme_switcher -%}
9494
<script src="{{ base_url }}/assets/theme-switcher.js" type="module"></script>
9595
{%- endif %}

_redirects

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
# Do not redirect
2-
/plugins/:plugin/index.html /plugins/:plugin/index.html 200
3-
/plugins/:plugin/demo.html /plugins/:plugin/demo.html 200
4-
/plugins/:plugin/demo.js /plugins/:plugin/demo.js 200
5-
/plugins/:plugin/demo.css /plugins/:plugin/demo.css 200
6-
71
# Components: languages, themes, plugins, etc.
8-
/components/* https://dev.prismjs.com/components/:splat 200
9-
/plugins/:plugin/:file https://dev.prismjs.com/plugins/:plugin/:file 200
10-
11-
# Make the autoloader plugin work
12-
/plugins/:plugin/components/* https://dev.prismjs.com/components/:splat 200
2+
/themes/:file https://v2.dev.prismjs.com/dist/themes/:file 301
3+
/languages/:file https://v2.dev.prismjs.com/dist/languages/:file 301
4+
/dist/plugins/:file https://v2.dev.prismjs.com/dist/plugins/:file 301

assets/download.js

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if (hstr) {
2727
}
2828
}
2929
if (category === "themes" && ids.length) {
30-
let themeInput = document.querySelector(`#theme input[value="${ ids[0] }"]`);
30+
let themeInput = document.querySelector(`#theme input[value="${ids[0]}"]`);
3131
if (themeInput) {
3232
themeInput.checked = true;
3333
themeInput.dispatchEvent(new Event("change"));
@@ -60,7 +60,9 @@ for (let category in components) {
6060
let all = components[category];
6161

6262
all.meta.section = form.querySelector(`#category-${category}`);
63-
all.meta.section.querySelector(`[name="check-all-${category}"]`)?.addEventListener("change", ({ target }) => {
63+
all.meta.section.querySelector(`[name="check-all-${category}"]`)?.addEventListener("change", ({
64+
target,
65+
}) => {
6466
all.meta.section.querySelectorAll(`input[name="download-${category}"]`).forEach(input => {
6567
all[input.value].enabled = input.checked = target.checked;
6668
});
@@ -80,8 +82,10 @@ for (let category in components) {
8082
let option = all[id].option || all.meta.option;
8183

8284
switch (option) {
83-
case "mandatory": disabled = true; // fallthrough
84-
case "default": checked = true;
85+
case "mandatory":
86+
disabled = true; // fallthrough
87+
case "default":
88+
checked = true;
8589
}
8690

8791
if (category === "themes" && storedTheme) {
@@ -99,7 +103,7 @@ for (let category in components) {
99103

100104
filepath = filepath.replace(/\{id\}/g, id);
101105

102-
let info = all[id] = {
106+
let info = (all[id] = {
103107
noCSS: all[id].noCSS || all.meta.noCSS,
104108
noJS: all[id].noJS || all.meta.noJS,
105109
enabled: checked,
@@ -110,7 +114,7 @@ for (let category in components) {
110114
paths: [],
111115
size: 0,
112116
},
113-
};
117+
});
114118

115119
info.require.forEach(v => {
116120
dependencies[v] = (dependencies[v] || []).concat(id);
@@ -137,7 +141,8 @@ for (let category in components) {
137141
});
138142
}
139143

140-
if (dependencies[id] && !target.checked) { // It’s required by others
144+
if (dependencies[id] && !target.checked) {
145+
// It’s required by others
141146
dependencies[id].forEach(dependent => {
142147
let input = form.querySelector(`label[data-id="${dependent}"] > input`);
143148
input.checked = false;
@@ -174,7 +179,7 @@ function getFileSize (category, id, filepath) {
174179
}
175180
}
176181

177-
function getFilesSizes() {
182+
function getFilesSizes () {
178183
for (let category in components) {
179184
let all = components[category];
180185

@@ -187,7 +192,7 @@ function getFilesSizes() {
187192
let files = distro.paths;
188193

189194
files.forEach(filepath => {
190-
let file = cache[filepath] = cache[filepath] || {};
195+
let file = (cache[filepath] = cache[filepath] || {});
191196

192197
if (!file.size) {
193198
let size = getFileSize(category, id, filepath);
@@ -206,13 +211,14 @@ function getFilesSizes() {
206211
}
207212
}
208213

209-
function prettySize(size) {
210-
return Math.round(100 * size / 1024) / 100 + "KB";
214+
function prettySize (size) {
215+
return Math.round((100 * size) / 1024) / 100 + "KB";
211216
}
212217

213-
function update(updatedCategory, updatedId) {
218+
function update (updatedCategory, updatedId) {
214219
// Update total size
215-
let total = { js: 0, css: 0 }; let updated = { js: 0, css: 0 };
220+
let total = { js: 0, css: 0 };
221+
let updated = { js: 0, css: 0 };
216222

217223
for (let category in components) {
218224
let all = components[category];
@@ -232,10 +238,11 @@ function update(updatedCategory, updatedId) {
232238
let size = file.size || 0;
233239

234240
if (info.enabled) {
235-
236241
if (!file.contentsPromise) {
237242
// FIXME: Remove “v2” when Prism v2 is released
238-
file.contentsPromise = getFileContents("https://v2.dev.prismjs.com/dist/" + path);
243+
file.contentsPromise = getFileContents(
244+
"https://v2.dev.prismjs.com/dist/" + path,
245+
);
239246
}
240247

241248
total[type] += size;
@@ -273,36 +280,37 @@ function update(updatedCategory, updatedId) {
273280

274281
Object.assign(form.querySelector(`label[data-id="${updatedId}"] .filesize`), {
275282
textContent: prettySize(updated.all),
276-
title: (updated.js ? Math.round(100 * updated.js / updated.all) + "% JavaScript" : "") +
283+
title:
284+
(updated.js ? Math.round((100 * updated.js) / updated.all) + "% JavaScript" : "") +
277285
(updated.js && updated.css ? " + " : "") +
278-
(updated.css ? Math.round(100 * updated.css / updated.all) + "% CSS" : "")
286+
(updated.css ? Math.round((100 * updated.css) / updated.all) + "% CSS" : ""),
279287
});
280288
}
281289

282290
form.querySelector("#filesize").textContent = prettySize(total.all);
283291

284292
Object.assign(form.querySelector("#percent-js"), {
285-
textContent: Math.round(100 * total.js / total.all) + "%",
286-
title: prettySize(total.js)
293+
textContent: Math.round((100 * total.js) / total.all) + "%",
294+
title: prettySize(total.js),
287295
});
288296

289297
Object.assign(form.querySelector("#percent-css"), {
290-
textContent: Math.round(100 * total.css / total.all) + "%",
291-
title: prettySize(total.css)
298+
textContent: Math.round((100 * total.css) / total.all) + "%",
299+
title: prettySize(total.css),
292300
});
293301

294302
delayedGenerateCode();
295303
}
296304

297305
// "debounce" multiple rapid requests to generate and highlight code
298-
function delayedGenerateCode() {
306+
function delayedGenerateCode () {
299307
if (timerId !== 0) {
300308
clearTimeout(timerId);
301309
}
302310
timerId = setTimeout(generateCode, 500);
303311
}
304312

305-
async function generateCode() {
313+
async function generateCode () {
306314
/** @type {CodePromiseInfo[]} */
307315
let promises = [];
308316
let redownload = {};
@@ -328,7 +336,7 @@ async function generateCode() {
328336
id: id,
329337
category: category,
330338
path: path,
331-
type: type
339+
type: type,
332340
});
333341
}
334342
});
@@ -369,11 +377,14 @@ async function generateCode() {
369377

370378
let newCode = Object.assign(document.createElement("code"), {
371379
className: codeElement.className,
372-
textContent: text
380+
textContent: text,
373381
});
374382

375-
Prism.highlightElement(newCode, false, () => {
376-
codeElement.replaceWith(newCode);
383+
Prism.highlightElement(newCode, {
384+
async: false,
385+
callback: () => {
386+
codeElement.replaceWith(newCode);
387+
},
377388
});
378389

379390
form.querySelector(`#download-${type} .download-button`).onclick = () => {
@@ -395,7 +406,7 @@ async function generateCode() {
395406
* @property {string} path
396407
* @property {string} type
397408
*/
398-
function buildCode(promises) {
409+
function buildCode (promises) {
399410
// sort the promises
400411

401412
/** @type {CodePromiseInfo[]} */
@@ -406,7 +417,8 @@ function buildCode(promises) {
406417
promises.forEach(p => {
407418
if (p.category === "core" || p.category === "themes") {
408419
finalPromises.push(p);
409-
} else {
420+
}
421+
else {
410422
let infos = toSortMap[p.id];
411423
if (!infos) {
412424
toSortMap[p.id] = infos = [];
@@ -416,12 +428,14 @@ function buildCode(promises) {
416428
});
417429

418430
// this assumes that the ids in `toSortMap` are complete under transitive requirements
419-
getLoader(components, Object.keys(toSortMap)).getIds().forEach(id => {
420-
if (!toSortMap[id]) {
421-
console.error(`${ id } not found.`);
422-
}
423-
finalPromises.push.apply(finalPromises, toSortMap[id]);
424-
});
431+
getLoader(components, Object.keys(toSortMap))
432+
.getIds()
433+
.forEach(id => {
434+
if (!toSortMap[id]) {
435+
console.error(`${id} not found.`);
436+
}
437+
finalPromises.push.apply(finalPromises, toSortMap[id]);
438+
});
425439
promises = finalPromises;
426440

427441
// build
@@ -434,18 +448,22 @@ function buildCode(promises) {
434448
if (i < l) {
435449
let p = promises[i];
436450
p.contentsPromise.then(function (contents) {
437-
code[p.type] += contents + (p.type === "js" && !/;\s*$/.test(contents) ? ";" : "") + "\n";
451+
code[p.type] +=
452+
contents + (p.type === "js" && !/;\s*$/.test(contents) ? ";" : "") + "\n";
438453
i++;
439454
f(resolve);
440455
});
441456
p.contentsPromise["catch"](function () {
442-
errors.push(Object.assign(document.createElement("p"), {
443-
textContent: `An error occurred while fetching the file "${ p.path }".`
444-
}));
457+
errors.push(
458+
Object.assign(document.createElement("p"), {
459+
textContent: `An error occurred while fetching the file "${p.path}".`,
460+
}),
461+
);
445462
i++;
446463
f(resolve);
447464
});
448-
} else {
465+
}
466+
else {
449467
resolve({ code: code, errors: errors });
450468
}
451469
};
@@ -456,7 +474,7 @@ function buildCode(promises) {
456474
/**
457475
* @returns {Promise<string>}
458476
*/
459-
async function getVersion() {
460-
let packageJSON = await getFileContents("https://dev.prismjs.com/package.json");
477+
async function getVersion () {
478+
let packageJSON = await getFileContents("https://v2.dev.prismjs.com/package.json");
461479
return JSON.parse(packageJSON).version;
462480
}

0 commit comments

Comments
 (0)