Skip to content

Commit 7a74497

Browse files
Made Autoloader and Diff Highlight compatible (#2580)
1 parent 0889bc7 commit 7a74497

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

plugins/autoloader/prism-autoloader.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,13 @@
460460
}
461461

462462
var deps = getDependencies(element);
463-
deps.push(language);
463+
if (/^diff-./i.test(language)) {
464+
// the "diff-xxxx" format is used by the Diff Highlight plugin
465+
deps.push('diff');
466+
deps.push(language.substr('diff-'.length));
467+
} else {
468+
deps.push(language);
469+
}
464470

465471
if (!deps.every(isLoaded)) {
466472
// the language or some dependencies aren't loaded

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.

plugins/diff-highlight/index.html

+24-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ <h1>How to use</h1>
2424

2525
<p>Replace the <code>language-diff</code> of your code block with a <code>language-diff-xxxx</code> class to enable syntax highlighting for diff blocks.</p>
2626

27-
<p>Optional:<br>
28-
You can add the <code>diff-highlight</code> class to your code block to indicate changes using the background color of a line rather than the color of the text.</p>
27+
<p>
28+
Optional:<br>
29+
You can add the <code>diff-highlight</code> class to your code block to indicate changes using the background color of a line rather than the color of the text.
30+
</p>
31+
32+
<h2>Autoloader</h2>
33+
34+
<p>The <a href="plugins/autoloader">Autoloader plugin</a> understands the <code>language-diff-xxxx</code> format and will ensure that the language definitions for both Diff and the code language are loaded.</p>
2935
</section>
3036

3137
<section>
@@ -63,12 +69,27 @@ <h1>Example</h1>
6369
+ const foo = bar.baz([1, 2, 3]) + 1;
6470
console.log(`foo: ${foo}`);</code></pre>
6571

72+
<p>
73+
Using <code>class="language-diff-rust diff-highlight"</code>:<br>
74+
(Autoloader is used to load the Rust language definition.)
75+
</p>
76+
77+
<pre><code class="language-diff-rust diff-highlight">@@ -111,6 +114,9 @@
78+
nasty_btree_map.insert(i, MyLeafNode(i));
79+
}
80+
81+
+ let mut zst_btree_map: BTreeMap&lt;(), ()> = BTreeMap::new();
82+
+ zst_btree_map.insert((), ());
83+
+
84+
// VecDeque
85+
let mut vec_deque = VecDeque::new();
86+
vec_deque.push_back(5);</code></pre>
6687
</section>
6788

6889
<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
6990

7091
<script src="prism.js"></script>
71-
<script src="components/prism-diff.js"></script>
92+
<script src="plugins/autoloader/prism-autoloader.js"></script>
7293
<script src="plugins/diff-highlight/prism-diff-highlight.js"></script>
7394
<script src="assets/utopia.js"></script>
7495
<script src="components.js"></script>

plugins/diff-highlight/prism-diff-highlight.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
(function () {
22

3-
if (typeof Prism === 'undefined' || !Prism.languages['diff']) {
3+
if (typeof Prism === 'undefined') {
44
return;
55
}
66

77

8-
var LANGUAGE_REGEX = /diff-([\w-]+)/i;
8+
var LANGUAGE_REGEX = /^diff-([\w-]+)/i;
99
var HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/gi;
1010
//this will match a line plus the line break while ignoring the line breaks HTML tags may contain.
1111
var HTML_LINE = RegExp(/(?:__|[^\r\n<])*(?:\r\n?|\n|(?:__|[^\r\n<])(?![^\r\n]))/.source.replace(/__/g, function () { return HTML_TAG.source; }), 'gi');
1212

13-
var PREFIXES = Prism.languages.diff.PREFIXES;
14-
13+
var warningLogged = false;
1514

1615
Prism.hooks.add('before-sanity-check', function (env) {
1716
var lang = env.language;
1817
if (LANGUAGE_REGEX.test(lang) && !env.grammar) {
19-
env.grammar = Prism.languages[lang] = Prism.languages['diff'];
18+
env.grammar = Prism.languages[lang] = Prism.languages.diff;
2019
}
2120
});
2221
Prism.hooks.add('before-tokenize', function (env) {
22+
if (!warningLogged && !Prism.languages.diff && !Prism.plugins.autoloader) {
23+
warningLogged = true;
24+
console.warn("Prism's Diff Highlight plugin requires the Diff language definition (prism-diff.js)." +
25+
"Make sure the language definition is loaded or use Prism's Autoloader plugin.");
26+
}
27+
2328
var lang = env.language;
2429
if (LANGUAGE_REGEX.test(lang) && !Prism.languages[lang]) {
25-
Prism.languages[lang] = Prism.languages['diff'];
30+
Prism.languages[lang] = Prism.languages.diff;
2631
}
2732
});
2833

@@ -39,8 +44,10 @@
3944
diffGrammar = Prism.languages[diffLanguage];
4045
}
4146

47+
var PREFIXES = Prism.languages.diff && Prism.languages.diff.PREFIXES;
48+
4249
// one of the diff tokens without any nested tokens
43-
if (env.type in PREFIXES) {
50+
if (PREFIXES && env.type in PREFIXES) {
4451
/** @type {string} */
4552
var content = env.content.replace(HTML_TAG, ''); // remove all HTML tags
4653

plugins/diff-highlight/prism-diff-highlight.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)