Skip to content

Commit db0b5d9

Browse files
Move Prism config to a separate file and fix the build options of the global bundle
Co-Authored-By: Dmitry Sharabin <[email protected]>
1 parent 3ead3be commit db0b5d9

File tree

5 files changed

+52
-45
lines changed

5 files changed

+52
-45
lines changed

scripts/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ async function buildJS () {
424424
...defaultOutputOptions,
425425
format: 'iife',
426426
name: 'Prism',
427-
exports: 'named',
427+
exports: 'default',
428+
extend: true,
428429
},
429430
},
430431
};

src/auto-start.ts

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,10 @@ import { documentReady } from './util/async';
44

55
Prism.components.add(autoloader);
66

7-
export const PrismConfig = {
8-
// TODO: Update docs
9-
/**
10-
* By default, Prism will attempt to highlight all code elements (by calling {@link Prism#highlightAll}) on the
11-
* current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
12-
* additional languages or plugins yourself.
13-
*
14-
* By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
15-
*
16-
* You obviously have to change this value before the automatic highlighting started. To do this, you can add an
17-
* empty Prism object into the global scope before loading the Prism script like this:
18-
*
19-
* ```js
20-
* window.Prism = window.Prism || {};
21-
* Prism.manual = true;
22-
* // add a new <script> to load Prism's script
23-
* ```
24-
*
25-
* @default false
26-
*/
27-
manual: false,
28-
};
29-
30-
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
31-
// Get current script and highlight
32-
const script = document.currentScript as HTMLScriptElement | null;
33-
if (script && script.hasAttribute('data-manual')) {
34-
PrismConfig.manual = true;
35-
}
36-
37-
void documentReady().then(() => {
38-
if (!PrismConfig.manual) {
39-
Prism.highlightAll();
40-
}
41-
});
42-
43-
// Make Prism available globally
44-
if (typeof globalThis !== 'undefined') {
45-
(globalThis as any).Prism = Prism;
7+
void documentReady().then(() => {
8+
if (!Prism.config.manual) {
9+
Prism.highlightAll();
4610
}
47-
}
48-
else {
49-
PrismConfig.manual = true;
50-
}
11+
});
5112

5213
export default Prism;

src/config.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const hasDOM = typeof document !== 'undefined' && typeof window !== 'undefined';
2+
const scriptElement: HTMLOrSVGScriptElement | null = hasDOM ? document.currentScript : null;
3+
const globalConfig: Record<string, PrismConfig[keyof PrismConfig] | null> =
4+
// @ts-ignore
5+
globalThis.Prism?.constructor?.name === 'Object' ? globalThis.Prism : {};
6+
7+
function getGlobalSetting (name: string) {
8+
// eslint-disable-next-line regexp/no-unused-capturing-group
9+
const camelCaseName = name.replace(/-([a-z])/g, g => g[1].toUpperCase());
10+
11+
if (camelCaseName in globalConfig) {
12+
return globalConfig[camelCaseName];
13+
}
14+
else if (name in globalConfig) {
15+
return globalConfig[name];
16+
}
17+
else if (hasDOM) {
18+
return (
19+
scriptElement?.dataset[camelCaseName] ??
20+
document.querySelector(`[data-prism-${name}]`)?.getAttribute(`data-prism-${name}`)
21+
);
22+
}
23+
}
24+
25+
function getGlobalBooleanSetting (name: string, defaultValue: boolean): boolean {
26+
const value = getGlobalSetting(name);
27+
28+
if (value === null || value === undefined) {
29+
return defaultValue;
30+
}
31+
32+
return !(value === false || value === 'false');
33+
}
34+
35+
export interface PrismConfig {
36+
manual?: boolean;
37+
}
38+
39+
export const globalDefaults: PrismConfig = {
40+
manual: getGlobalBooleanSetting('manual', !hasDOM),
41+
};
42+
43+
export default globalDefaults;

src/core/classes/prism.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import globalDefaults from '../../config';
12
import { highlight } from '../highlight';
23
import { highlightAll } from '../highlight-all';
34
import { highlightElement } from '../highlight-element';
@@ -18,6 +19,7 @@ export default class Prism {
1819
hooks = new Hooks();
1920
components = new Registry(this);
2021
plugins: Record<string, unknown> = {};
22+
config = globalDefaults;
2123

2224
/**
2325
* See {@link highlightAll}.

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import globalPrism from './auto-start';
33

44
export * from './core';
55
export { loadLanguages } from './load-languages';
6-
export { PrismConfig } from './auto-start';
6+
export { PrismConfig } from './config';
77
export default globalPrism;

0 commit comments

Comments
 (0)