'Monaco-editor automatic language detection doesn't work

I followed this answer on how to let monaco automatically detect the language of your file:

const model = monaco.editor.createModel(fileContent, undefined, filePath/* full path to the file */);
editor.setModel(model);

The content of the file does show up in the editor, but there's no syntax highlighting, just white text. I also tried replacing filePath with fileName (base name of the file), however, it still doesn't work. Is there a fix to this?



Solution 1:[1]

The monaco.editor.createModel function third parameter must be a Uri, not a simple string. You can create a Uri from a filename, as specified in the answer you linked :

const model = monaco.editor.createModel(fileContent, undefined, monaco.Uri.file(fileName));
editor.setModel(model);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Astor Bizard