Description
To ensure your grammar continues to function smoothly with future releases of Highlight.js you may need to make a few small changes. The good news is you can make all these changes today - and your grammar will continue to work with the v10 build system as well. You'll just be ready for v11 early.
Following our official spec makes it easy to generate a CDN ready dist
folder and also allows advanced users/integrators to build custom versions of Highlight.js that include your grammar in the main minified bundle with very little effort.
If you have some disagreement with the official spec or build process (and would also like to invest your time in finding a better solution) please continue that discussion here: Improving testing / integration / usage for 3rd party grammars This is not the thread for that larger discussion.
That topic died because at the time no one seemed interested in discussing or working on it.
So is v11 changing how 3rd party grammars work or are built?
Not really. Some early 3rd party grammars (perhaps your own) were invented side-by-side with the official spec and have always done things a "bit differently". The v10 build system thus has several hacks to "accommodate" these early grammars - allowing them to work as-is. These hacks will be removed from the v11 build system, requiring all grammar modules to provide a single export that can be understood by the Highlight.js build system (rollup).
Summary
- Your primary JS file should set only the default export, nothing further.
- ie, either
export default
ormodule.exports = ...
- this means no more
definer
exports, your primary JS file should not worry about the web browser context, that is what thedist
CDN builds are for - no more shims:
var module = module ? module : {};
- ie, either
- Your repository should include a
dist
folder with a CDN ready build of your grammar (built using the Highlight.js build system) See 3RD_PARTY_QUICK_START.md.md
An example grammar already meeting all requirements:
- https://github.com/highlightjs/highlightjs-robots-txt/
- https://github.com/highlightjs/highlightjs-robots-txt/blob/master/src/robots-txt.js
Spec Checklist
You can copy this template into your own project's issues area on GitHub:
- [ ] Your primary JS file should only set a default export, no others
- [ ] Remove any other definer and/or other additional exports
- [ ] Your repository should include a `dist` folder with pre-built CDN assets. Reference: [3RD_PARTY_QUICK_START.md.md](https://github.com/highlightjs/highlight.js/blob/master/extra/3RD_PARTY_QUICK_START.md)
- [ ] Remove any browser shims from your raw JS source
- [ ] Consider adding a `package.json` and publishing your grammar on [NPM](https://www.npmjs.com) if you aren't already
- [ ] Please update your `README.md` if necessary to reflect these changes
- [ ] Remove `charset="UTF-8"` from your examples, it should no longer be necessary with `dist` builds produced by our build tools
Browser shims
If your grammar has any shims like:
var module = module ? module : {}; // shim for browser use
Remove the shims entirely. The dist
CDN distributable is meant to solve this issue.
Simply export default
If you have a very specifically named define function:
function hljsDefineRpmSpecfile(hljs) {
return {
name: "RPM specfile"
This isn't necessary. It's actually not necessary to name this function at all.
What a simple export looks like (ES modules):
export default function(hljs) {
return {
name: "RPM specfile"
What a simple export looks like (CJS):
module.exports = function (hljs) {
return {
name: "RPM specfile"
Whether you use ES or CJS may depend on how you expect node
users to use your library (and whether you still want to support Node v10). The Highlight.js build system can build CDN distributable regardless of whether your source is in ES module or CJS format.
Exporting other things
If your grammar main JS file includes code like:
module.exports = function(hljs) {
hljs.registerLanguage('rpm-specfile', hljsDefineRpmSpecfile);
};
module.exports.definer = hljsDefineRpmSpecfile;
All this boilerplates can be removed. This is what the CDN ready distributable in dist
does for you, handles browser concerns. Just remove all this and export the single function, as shown above.