8000 [Line Numbers] Automatically attach to the global Prism instance by DmitrySharabin · Pull Request #3984 · PrismJS/prism · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Line Numbers] Automatically attach to the global Prism instance #3984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

DmitrySharabin
Copy link
Contributor
@DmitrySharabin DmitrySharabin commented Jul 12, 2025

Copy link
github-actions bot commented Jul 12, 2025

No JS Changes

Generated by 🚫 dangerJS against 8562579


export default Self;

if (typeof document !== 'undefined') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this check, Rollup chokes on bundling the code (when splitting it into chunks): in documentReady, it tries to access a property of an undefined document.

@LeaVerou
Copy link
Member

Sorry, I meant to approve the other one!

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.

See comment

void documentReady().then(() => {
const Prism = (globalThis as any).Prism;
if (Prism) {
Prism.components.add(Self);
Copy link
Member

Choose a reason for hiding this comment

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

We should not be inflicting all this boilerplate on plugin authors!
As a design principle, syntax should not be more than the minimum required to declare intent. Boilerplate should be handled by suitable abstractions.

Besides the length and maintainability, whoever is including a plugin (either a Prism user or the plugin author adding the plugin to the global instance) should not need to care about when the document is ready. The Prism method one calls to add a plugin should automatically take care of that.

Also, ESM automatically takes care of running JS when the document is ready, so when using ESM one rarely needs to use a documentReady() event. Is this for the global Rollup build? If so, we should approach it differently (but still in an abstraction, not every single plugin). Additionally, not all plugins even need to wait for the document to be ready, since most just add code to hooks, and the hooks already run at the right time.

As a priority of constituencies, it's users > Prism users > Plugin users > Prism maintainers.

All a plugin should need to do is something like:

import prism from "/path/to/global/prism"

export default const Self = { /* plugin */ };

prism.components.add(Self);

or even

import prism from "/path/to/global/prism"

export default prism.components.add({ /* plugin */ });

Base automatically changed from plugins-prettier to v2 July 12, 2025 20:16
Copy link
netlify bot commented Jul 12, 2025

Deploy Preview for dev-prismjs-com ready!

Name Link
🔨 Latest commit 8562579
🔍 Latest deploy log https://app.netlify.com/projects/dev-prismjs-com/deploys/6872c39eab194a00089c24de
😎 Deploy Preview https://deploy-preview-3984--dev-prismjs-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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
0