Fix ERB template highlighting#262
Fix ERB template highlighting#262jonatanklosko wants to merge 1 commit intoFormidableLabs:masterfrom
Conversation
|
|
|
||
| prism.hooks.run("before-tokenize", prismConfig) | ||
| prismConfig.tokens = prism.tokenize(code, grammar) | ||
| prismConfig.tokens = prism.tokenize(prismConfig.code, prismConfig.grammar) |
There was a problem hiding this comment.
See the original Prism highlight implementation as a reference: https://github.com/PrismJS/prism/blob/v1.30.0/prism.js#L661-L670.
Calling before-tokenize above modifies prismConfig.code. For example, given this snippet:
<div class="flex gap-2">
<%= link_to "Controller error", page_controller_error_path,
class: "btn btn-error btn-soft" %>
<%= link_to "View error", page_view_error_path,
class: "btn btn-error btn-soft" %>
<%= link_to "View sytnax error", page_view_syntax_error_path,
class: "btn btn-error btn-soft" %>
<%= link_to "Missing view error", page_missing_view_error_path,
class: "btn btn-error btn-soft" %>
<%= link_to "Route error", "/page/unknown-route",
class: "btn btn-error btn-soft" %>
</div>
before-tokenize rewrites the code to:
<div class="flex gap-2">
___ERB0___
___ERB1___
___ERB2___
___ERB3___
___ERB4___
</div>
it is expected that we pass the modified code to prism.tokenize, rather than the original code, as currently done.
Currently ERB (Ruby templates) are highlighted wrongly as if they where entirely Ruby. This bug probably applies to other mixed grammars.
This was an issue in the past and was fixed in #105, but during subsequent refactoring a regression was introduced.