shikijs / shiki

A beautiful yet powerful syntax highlighter
http://shiki.style/
MIT License
9.19k stars 330 forks source link

Detect Notation Transformers dynamically #705

Open fuma-nama opened 2 weeks ago

fuma-nama commented 2 weeks ago

Clear and concise description of the problem

Currently, Shiki uses a single regex matcher for notation transformers. It resulted in a few problems:

Suggested solution

To solve the problem, we can use a specific matcher depending on the format/language, and allow people to customise it.

For example, when processing a JavaScript file, we only support JSX and JS style comments.

About the exact implementation, I think we can add a matchers map for different languages. When cannot find a matcher for current format, it fallbacks to our current regex matcher which supports common comment syntaxes.

const matchers = {
  javascript: [/regex1/, /regex2/],
  typescript: [/regex1/, /regex2/], 
  csharp: [/regex1/],
  ...
}

People can pass their own matchers object to customise it.

Alternative

For some notation transformers, we can use the corresponding meta transformer instead. like line/word highlight have their meta transformers, but this is not supported for all notation transformers.

Additional context

No response

Validations

Contributes