Why highlight.js language alias does not work?

40 views
Skip to first unread message

talha131

unread,
Apr 6, 2019, 7:14:26 AM4/6/19
to tiddl...@googlegroups.com

Highlight.js lets you use aliases for languages. It’s list of aliases is available here.

According to it, both of the following code blocks are similar

```javascript
```js

But in TW, js does not work for javascript, or any alias for other languages.

I looked into the source code of highlight.js plugin in TW.

After prettifying it, it was clear that aliases for each language were defined in the highlight.js source code.

Then I looked up the code of highlightblock.js.

What I understand, this line of code is used to see if language is supported by the plugin or not

if(language && hljs.listLanguages().indexOf(language) !== -1) {

listLanguages() method does not support aliases. It only returns a list of full name of supported languages.

Its API has another method getLanguage() that supports aliases. So

hljs.getLanguage('javascript') // Object
hljs.getLanguage('js') // Object
hljs.getLanguage('blahblah') // undefined

So changing the offending line with

if(language && hljs.getLanguage(language)) {

fixed the issue in my tests. I have submitted a PR with this patch

https://github.com/Jermolene/TiddlyWiki5/pull/3898/files

I am writing it in detail in here so that if my understanding is incorrect then someone would correct me and suggest a proper solution.

Reply all
Reply to author
Forward
0 new messages