更新
根据要求,要求是禁用选择框并自动突出显示语法。
自 quill 2.0.0 以来,在强制选择语言的情况下,语法高亮的工作方式发生了巨大变化。
为了达到目的,我们需要重写 quill Syntax 类。
class CodeSyntax extends Syntax {
// override initListener to avoid creating selection box
initListener() {}
// overrider highlightBlot to highlight the text automatically
highlightBlot(text, language = 'plain') {
const container = this.quill.root.ownerDocument.createElement('div');
container.classList.add(CodeBlock.className);
container.innerHTML = this.options.hljs.highlightAuto(text).value;
...
}
}
mounted() {
const editorOpts = {
modules: {
syntax: {
hljs // inject highlight.js to quill
},
toolbar: {
container: [
[{ header: [1, 2, 3, false] }],
["bold", "italic", "underline", "code-block"],
],
},
},
theme: "snow",
};
editorOpts["scrollingContainer"] = this.$refs.scrollingContainer;
// override the default syntax module with our own version of it
Quill.register({ "modules/syntax": CodeSyntax }, true);
this.editorInstance = new Quill(this.$refs.editorNode, editorOpts);
},
Codesandbox 似乎无法正确处理覆盖包类。我上传了the code in Github。
附:为了简单起见,我只是使用 css 而不是 scss。
我认为您只是缺少 highlightjs css。
https://codesandbox.io/s/importing-sass-in-vue-forked-fympz?file=/src/components/HelloWorld.vue:1717-1758
@import "highlight.js/styles/github.css";
导入并再次测试,语法应该被高亮显示。