markedjs / marked

A markdown parser and compiler. Built for speed.
https://marked.js.org
Other
33.21k stars 3.39k forks source link

Inconsistent highlightjs syntax highlighting #935

Closed fastfoodcoding closed 6 years ago

fastfoodcoding commented 7 years ago

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) {
        }
    }
}
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
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) {
        }
    }
}
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) {
        }
    }
}

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

```javascript
marked = require('marked');
// Synchronous highlighting with highlight.js
marked.setOptions({
  breaks: true,
  highlight: function(code) {
    return require('highlight.js').highlightAuto(code).value;
  }
});
joshbruce commented 6 years ago

983

Feder1co5oave commented 6 years ago

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