0

I would like to syntax highlight my SASS code with highlight.js NPM package. However, I cannot get it to work.

SASS is not syntax highlighted, and the console displays this warning:

a
  text-decoration: none
 
CSS does work:

a {
  text-decoration: none
 }

WARN: Could not find the language 'sass', did you forget to load/include a language module?
WARN: Falling back to no-highlight mode for this block. <code class="language-sass">
a
  text-decoration: none
 </code>

You can check it with this snippet:

hljs.highlightAll();
<script src="https://unpkg.com/@highlightjs/[email protected]/highlight.min.js"></script>
<link rel="stylesheet"
      href="//unpkg.com/@highlightjs/[email protected]/styles/nnfx-dark.min.css">

<p>SASS does NOT work:</p>
<pre>
 <code class='language-sass'>
a
  text-decoration: none
 </code>
</pre>

<p>CSS does work:</p>
<pre>
 <code class='language-css'>
a {
  text-decoration: none
 }
 </code>
</pre>

I would like to be able to do it in the next way:

import 'highlight.js/styles/nnfx-light.css'
import hljs from 'highlight.js/lib/core'

import sass from 'highlight.js/lib/languages/sass'
hljs.registerLanguage('sass', sass)

hljs.highlightAll();
7
  • The given code is valid, what is the problem? Please read through the how do I ask a good question?. Please update your question with a minimal reproducible example demonstrating the problem. Commented Jul 4, 2023 at 10:10
  • @AdrianKokot , I added a snippet to the question. Commented Jul 4, 2023 at 12:08
  • If you want to use it as es modules you have to install it as npm package, not cdn. Do you have it that way? Commented Jul 4, 2023 at 12:21
  • Yes, I installed the NPM package, but for the snippet I did it this way. Commented Jul 4, 2023 at 13:17
  • Are you getting some error or something? Because I still don't understand what's not working and why you ask question Commented Jul 4, 2023 at 13:23

2 Answers 2

2

Looking at the Highlight JS Support language list, there is no SASS syntax available.


There is a git issue asking for it, but that only mentions the Stylus syntax which is partly compatible with SASS.


Example with the stylus language:

hljs.highlightAll();
<link rel="stylesheet" href="https://unpkg.com/@highlightjs/[email protected]/styles/default.min.css">
<script src="https://unpkg.com/@highlightjs/[email protected]/highlight.min.js"></script>
<script src="https://unpkg.com/@highlightjs/[email protected]/languages/stylus.min.js"></script>

<pre>
    <code class='language-styl'>
a
    text-decoration: none
  
nav
    ul
        margin: 0
        padding: 0
        list-style: none

    li
        display: inline-block

    a
        display: block
        padding: 6px 12px
        text-decoration: none
    </code>
</pre>

Sign up to request clarification or add additional context in comments.

Comments

0

I found a temporal solution: to register SCSS as SASS with highlight.js NPM package:

import scss from 'highlight.js/lib/languages/scss'
hljs.registerLanguage('sass', scss)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.