question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No Syntax highlighting when built

See original GitHub issue

Describe the bug Currently when I am working in my dev environment and the syntax highlighting is working as expected, however when I build the app there is no syntax highlighting and it just renders the string that was passed in (using CRA btw)

To Reproduce The repo can be found here: https://github.com/Adam-Collier/rownan-react/tree/build_app

you can try it in dev npm run dev

and build npm run build && npm run electron

Expected behavior I expected the syntax highlighting to be identical to that in development

Screenshots

import React from 'react'
import { useAppState } from '../state-context'
import styled from 'styled-components'

import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'
import { githubGist } from 'react-syntax-highlighter/dist/esm/styles/hljs'
import html from 'react-syntax-highlighter/dist/esm/languages/hljs/htmlbars'

const CodeEditor = styled.div`
  overflow: scroll;
  background-color: rgb(245, 242, 240);
  height: 100vh;
  code {
    font-family: 'Fira Mono';
    font-size: 14px;
  }
  pre {
    padding: 35px 25px 25px 25px;
    margin-top: 0;
  }
`

const CodePreview = () => {
  const { outputHTML } = useAppState()

  SyntaxHighlighter.registerLanguage('html', html)

  return (
    <CodeEditor className="CodeMirror">
      <SyntaxHighlighter
        language="html"
        style={githubGist}
      >
        {outputHTML}
      </SyntaxHighlighter>
    </CodeEditor>
  )
}

export default CodePreview

Before it is built:

image

After it is built: image

what is in the inspector: image

Desktop (please complete the following information):

  • OS: Mac
  • Electron App

I have no idea why it would do this, hope this can be solved

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

8reactions
Adam13531commented, Jun 18, 2020

Oh boy, I just spent a couple of hours debugging what was essentially this exact same problem. 😳 I made progress and I think I have answers for anyone who stumbles on this in the future.

I’m using NextJS with static exports and react-syntax-highlighter, and I was trying to highlight HTML with nested JavaScript in a <script> tag (that’s the only real difference from @Adam-Collier’s original problem, which was HTML with a nested style tag instead of a script tag).

Here are my findings:

  • The underlying problem is due to how highlightjs (a dependency of react-syntax-highlighter) works—the concept of “sub-languages”. This is when one language (like HTML) includes another (like JavaScript or CSS). In a development build, tree-shaking is usually turned off, so calling registerLanguage('html', htmlbars) is likely enough to include its sub-languages (xml, css, javascript, and potentially others). However, in production, tree-shaking results in them being discarded.
  • The solution is to explicitly register each sub-language. However, htmlbars is intended specifically for Handlebars, not HTML, so I omitted that and instead manifested the two languages I was using: HTML and JavaScript. This is obfuscated slightly because HTML doesn’t actually exist as a standalone language in highlightjs; it’s an alias of XML. Thus, the final imports and registerLanguage calls look like this:
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
import xml from 'react-syntax-highlighter/dist/cjs/languages/hljs/xml';
import javascript from 'react-syntax-highlighter/dist/cjs/languages/hljs/javascript';
SyntaxHighlighter.registerLanguage('xml', xml);
SyntaxHighlighter.registerLanguage('javascript', javascript);
  • If you need htmlbars, then you would import and register it in addition to the two languages above. Likewise with css.
  • I didn’t look into Prism, but I assume that it works because its underlying language files are done through refractor, which must operate differently. For anyone curious, here’s a working link to @Adam-Collier’s updated code where you can see it in action.

There was one unexplained portion for me, which is that for what looked like a single animation frame, my code did seem to render with proper syntax highlighting even in production (although it used the wrong theme). Afterward, it would revert to what’s shown in the original post’s images (where there’s a <code> DOM element with a raw string for its contents). I didn’t investigate this further since my original problem was just trying to add a code snippet to my blog, and I’m satisfied with the primary explanations.

1reaction
backuscommented, Mar 1, 2021

Perfect explanation @Adam13531, thanks for sharing how you solved the problem

Read more comments on GitHub >

github_iconTop Results From Across the Web

No Syntax highlighting when built · Issue #196 - GitHub
I'm using NextJS with static exports and react-syntax-highlighter , and I was trying to highlight HTML with nested JavaScript in a <script> tag ......
Read more >
c# - No syntax highlighting - Stack Overflow
I could get syntax highlighting working for .axml files, but not for Xamarin .cs files. Other .cs files, not generated in Xamarin, work...
Read more >
Javascript syntax highlighting it not working
I have an .html file which is part of a large project, and in the middle of the file, syntax highlighting just flat...
Read more >
Syntax highlighting - Wikipedia
Syntax highlighting is a form of secondary notation, since the highlights are not part of the text meaning, but serve to reinforce it....
Read more >
A case against syntax highlighting - Linus Åkesson
Do you rely on syntax highlighting when developing software? ... shoplifting, or checking in code without unit tests -- should be made hard....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found