Expose the includeExplanation
option and/or export the tokensToHast
function
#568
Closed
3 of 4 tasks
Clear and concise description of the problem
includeExplanation
option would enabletokens
transformers to examine the explanations attached to highlighted tokens, which would facilitate building custom semantic highlighters with shiki.tokensToHast
function would help anyone usingcodeToThemedTokens
to transform their themed tokens to hast.Suggested solution
I'm using shiki to do highlighting of a custom extension to C/C++, and along the way, I implemented my own simple semantic highlighter for C/C++. I managed to port this work to the current shiki 1.0.0 beta version yesterday. But although my code works, it isn't as simple as I would like.
Ideally, I think something like the following code should work:
Unfortunately, this code doesn't work. The main issue is that
SemanticHighlight
uses the explanations attached to themed tokens to perform its semantic analysis of the code. ButcodeToHtml
seems to generate themed tokens without those explanations by default, so the semantic analysis fails. I haven't found a way change that default behavior ofcodeToHtml
, though I might have missed something.(Aside: I'm not sure if the
tokens
transformer above is correct — whether it's supposed to return a new set of themed tokens — since I couldn't find examples of such transformers.)Instead, I currently use the following code to incorporate the custom semantic highlighting:
The main issue with this code, however, is that shiki does not currently export the
tokensToHast
function. To make this code work, I copied the implementation oftokensToHast
from shiki into my own code. That code duplication is problematic for maintenance and seems like it should be easy to avoid. I also find it strange that shiki exportscodeToThemedTokens
andhastToHtml
but not the intermediatetokensToHast
step.If shiki either exposed the
includeExplanation
option throughcodeToHtml
/codeToHast
or exportedtokensToHast
, it would simplify and improve my code. These would seem to be simple changes to shiki that would enable some generally useful capabilities, but I don't know if there are fundamental issues with either change.Alternative
No response
Additional context
No response
Validations
Contributes
The text was updated successfully, but these errors were encountered: