Skip to content
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

Line numbers not showing when using Prism.highlight #3865

Open
lukehutch opened this issue Mar 4, 2025 · 0 comments
Open

Line numbers not showing when using Prism.highlight #3865

lukehutch opened this issue Mar 4, 2025 · 0 comments

Comments

@lukehutch
Copy link

Information:

  • Prism version: latest from the download page
  • Plugins: line-numbers
  • Environment: Chrome on macOS

Description

I am using the following code to programmatically add some code to the DOM and then highlight it. The line-numbers plugin is not being called (the code does not show line numbers). What am I doing wrong?

        function showSourceCodeModal() {
            // Create a container for the source code
            const codeContainer = document.createElement('div');
            codeContainer.className = 'source-code-content';

            // Create fullscreen container with source code
            const { container, toggleFullscreen } = createFullscreenContainer(codeContainer);

            // Append to body
            document.body.appendChild(container);

            // Add the code with syntax highlighting
            if (currentSourceCode) {
                var pre = document.createElement('pre');
                pre.className = 'line-numbers language-javascript';
                var code = document.createElement('code');
                code.className = 'language-javascript';
                code.innerHTML = Prism.highlight(currentSourceCode, Prism.languages.javascript, 'javascript');
                pre.appendChild(code);
                codeContainer.appendChild(pre);
            } else {
                codeContainer.innerHTML = '<p>No source code available</p>';
            }

            setTimeout(toggleFullscreen, 50);
        }

It seems that Prism.highlight is not properly calling the line-numbers plugin. I have matching .js and .css files from the Prism download page that have line numbers enabled.

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

No branches or pull requests

1 participant