Skip to content

Commit 4b55bd6

Browse files
Made Match Braces and Custom Class compatible (#2947)
1 parent e8d3b50 commit 4b55bd6

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

plugins/custom-class/prism-custom-class.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
var prefixString = '';
3131

3232

33+
/**
34+
* @param {string} className
35+
* @param {string} language
36+
*/
37+
function apply(className, language) {
38+
return prefixString + (mapper ? mapper(className, language) : className);
39+
}
40+
41+
3342
Prism.plugins.customClass = {
3443
/**
3544
* Sets the function which can be used to add custom aliases to any token.
@@ -62,7 +71,16 @@
6271
*/
6372
prefix: function prefix(string) {
6473
prefixString = string || '';
65-
}
74+
},
75+
/**
76+
* Applies the current mapping and prefix to the given class name.
77+
*
78+
* @param {string} className A single class name.
79+
* @param {string} language The language of the code that contains this class name.
80+
*
81+
* If the language is unknown, pass `"none"`.
82+
*/
83+
apply: apply
6684
};
6785

6886
Prism.hooks.add('wrap', function (env) {
@@ -85,7 +103,7 @@
85103
}
86104

87105
env.classes = env.classes.map(function (c) {
88-
return prefixString + (mapper ? mapper(c, env.language) : c);
106+
return apply(c, env.language);
89107
});
90108
});
91109

plugins/custom-class/prism-custom-class.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/match-braces/prism-match-braces.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
return;
55
}
66

7+
function mapClassName(name) {
8+
var customClass = Prism.plugins.customClass;
9+
if (customClass) {
10+
return customClass.apply(name, 'none');
11+
} else {
12+
return name;
13+
}
14+
}
15+
716
var PARTNER = {
817
'(': ')',
918
'[': ']',
@@ -51,15 +60,15 @@
5160
}
5261

5362
[this, getPartnerBrace(this)].forEach(function (e) {
54-
e.classList.add('brace-hover');
63+
e.classList.add(mapClassName('brace-hover'));
5564
});
5665
}
5766
/**
5867
* @this {HTMLElement}
5968
*/
6069
function leaveBrace() {
6170
[this, getPartnerBrace(this)].forEach(function (e) {
62-
e.classList.remove('brace-hover');
71+
e.classList.remove(mapClassName('brace-hover'));
6372
});
6473
}
6574
/**
@@ -71,7 +80,7 @@
7180
}
7281

7382
[this, getPartnerBrace(this)].forEach(function (e) {
74-
e.classList.add('brace-selected');
83+
e.classList.add(mapClassName('brace-selected'));
7584
});
7685
}
7786

@@ -102,22 +111,25 @@
102111
pre.addEventListener('mousedown', function removeBraceSelected() {
103112
// the code element might have been replaced
104113
var code = pre.querySelector('code');
105-
Array.prototype.slice.call(code.querySelectorAll('.brace-selected')).forEach(function (e) {
106-
e.classList.remove('brace-selected');
114+
var className = mapClassName('brace-selected');
115+
Array.prototype.slice.call(code.querySelectorAll('.' + className)).forEach(function (e) {
116+
e.classList.remove(className);
107117
});
108118
});
109119
Object.defineProperty(pre, '__listenerAdded', { value: true });
110120
}
111121

112122
/** @type {HTMLSpanElement[]} */
113-
var punctuation = Array.prototype.slice.call(code.querySelectorAll('span.token.punctuation'));
123+
var punctuation = Array.prototype.slice.call(
124+
code.querySelectorAll('span.' + mapClassName('token') + '.' + mapClassName('punctuation'))
125+
);
114126

115127
/** @type {{ index: number, open: boolean, element: HTMLElement }[]} */
116128
var allBraces = [];
117129

118130
toMatch.forEach(function (open) {
119131
var close = PARTNER[open];
120-
var name = NAMES[open];
132+
var name = mapClassName(NAMES[open]);
121133

122134
/** @type {[number, number][]} */
123135
var pairs = [];
@@ -132,12 +144,12 @@
132144
if (text === open) {
133145
allBraces.push({ index: i, open: true, element: element });
134146
element.classList.add(name);
135-
element.classList.add('brace-open');
147+
element.classList.add(mapClassName('brace-open'));
136148
openStack.push(i);
137149
} else if (text === close) {
138150
allBraces.push({ index: i, open: false, element: element });
139151
element.classList.add(name);
140-
element.classList.add('brace-close');
152+
element.classList.add(mapClassName('brace-close'));
141153
if (openStack.length) {
142154
pairs.push([i, openStack.pop()]);
143155
}
@@ -166,11 +178,11 @@
166178
allBraces.sort(function (a, b) { return a.index - b.index; });
167179
allBraces.forEach(function (brace) {
168180
if (brace.open) {
169-
brace.element.classList.add('brace-level-' + (level % LEVEL_WARP + 1));
181+
brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
170182
level++;
171183
} else {
172184
level = Math.max(0, level - 1);
173-
brace.element.classList.add('brace-level-' + (level % LEVEL_WARP + 1));
185+
brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
174186
}
175187
});
176188
});

plugins/match-braces/prism-match-braces.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy