Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useTokenize = ({ prism, code, grammar, language }: Options) => {
}

prism.hooks.run("before-tokenize", prismConfig)
prismConfig.tokens = prism.tokenize(code, grammar)
prismConfig.tokens = prism.tokenize(prismConfig.code, prismConfig.grammar)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

prism.hooks.run("after-tokenize", prismConfig)
return normalizeTokens(prismConfig.tokens)
}, [
Expand Down