question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Plugin to use custom class name

See original GitHub issue

Idea: I want to customize those classes generated by Prism for each token (for example, var in JS will be generated to <span class="token keyword">var</span>. I want to change the string “token keyword”.

Reason: Some situation require me to use locally scoped classes, like in a CSS Modules project or one that follow BEM Style.

Solution: I wrote a small plugin, based on Highlight Keyword, to customize those classes using a style-map object. The object looks like this:

var customStyleMap = {
  tokenType: className
}

with tokenType is type of token I want to customize (eg: 'keyword', 'string', 'operator'…) and className is the class string I want to use for that token (eg: 'my-special-class-for-token' or a hashed one like '_3ka93h'

Example Usage:

var customStyleMap = {
  keyword: 'special-keyword',
  string: 'special-string'
};
customizePrismClasses(customStyleMap);

Or we can use the object returned by CSS Modules:

var customStyleMap = require('my-custom-style-map.css');
customizePrismClasses(customStyleMap);

Then, the string var will become <span class="special-keyword">var</span>.

The source code is very simple, but I think it is useful, especially for project which we want to scope the class name locally, like CSS Modules projects.

Please tell me what do you guys think about this. I’ll make a pull request if you think it is useful enough.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
zeitgeist87commented, May 4, 2016

Prism autohighlight runs only after all other JS has finished. This should be fine:

<script src="prism.js"></script>
<script src="plugins/your-plugin/your-plugin.js"></script>
<script>
Prism.plugins.YourPlugin.customizeClasses({ .... });
</script>

Don’t add it to the wrap hook inside of customizeClasses. I would do it like this:

(function() {

var classMap;
Prism.plugins.YourPlugin = {
    customizeClasses: function (map) {
        // maybe merge classMap and map?
        classMap = map;
    }
}


Prism.hooks.add('wrap', function (env) {
    if (!classMap) {
        return;
    }

    /* use classMap */
});

}());
1reaction
zeitgeist87commented, May 4, 2016

It is possible, that __id doesn’t exist. It is added on the fly by the _.util.objId() function. In the rare event where insertBefore is never called you would have no __id fields:

This should work in all cases, but I havn’t tested it:

if (typeof language === 'undefined') {
    for (var key in Prism.languages) {
        if (_.util.objId(grammar) === _.util.objId(Prism.languages[key])) {
            language = key;
            break;
        }
    }
}

The question is, if we should make the language parameter for the highlight function optional. Inside Prism the parameter is always supplied. You could use the above code just as easily in your plugin or before you call the highlight function.

On the other hand we have examples on http://prismjs.com/ without the language parameter:

var Prism = require('prismjs');

// The code snippet you want to highlight, as a string
var code = "var data = 1;";

// Returns a highlighted HTML string
var html = Prism.highlight(code, Prism.languages.javascript);

But this example was added only a few days ago with a PR. @LeaVerou What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Class Prism plugins
Prism default classes are sensible but fixed and too generic. This plugin provide some ways to customize those classes to suit your needs....
Read more >
Custom Body Class – WordPress plugin
A plain simple plugin which allows you to add a custom CSS class the HTML body tag.
Read more >
Specify a custom class name - I18N Maven Plugin
Specify a custom class name. By default, the class name will be based on the I18N bundle file name. Specify a custom class...
Read more >
How to Add a Custom Class to a WordPress Menu Item
4. Enter your class name and save your menu to apply the class to the menu item ... Remember that you may need...
Read more >
How to Add Custom CSS Classes to WPForms
To add a custom CSS class name for either option, open the form builder and go to Settings » General. From here, go...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found