syntax.mdx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ---
  2. title: Syntax Highlighter Module
  3. ---
  4. The Syntax Module enhances the Code Block format by automatically detecting and applying syntax highlighting. The excellent [highlight.js](https://highlightjs.org/) library is used as a dependency to parse and tokenize code blocks.
  5. In general, you may [configure](https://highlightjs.readthedocs.io/en/latest/api.html#configure-options) highlight.js as needed. However, Quill expects and requires the `useBR` option to be `false` if you are using highlight.js < v11.
  6. Quill supports highlight.js v9.12.0 and later.
  7. ### Usage
  8. <Sandpack
  9. defaultShowPreview
  10. files={{
  11. "/index.html": `
  12. <!-- Include your favorite highlight.js stylesheet -->
  13. <link href="{{site.highlightjs}}/styles/atom-one-dark.min.css" rel="stylesheet">
  14. <!-- Include the highlight.js library -->
  15. <script src="{{site.highlightjs}}/highlight.min.js"></script>
  16. <link href="{{site.cdn}}/quill.snow.css" rel="stylesheet" />
  17. <script src="{{site.cdn}}/quill.js"></script>
  18. <div id="editor"></div>
  19. <script>
  20. const quill = new Quill('#editor', {
  21. modules: {
  22. syntax: true, // Include syntax module
  23. toolbar: [['code-block']] // Include button in toolbar
  24. },
  25. theme: 'snow'
  26. });
  27. const Delta = Quill.import('delta');
  28. quill.setContents(
  29. new Delta()
  30. .insert('const language = "JavaScript";')
  31. .insert('\\n', { 'code-block': 'javascript' })
  32. .insert('console.log("I love " + language + "!");')
  33. .insert('\\n', { 'code-block': 'javascript' })
  34. );
  35. </script>
  36. `}}
  37. />
  38. If you install highlight.js as an npm package and don't want to expose it to `window`, you need to pass it to syntax module as an option:
  39. ```js
  40. import Quill from 'quill';
  41. import hljs from 'highlight.js';
  42. const quill = new Quill('#editor', {
  43. modules: {
  44. syntax: { hljs },
  45. },
  46. });
  47. ```