FormidableLabs / prism-react-renderer

🖌️ Renders highlighted Prism output to React (+ theming & vendored Prism)
MIT License
1.88k stars 153 forks source link

Improve tokenize #239

Closed AlmostBuddist closed 7 months ago

AlmostBuddist commented 9 months ago

I use this library for displays Python code, and it is truly one of the best libraries of its kind. But in most cases there are too few tokens, and I can’t find how to add them or set more rules for them. I would like to know if there is any mechanism for this or if you can add more tokens?

AlmostBuddist commented 9 months ago

For example, here I cannot color the “Button” function, since it is marked as plain image

I would also like to be able to color brackets separately from all categories, dividing them into round, curly and square, but they are added to the general category of punctuation marks, which is why periods and commas will be colored, and so on. image

Variables are also marked as plain... image

I would also like to color the properties after the dots separately somehow image

When listing parameters, I would also like to colorize the variables (like x and y here) Снимок

I apologize for my English, I'm bad at it, I hope I explained the problem clearly, I would like to get at least some answer

FIameCaster commented 9 months ago

To add more tokens, you can simply modify the Python grammar. Since prism-react-renderer exports its Prism instance, you can easily do this.

Below I'm adding most of what you mentioned:

import { Prism } from 'prism-react-renderer';

Prism.languages.insertBefore('python', 'punctuation', {
  'bracket': /[()]/,
  'square-bracket': /[[\]]/,
  'curly-bracket': /[{}]/,
  // Properties after dots
  'property-access': {
    pattern: /(\.)\w+/,
    lookbehind: true
  },
  // These can match things that aren't variables
  // So you might not want to add these two
  'capitalized-variable': /[A-Z]\w+/,
  'variable': /\w+/
});

You can name these tokens whatever you want as long as it doesn't collide with any of python's existing tokens.

I'd suggest you to read Prism's documentation on extending grammars.