PrismJS / PrismJS/prism

What to do about Previewers in v2

Open
#3,543 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
13k
Forks
1.4k
Avg merge
15h 36m
Merged PRs (30d)
3

Description

Previewers is the only component that I haven't migrated yet. I started, but I don't know how to finish it. The problem is that Previewers works by modifying its target languages (CSS/S[AC]SS/Less/Stylus). This is not allowed in v2 because v2 does not support modify dependencies.

To be specific, those are the tokens Previewers adds:

previewTokens = {
    'gradient': {
        pattern: /(?:\b|\B-[a-z]{1,10}-)(?:repeating-)?(?:linear|radial)-gradient\((?:(?:hsl|rgb)a?\(.+?\)|[^\)])+\)/gi,
        inside: {
            'function': /[\w-]+(?=\()/,
            'punctuation': /[(),]/
        }
    },
    'angle': /(?:\b|\B-|(?=\B\.))(?:\d+(?:\.\d+)?|\.\d+)(?:deg|g?rad|turn)\b/i,
    // 'color' already gets added by CSS Extras
    'easing': {
        pattern: /\bcubic-bezier\((?:-?(?:\d+(?:\.\d+)?|\.\d+),\s*){3}-?(?:\d+(?:\.\d+)?|\.\d+)\)\B|\b(?:ease(?:-in)?(?:-out)?|linear)(?=\s|[;}]|$)/i,
        inside: {
            'function': /[\w-]+(?=\()/,
            'punctuation': /[(),]/
        }
    },
    'time': /(?:\b|\B-|(?=\B\.))(?:\d+(?:\.\d+)?|\.\d+)m?s\b/i
}

I can only think of 2 way to solve this:

  1. Copy the Previewer tokens into the target languages, so Previewers doesn't have to add them.
  2. Same as (1) but store the tokens as an optional dependency (previewers-extensions).

The problem with (1) is that increases the size and complexity of the those languages. (2) also does that just less, but (2) also results in a larger total complexity and size.

We could also "parse" the token streams of the target languages instead of adding new patterns to those grammars. E.g. it would be relatively simple to detect a number token followed by a unit token in a token stream, so we could implement time and angle as a simple after-tokenize hook. gradient and easingare a bit more difficult to parse, but also doable.

However, the after-tokenize hook doesn't work for embedded languages (e.g. CSS in HTML's style attribute), so we might have to go with a custom tokenizer (optional dependency).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing Previewers migration and the v2 restriction on modifying dependencies. Compare copying tokens, an optional previewers-extensions dependency, and token-stream or custom-tokenizer approaches, including the embedded-language limitation. Done means Previewers works in v2 while preserving gradient, angle, easing, and time highlighting.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.