Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore code blocks with unsupported languages #842

Closed
hubgit opened this issue Apr 28, 2015 · 11 comments
Closed

Ignore code blocks with unsupported languages #842

hubgit opened this issue Apr 28, 2015 · 11 comments

Comments

@hubgit
Copy link
Contributor

hubgit commented Apr 28, 2015

If a language is specified using a classname on a code block, but that language is unsupported by highlight.js (e.g. language-yaml), ignore this code block rather than trying to highlight it wrongly as a different language.

@Sannis
Copy link
Collaborator

Sannis commented Apr 28, 2015

You can use no-highlight class for that.

@hubgit
Copy link
Contributor Author

hubgit commented Apr 28, 2015

You can use no-highlight class for that.

That's a good workaround, but it means knowing in advance which languages are supported for highlighting, which can change over time as they get added.

@sourrust
Copy link
Member

The alternative way to turn off autodetection is with hljs.configure:

hljs.configure({ languages: [] });
hljs.initHighlighting();

That way if the language isn't there, highlight.js doesn't try to guess the language itself.

@sourrust
Copy link
Member

sourrust commented May 6, 2015

This is pretty much resolved so I'm closing this issue.

@sourrust sourrust closed this as completed May 6, 2015
@hubgit
Copy link
Contributor Author

hubgit commented May 6, 2015

The alternative way to turn off autodetection is with hljs.configure

Turning off autodetection would turn off autodetection completely, though - the aim of this issue is to keep autodetection enabled, but not to try to guess a different language when one is already specified.

@isagalaev
Copy link
Member

One problem here is that it'd be hard to solve in a good way. We could tell that a class name is a language name only if it's prefixed by "language-". However people mostly just use unprefixed names like "php", "cpp", which highlight.js can't distinguish from any other class names having nothing to do with languages.

I'd say rather then put a usecase-specific code into core it's better to use custom initialization on the client side.

@hubgit
Copy link
Contributor Author

hubgit commented May 6, 2015

If the class name is prefixed with language- then it is definitely specifying a language - and in that case, the autodetection could be turned off.

@isagalaev
Copy link
Member

Yes, I understand that. My point is that the prefix isn't used often enough to warrant a non-obvious special case condition in the core library. I may be very well wrong about it (I'm rather away from that part of code right now) so if it is easy to implement in a readable way I see no problem in fixing it. Could you try and make the fix? It should probably be somewhere around highlightBlock and blockLanguage functions.

@isagalaev isagalaev reopened this May 6, 2015
@hubgit
Copy link
Contributor Author

hubgit commented May 7, 2015

I've had a go at treating language-* as no-highlight if no supported language is present:
https://github.com/isagalaev/highlight.js/compare/master...hubgit:unsupported-language?expand=1

It's a bit less efficient, so up to you whether it makes sense to include - I'll make a pull request if it's useful.

@isagalaev
Copy link
Member

Okay, I've taken this change but while testing realized that it still would highlight blocks with classes like "c language-plain", so I've changed all that logic in dde27ca.

@hubgit
Copy link
Contributor Author

hubgit commented May 13, 2015

Brilliant, thanks @isagalaev 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants