tempestphp / highlight

🎨 Fast, extensible, server-side code highlighting for web and terminal
https://tempest.stitcher.io/highlight/01-getting-started
MIT License
634 stars 37 forks source link

Additional tokens for more detailed highlighting #73

Closed tabuna closed 5 months ago

tabuna commented 6 months ago

You may have noticed that the code lacks highlighting for various types of values, such as strings, numbers, and boolean constants. Unlike some libraries (for example, prismjs), where such highlighting is already implemented, in tempestphp, this does not happen, and as a result, values and the return operators are marked with the same style - as keywords.

Here's an example of how prismjs marks:

<span class="token number">30</span>
<span class="token constant boolean">false</span>
<span class="token string double-quoted-string">"TaskScreen"</span>

To illustrate, let's take the following code:

/**
 * Get the permission key for the resource.
 *
 * @return string|null
 */
public static function permission(): ?string
{
    return null;
}

In GitHub's syntax highlighting, we see that return and null are different in color. However, when using the library, this does not happen because both elements have the CSS selector hl-keyword. Example:

image

As a potential solution, could we add more selectors to specify the element?

brendt commented 6 months ago

As a potential solution, could we add more selectors to specify the element?

We could. I'm not entirely against it. Although in my personal preference, more colors don't necessarily mean better readability. I based the initial token set on what I've been using and finetuning on my blog and in my IDE for years. I think it strikes a good balance.

However, adding tokens and more rules isn't all that difficult. We should consider breaking changes though. If we change the token of an existing keyword, that is a breaking change.

I'll keep this issue open, but I don't intend to work on it soon. I will reconsider it when we're working on a v2.

binaryfire commented 6 months ago

I'd just like to add my voice to this request. I'd love to migrate to this from Highlight.js but I can't unless I can achieve the same level of highlighting.

The concept of a native PHP highlighter is awesome, but I think it'll have very limited appeal if the colorization isn't on the same level of what the JS solutions offer (Highlight.js, Shiki etc.). It's just not the UI end-users are looking for in apps.

brendt commented 6 months ago

Given that I've added a bunch of features for v2, I've removed the v2 milestone from this issue. I still plan on looking into it for a future major version, but it's not high on my priority list. If someone want to put in the work, I'm happy to review and merge a PR.

brendt commented 5 months ago

Right now, I don't have any plans on adding new tokens. However, it's pretty trivial to add your own, I wrote a detailed guide on how to do it here: https://tempest.stitcher.io/highlight/03-adding-tokens