-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Improved markup, CSS, JS tokenization #1871
Comments
Thanks for taking the time to make this report! HTML: DOCTYPEAgreed. HTML: Treating quotes and equals differently than normal punctuation
CSS: URL valuesAgreed. (#1874) CSS: Arguments in selectorsIf you only need CSS: Keywords - ex. generic font family names, colors (maybe in CSS-Extra?)CSS has quite a few keywords, so I don't know if we want to highlight them all because this will increase the size of the CSS language definition. Also, we highlight everything besides the keywords, so wouldn't it be possible to just assume that everything which isn't highlighted is a keyword? JS: Regex sequences, regex escapingThe Regex language is what you're looking for. This language will add regex tokenization to JS (similar to how CSS-Extras adds new features to CSS). |
Regarding the quotes: I have 2 possible solutions: 1. Add a hook which gives the first Prism.hooks.add('wrap', function (env) {
if (env.type === 'punctuation' && env.content === '=') {
env.classes.push('special-punctuation-name');
}
}); Not tested but should work. This will also affect the The second solution is to modify the markup language definition: 'attr-value': {
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/i,
inside: {
'punctuation': /^=/,
'string': {
pattern: /(\s*)[\s\S]+/,
lookbehind: true
}
}
} I would careful with modifying the language definition because my other languages rely on markup, so this might cause problems. |
Regarding "HTML: Treating quotes and equals differently than normal punctuation": Another CSS-based solution is to use the fact that the .token.attr-value > .token.punctuation:first-child {
/* styles */
} |
Thanks, these solutions look mostly really good for a start. A couple of points:
Would it be an acceptable increase to add just the colors and generic font family names for now? If the size is too large, would it be viable to move these out to CSS Extras instead?
If you take a look at the screenshot from VS Code, you'll see that
Ah great, thanks for that, didn't know about that! |
I'll be sure to file any further issues if they come up! (Keeping an eye on pragmatism of course) |
Regarding "HTML: DOCTYPE": Adding tag-highlighting to the Instead of changing the language definition for everyone, it might be the simplest solution to delete this token for just you. VS Code treats the doctype like any other tag, AFAIK. |
Regarding CSS keywords: I see the issue with fonts and the like but thought that these minor false positives were acceptable. |
Hm... can you elaborate? Since the doctype is already being matched, is there no way of doing some kind of sub-matching on the group already?
Do you mean that if I create a custom rule, I can stop Prism from tokenizing the doctype and it will get similar highlighting to the other tags? For example, will Compared to a regular tag, it appears that the tag name and the attribute are similarly highlighted (the only difference is the |
Here's a little snippet which extends the highlighting of doctypes: Prism.languages.markup.doctype = {
pattern: Prism.languages.markup.doctype,
inside: Prism.util.clone(Prism.languages.markup.tag.inside)
};
Prism.languages.markup.doctype.inside.tag = {
pattern: /^<!DOCTYPE/i,
inside: {
'punctuation': /^<!/
}
}; The If you really don't care about the delete Prism.languages.markup['doctype'];
Prism.languages.markup['tag'].inside['tag'].inside['punctuation'] = /^<[!/]?/; Both snippets handle the And with Adding tag-highlighting to the doctype token significantly changes the look of Prism's HTML highlighting, I meant really just that. It looks different. |
Ah sorry I think there was a misunderstanding here.
Now I'm understanding! Adding highlighting of the doctype is a big change to the visual appearance of HTML files, and it's uncertain if this change is aligned with the goals of the Prism project. Got it! My understanding was that you meant that the internal implementation looked a lot different (significantly changes the look of Prism's HTML highlighting) and the code became more complex, causing worries about adding that complexity.
Yeah as long as it's possible with some extension, I'm fine with this route as well. |
Hi! First, thanks of all for this awesome project! It's affected so many projects around the world. Incredible work!
Motivation
I'm trying to recreate the default VS Code syntax highlighting with Prism.js (via prism-react-renderer) and I'm running into limitations of Prism.js.
Description
I would like to be able to highlight certain tokens that are not granular enough in HTML, CSS and JS.
Would it be something that the Prism.js team is interested in to have more granular tokenization of HTML, CSS and JS? If possible, also with backwards compatibility?
Some first quick examples of things that could be improved across HTML, CSS and JS (compared against VS Code
1.33.1
default dark mode):HTML: DOCTYPE
HTML: Treating quotes and equals differently than normal punctuation
CSS: URL values
CSS: Arguments in selectors
CSS: Keywords - ex. generic font family names, colors (maybe in CSS-Extra?)
JS: Regex sequences, regex escaping
The code used:
The text was updated successfully, but these errors were encountered: