Skip to content

Commit e7608f5

Browse files
committed
feat: optional prism autoload
Adds support to manually load tokenizer dependencies to bundle them instead of loading on the fly via CDN. - Skips prism autoload core + dependencies when `window.Prism` is already defined - Reverts `config.prismBaseUrl` (#34)
1 parent 7558e97 commit e7608f5

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Currently there are only limited [themes](https://github.com/andreruffert/syntax
7171
/**
7272
* @typedef Config
7373
* @type {object}
74-
* @property {string} prismBaseUrl - Prism base URL to fetch the tokenizer + language data.
7574
* @property {string[]} languages - Language grammars to highlight.
7675
* @property {{ [key: string]: string[] }} languageTokens - Language specific token types.
7776
*/
@@ -80,7 +79,6 @@ window.she = window.she || {};
8079

8180
/** @type {Config} */
8281
window.she.config = {
83-
prismBaseUrl: 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0', // Default
8482
languages: ['markup', 'css', 'javascript'], // Default
8583
};
8684
```

docs/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ <h1>Configuration</h1>
161161
<syntax-highlight language="js" id="copy-config">/**
162162
* @typedef Config
163163
* @type {object}
164-
* @property {string} prismBaseUrl - Prism base URL to fetch the tokenizer + language data.
165164
* @property {string[]} languages - Prism languages.
166165
* @property {{ [key: string]: string[] }} languageTokens - Language specific token types.
167166
*/
@@ -170,7 +169,6 @@ <h1>Configuration</h1>
170169

171170
/** @type {Config} */
172171
window.she.config = {
173-
prismBaseUrl: 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0', // Default
174172
languages: ['markup', 'css', 'javascript'], // Default
175173
};</syntax-highlight>
176174
<clipboard-copy for="copy-config" class="button" aria-label="Copy content">

index.html

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
<script>
3333
window.she = window.she || {};
3434
window.she.config = {
35-
prismBaseUrl: 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0', // Default
36-
languages: ['html', 'css', 'js', 'jsx', 'md'],
35+
languages: ['html', 'css', 'js', 'jsx', 'md'], // Default
3736
// Optional: language specific token type overwrites
3837
languageTokens: {
3938
css: ['important'],
@@ -81,12 +80,23 @@
8180
- unordered list item 2
8281
</syntax-highlight>
8382

84-
<syntax-highlight language="jsx">
85-
{/* ... */}
86-
&lt;Component>&lt;/Component&gt;
83+
<syntax-highlight language="jsx">{/* ... */}
84+
&lt;Component attr="value">
85+
{children}
86+
&lt;/Component&gt;
8787
</syntax-highlight>
8888

89-
<script type="module" src="/src/index.js"></script>
89+
<script type="module">
90+
// Manually loading prism dependencies
91+
// import 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-core.min.js'
92+
// import 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-markup.min.js'
93+
// import 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-css.min.js'
94+
// import 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-clike.min.js'
95+
// import 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-javascript.min.js'
96+
// import 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-markdown.min.js'
97+
98+
import '/src/index.js'
99+
</script>
90100
<script>
91101
document.addEventListener('DOMContentLoaded', () => {
92102
document.addEventListener('keyup', event => {

src/constants.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
export const NAMESPACE = 'she';
2-
export const DEFAULT_LANGUAGES = ['markup', 'css', 'javascript'];
3-
export const DEFAULT_PRISM_BASE_URL = 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0';
42

53
/**
64
* @typedef Config
75
* @type {object}
8-
* @property {string} prismBaseUrl - Prism base URL to fetch the tokenizer + language data.
96
* @property {string[]} languages - Prism languages.
107
* @property {{ [key: string]: string[] }} languageTokens - Language specific token types.
118
*/

src/syntax-highlight-element.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CONFIG, DEFAULT_LANGUAGES, DEFAULT_PRISM_BASE_URL } from './constants';
2-
import { loadPrismCore, loadPrismLanguage, setupTokenHighlights, tokenize } from './utils';
1+
import { CONFIG } from './constants';
2+
import { setupTokenHighlights, setupTokenizer, tokenize } from './utils';
33

44
const DEFAULT_TAG_NAME = 'syntax-highlight';
55

@@ -11,19 +11,10 @@ export class SyntaxHighlightElement extends HTMLElement {
1111
}
1212

1313
if (!registry.get(tagName)) {
14+
await setupTokenizer();
1415
setupTokenHighlights(CONFIG?.languageTokens || {});
15-
try {
16-
const prismBaseUrl = CONFIG?.prismBaseUrl || DEFAULT_PRISM_BASE_URL;
17-
await loadPrismCore(prismBaseUrl);
18-
await loadPrismLanguage({
19-
baseUrl: prismBaseUrl,
20-
language: CONFIG?.languages || DEFAULT_LANGUAGES,
21-
});
22-
registry.define(tagName, SyntaxHighlightElement);
23-
return SyntaxHighlightElement;
24-
} catch (error) {
25-
console.error(error);
26-
}
16+
registry.define(tagName, SyntaxHighlightElement);
17+
return SyntaxHighlightElement;
2718
}
2819
}
2920

src/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
import { CONFIG } from './constants';
2+
3+
export async function setupTokenizer() {
4+
try {
5+
if (!window.Prism) {
6+
const prismBaseUrl = 'https://cdn.jsdelivr.net/npm/prismjs@1.30.0';
7+
await loadPrismCore(prismBaseUrl);
8+
await loadPrismLanguage({
9+
baseUrl: prismBaseUrl,
10+
language: CONFIG?.languages || ['markup', 'css', 'javascript'],
11+
});
12+
}
13+
} catch (error) {
14+
console.error(error);
15+
}
16+
}
17+
118
/**
219
* Create & register the token `Highlight`'s in the `CSS.highlights` registry.
320
* This enables the use of `::highlight(tokenType)` in CSS to style them.

0 commit comments

Comments
 (0)