Skip to content

Before highlighting code, resolve the language if its definition is already loaded, and it is not yet resolved#3951

Merged
DmitrySharabin merged 1 commit intosimplifyfrom
highlight-suggestion
May 29, 2025
Merged

Before highlighting code, resolve the language if its definition is already loaded, and it is not yet resolved#3951
DmitrySharabin merged 1 commit intosimplifyfrom
highlight-suggestion

Conversation

@DmitrySharabin
Copy link
Member

@DmitrySharabin DmitrySharabin commented May 29, 2025

For now, one can write a code that looks correct but will throw (necessary imports are omitted):

prism.languageRegistry.add(javascript);
prism.highlight("let foo = 42;", javascript);

@github-actions
Copy link

No JS Changes

Generated by 🚫 dangerJS against 6dd6a59

@DmitrySharabin DmitrySharabin requested a review from LeaVerou May 29, 2025 11:59
env.language = prism.languageRegistry.getLanguage(env.languageDef);
}

prism.hooks.run('before-tokenize', env);
Copy link
Member Author

Choose a reason for hiding this comment

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

I thought the hook should have the full context during the run, so I decided to add this code before calling it. Am I right?

};

if (env.languageDef && !env.language) {
env.language = prism.languageRegistry.getLanguage(env.languageDef);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
env.language = prism.languageRegistry.getLanguage(env.languageDef);
// NOTE is this the best place to manage this logic?
env.language = prism.languageRegistry.getLanguage(env.languageDef);

Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

Not 100% sure this is the best place to manage this logic, but in the interest of progress, let's merge it.

@DmitrySharabin
Copy link
Member Author

Me neither. I just couldn't find a better place. 🤷‍♂️

@DmitrySharabin DmitrySharabin merged commit b86c4e0 into simplify May 29, 2025
2 checks passed
@DmitrySharabin DmitrySharabin deleted the highlight-suggestion branch May 29, 2025 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants