Skip to content

MagicComments should be applied after Prism tokenization to properly track line numbers #8550

Open

Description

@PoetaKodu

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

The way magic comments are currently applied (before passing the code to Prism) interferes with Prism after-tokenize hook (and possibly other hooks too). I implemented a way to manually specify type names for proper highlighting:

image

(this is a screenshot from the webpage)

This requires me to erase each line with a specific pattern:

// custom-syntactic-tweak-that-adds-types:Vehicle

This results in magic comments being shifted.

Reproducible demo

https://codesandbox.io/s/docusaurus-magiccomments-breaks-lines-n78f0r?file=/docusaurus.config.js

Steps to reproduce

To see the problem:

  1. Click on "Tutorial" to go to the intro doc.

How to reproduce it starting from a fresh installation:

  1. Using prism-include-languages.js hook to Prism after-tokenize and erase any token that contains a new line
  2. Test how // highlight-next-line works in a code block

Expected behavior

Magic comments are applied after the after-tokenize hook so that user-provided hooks work if lines are added or removed.

Actual behavior

Magic comments are applied before passing the code to the Prism tokenizer and this results in highlighted lines being shifted if a hook is used to add/remove tokens that contains new lines.

Your environment

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Jan 14, 2023
  2. added and removed
    status: needs triage
    on Jan 15, 2023
  3. Josh-Cena commented on Jan 15, 2023

    @Josh-Cena
    Collaborator

    Not sure how easy it is to implement, but accepting a PR to experiment. We can even move this logic to a Prism hook if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions