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

Inconsistent highlightjs syntax highlighting #935

Closed
fastfoodcoding opened this issue Sep 5, 2017 · 2 comments
Closed

Inconsistent highlightjs syntax highlighting #935

fastfoodcoding opened this issue Sep 5, 2017 · 2 comments
Labels
help wanted L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue

Comments

@fastfoodcoding
Copy link

fastfoodcoding commented Sep 5, 2017

I am using marked with highlightjs for syntax highlighting. Both are latest versions. the following markdown is generating inconsistent highlighted code.

```java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

class FilesHandling {
	public static void main(String[] args) {
		try {
			List<String> lines = Files.readAllLines(Paths.get("/path/to/file.txt"));
			int lineNum = 1;
		} catch (IOException e) {
		}
	}
}
```

```java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

class FilesHandling {
	public static void main(String[] args) {
		try {
		} catch (IOException e) {
		}
	}
}
```

This is generating following html code

<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.nio.file.Files;
<span class="hljs-keyword">import</span> java.nio.file.Paths;
<span class="hljs-keyword">import</span> java.util.<span class="hljs-built_in">List</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FilesHandling</span> </span>{
    public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(<span class="hljs-built_in">String</span>[] args) {
        <span class="hljs-keyword">try</span> {
            <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">String</span>&gt; lines = Files.readAllLines(Paths.<span class="hljs-keyword">get</span>(<span class="hljs-string">"/path/to/file.txt"</span>));
            <span class="hljs-built_in">int</span> lineNum = <span class="hljs-number">1</span>;
        } <span class="hljs-keyword">catch</span> (IOException e) {
        }
    }
}
</code></pre>
<pre><code class="lang-java"><span class="hljs-meta"><span class="hljs-meta-keyword">import</span> java.io.IOException;</span>
<span class="hljs-meta"><span class="hljs-meta-keyword">import</span> java.nio.file.Files;</span>
<span class="hljs-meta"><span class="hljs-meta-keyword">import</span> java.nio.file.Paths;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FilesHandling</span> </span>{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(String[] args) {
        <span class="hljs-keyword">try</span> {
        } <span class="hljs-keyword">catch</span> (IOException e) {
        }
    }
}
</code></pre>

You can see it live here https://fastfoodcoding.com/tutorials/1504364531942/file-handling-in-java
See the difference between first code snippet and the rest.

Following is my configuration in code

marked = require('marked');
// Synchronous highlighting with highlight.js
marked.setOptions({
  breaks: true,
  highlight: function(code) {
    return require('highlight.js').highlightAuto(code).value;
  }
});
@fastfoodcoding fastfoodcoding changed the title inconsistent highlightjs syntax highlighting Inconsistent highlightjs syntax highlighting Sep 5, 2017
@joshbruce joshbruce added help wanted L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue labels Dec 1, 2017
@joshbruce
Copy link
Member

#983

@Feder1co5oave
Copy link
Contributor

It's because you're using highlightAuto, so highlight.js is guessing the language inside the code block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue
Projects
None yet
Development

No branches or pull requests

3 participants