Feature preview: variants

136 views
Skip to first unread message

Ivan Sagalaev

unread,
Dec 3, 2013, 4:19:50 AM12/3/13
to highl...@googlegroups.com
Hello everyone!

I invented a new syntax feature that should both improve readability of
language files and reduce size of the compressed file. Right now I'm in
the middle of converting language files and it's going to take a while
before I could publish it without breaking everything so I thought of
just describing it here.

We currently have a lot of things like this:

var STRINGS = [
{
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE]
//...
},
{
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE]
// ...
},
// repeat 5 more times
];

And then when you want to reuse those STRINGS you should use very ugly
`concat` call, sometimes even chaining them:

{
contains: STRINGS.concat(NUMBERS.concat([
// other submodes
]))
}

Now this will look much more streamlined:

var STRING = {
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE]
variants: [
{ ... }, // only the relevant bits that actually differ
{ ... },
]
}

And it can be used right inside the `contains`:

{
contains: [
STRING,
NUMBER,
// other submodes
]
}

And the implementation turns out to be surprisingly easy, constrained to
a single spot in mode compilation.

Ivan Sagalaev

unread,
Dec 3, 2013, 5:04:42 AM12/3/13
to highl...@googlegroups.com
OK, this took much less time than I anticipated. It's in:
https://github.com/isagalaev/highlight.js/commit/c67f646ab2d9f159d4d200be656b1460e886a15f

Back to < 30K for the :common browser build :-).
Reply all
Reply to author
Forward
0 new messages