Skip to content

Commit a7c23c7

Browse files
yutingzhao1991tingzhao.ytzstevenjoezhangyoshinorin
authored
refactor: remove alias json for support add custom language support (#414)
* refactor: remove alias json for support add custom language support * test: add test case for highlight.js registerLanguage * Update highlight.spec.ts Signed-off-by: Mimi <stevenjoezhang@gmail.com> * fix: review issue for test case * Revert "refactor: remove alias json for support add custom language support" This reverts commit 256c483. * feat: use hljs.getLanguage for judge lang support * chore: update format * chore: run eslint --fix --------- Signed-off-by: Mimi <stevenjoezhang@gmail.com> Co-authored-by: tingzhao.ytz <tingzhao.ytz@antgroup.com> Co-authored-by: Mimi <stevenjoezhang@gmail.com> Co-authored-by: yoshinorin <yoshinorin.net@outlook.com>
1 parent 37dab11 commit a7c23c7

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function highlight(str: string, options: Options) {
125125
return closeTags(result);
126126
}
127127

128-
if (!lang || !alias.aliases[lang]) {
128+
if (!lang || (!alias.aliases[lang] && !hljs.getLanguage(lang))) {
129129
lang = 'plaintext';
130130
}
131131

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"domhandler": "^5.0.3",
4848
"eslint": "^8.23.0",
4949
"eslint-config-hexo": "^5.0.0",
50+
"highlightjs-solidity": "^2.0.6",
5051
"html-entities": "^2.3.3",
5152
"html-tag-validator": "^1.6.0",
5253
"mocha": "^10.0.0",

test/highlight.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import chai from 'chai';
22
import hljs from 'highlight.js';
3+
import { solidity } from 'highlightjs-solidity';
34
import { encode } from 'html-entities';
45
import highlight from '../lib/highlight';
56

@@ -181,6 +182,42 @@ describe('highlight', () => {
181182
validateHtmlAsync(result, done);
182183
});
183184

185+
186+
it('lang = solidity', done => {
187+
hljs.registerLanguage('solidity', solidity);
188+
const solString = 'contract MyContract {}';
189+
const result = highlight(solString, { lang: 'solidity' });
190+
191+
result.should.eql(
192+
[
193+
'<figure class="highlight solidity"><table><tr>',
194+
gutter(1, 1),
195+
code(solString, 'solidity'),
196+
end
197+
].join('')
198+
);
199+
validateHtmlAsync(result, done);
200+
hljs.unregisterLanguage('solidity');
201+
});
202+
203+
it('lang = sol', done => {
204+
hljs.registerLanguage('solidity', solidity);
205+
const solString = 'contract MyContract {}';
206+
const result = highlight(solString, { lang: 'sol' });
207+
208+
result.should.eql(
209+
[
210+
'<figure class="highlight sol"><table><tr>',
211+
gutter(1, 1),
212+
code(solString, 'solidity'),
213+
end
214+
].join('')
215+
);
216+
validateHtmlAsync(result, done);
217+
218+
hljs.unregisterLanguage('solidity');
219+
});
220+
184221
it('auto detect', done => {
185222
const result = highlight(testString, {autoDetect: true});
186223

0 commit comments

Comments
 (0)