-
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
Increase token granularity for improved syntax highlighting #1623
Comments
If you want more fine-grained control about the way keywords are highlighted, I recommend the Highlight keywords plugin. Also, |
Thanks, I didn't see that plugin. And the PR looks good. I'll create my own language extension and copy some of that code while the PR waits to be merged. Do you have ideas for the regex necessary for the other tokens listed in the OP? I'm really not great with regex and can't figure it out on my own. 😭 |
Insert this code into The codePrism.languages.insertBefore('javascript', 'punctuation', {
'definition': {
pattern: /([{,]\s*)[a-z]\w*(?=\s*:)/i,
lookbehind: true,
alias: 'property'
},
'access': {
pattern: /(\.\s*)[a-z]\w*/i,
lookbehind: true,
alias: 'property'
},
'variable ': {
pattern: /\b(?:window|document|navigator|performance|localStorage)\b/,
alias: 'dom'
},
'console': /\bconsole\b/,
'spread': {
pattern: /\.{3}/,
alias: 'punctuation'
}
}); But let me warn you:
Keep in mind that if you want to use the minified file, you will have to rebuild Prism (refer to |
Wow thank you so much. For the code snippets I'm using it's working pretty well. I appreciate it! |
Spread wasn't working, adding it before I was able to add However, the property definition doesn't work if there are comments within the object. {
// Here's a comment
property: true
} Any fix for that? Everything's working nicely apart from that 😄. Thanks tons for the help @RunDevelopment |
My bad.
Just insert them into the character sets. Change
That's a really tough one. We could adjust the lookbehind You can however say: My code is well-formatted as such: |
Yeah sorry I did add them to the character set and it works fine. I had a feeling the comment thing would make it really hard! Your final sentence works fine for my code though. Thanks! |
For anyone wondering, here are all the tokens I've hacked on to get pretty close to my VS code theme 😜. I'm also using Prism directly from GitHub to get the parameter highlighting. @RunDevelopment I kind of don't know what I'm doing with The Prism.languages.insertBefore('javascript', 'keyword', {
module: {
pattern: /\b(?:import|as|export|from|default)\b/,
alias: 'keyword',
},
op: {
pattern: /\b(?:typeof|new|of|delete)\b/,
alias: 'keyword',
},
nil: {
pattern: /\b(?:null|undefined)\b/,
alias: 'keyword',
},
flow: {
pattern: /\b(?:return|await)\b/,
alias: 'keyword',
},
func: {
pattern: /(\.\s*)[a-z_$][\w$]*(?=(\())/i,
lookbehind: true,
alias: 'method',
},
})
Prism.languages.insertBefore('javascript', 'punctuation', {
definition: {
pattern: /[a-z]\w*(?=:)/i,
lookbehind: true,
alias: 'property',
},
access: {
pattern: /(\.\s*)[a-z_$][\w$]*/i,
lookbehind: true,
alias: 'property',
},
dom: {
pattern: /\b(?:window|document|navigator|performance|localStorage)\b/,
alias: 'variable',
},
console: /\bconsole\b/,
class: {
pattern: /\b[A-Z][A-Za-z0-9_]+\b/,
alias: 'class-name',
},
})
Prism.languages.insertBefore('javascript', 'operator', {
spread: {
pattern: /\.{3}/,
alias: 'punctuation',
},
arrow: {
pattern: /=>/,
alias: 'operator',
},
})
Prism.languages.insertBefore('javascript', 'function', {
method: {
pattern: /(\.\s*)[a-z_$][\w$]*(?=(\())/i,
lookbehind: true,
alias: 'function',
},
}) Also one last thing I'd like to have: string highlighting in CSS attribute selectors. Example: .example[attr="value"] {
color: red;
} How would I make Here you can see tons of highlighting granularity as an example that you don't get by default with Prism: https://atomiks.github.io/tippyjs/tippy-instance/ |
I gotta say: The code on your website looks really good! |
@atomiks Can I use this for a new language JS Extras? |
Yup sure! You'd need to test it with a bunch of code samples of unexpected code (as you mentioned before with the |
Thank you very much! |
The following to highlight object properties no longer seems to work (latest Prism does not tokenize Prism.languages.insertBefore('javascript', 'punctuation', {
definition: {
pattern: /([{,]\s*)[a-z]\w*(?=\s*:)/i,
lookbehind: true,
alias: 'property',
},
});
const code = `
const object = {
property: true
};
`.trim();
Prism.highlight(code, Prism.languages.javascript, 'javascript'); Do you have an updated example? The last working version is |
Prism new supports JS object properties out of the box (#3099). If you want to keep using your -Prism.languages.insertBefore('javascript', 'punctuation', {
+Prism.languages.insertBefore('javascript', 'property', { |
Ah okay, I see that the change hasn't been released to npm yet. I'm also using I've added: Prism.languages.insertBefore('javascript', 'operator', {
'literal-property': {
pattern: /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,
lookbehind: true,
alias: 'property'
},
}); and it works. Thanks! |
It would be cool to add more tokens so different parts of the code can be highlighted differently. Prism seems more granular than CodeMirror, but it's still missing some nice tokens compared to Atom and VS Code.
For JavaScript, it would be nice to have these:
token parameter
token property definition
andtoken property access
token function method
token keyword module
All keywords here should receive the
module
token.token keyword special
"special"(?) keywords like
this
token variable dom
token console
token operator spread
The text was updated successfully, but these errors were encountered: