-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
"Show Language" label #1108
Comments
It's already possible in the same way it's done in that Prism plugin. All you have to do is run over all the highlighted fragments after highlighting happens: hljs.initHighlightingOnLoad();
addEventListener('load', function() {
var blocks = document.querySelectorAll('pre code.hljs');
Array.prototype.forEach.call(blocks, function(block) {
var language = block.result.language;
});
}) A few caveats to note:
|
I'll go ahead and close it as it seems to be answered. Do feel free to re-open if you think we could/should do something to make such a solution easier. |
That would be another way to get it done. :-) I think the point here is how you'd do it programmatically. |
I was not able to get the label shown using the code in this thread. It would be great if you can show some working examples. |
I see it in your snap, what is the issue you're having exactly? |
I want to show the little language label on the top right corner. But I was not able find a working example. Google for highlight.js language label brought me here. @isagalaev in this thread mentioned it's done, and he showed some code snippet, but I was not able to get the label shown using his code. |
Because his code is no a complete example. hljs.initHighlightingOnLoad();
addEventListener('load', function() {
var blocks = document.querySelectorAll('pre code.hljs');
Array.prototype.forEach.call(blocks, function(block) {
var language = block.result.language;
// you need to use the DOM here to manipulate your HTML to add the label dynamically
});
}) |
Oh, I see. Thanks for the explanation. I was hoping as a user, we could do something like this: hljs.highlightBlock(block, {showLanguageLabel: true}); But I understand the complexity with so many different styles, what to do with the color of the label. maybe the main library should only provide this parameter, and it's the style's responsibility to implement this label. |
I see no need for this to be a feature of the core library when it's easy to achieve with 5-10 lines of Javascript code outside the library, or even as a plugin. This would great content for If you make a working https://jsfiddle.net/ for me I might be able to go provide you a working example. It'd be great if you wanted to contribute that back to the documentation I mentioned earlier. Or maybe I can use one I already have... |
Tested in latest Chrome. addEventListener('load', function() {
var blocks = document.querySelectorAll('pre code.hljs');
Array.prototype.forEach.call(blocks, function(block) {
var language = block.result.language;
block.insertAdjacentHTML("afterbegin",`<label>${language}</label>`)
});
}) Just one way. With no additional tags at all... if you want to do it differently you'd just have to write the correct code to manipulate the DOM to get the HTML you desire. And since it's impossible for us to know what that is, this is best left to the user. |
https://jsfiddle.net/a79spbd0/ Thanks for the code example. It works! |
Here's a highlight.js plugin I wrote to do this for my own needs: https://github.com/crpietschmann/hljslanguagedisplayplugin |
Would it be possible to add a label to the code sections, showing the reader what the language of the code is?
Basically, it would be great to have something like Prism's Show Language plugin for highlight.js: http://prismjs.com/plugins/show-language/
The text was updated successfully, but these errors were encountered: