|
| 1 | +# Wire `monaco-textmate` with `monaco-editor` |
| 2 | + |
| 3 | +## Install |
| 4 | + |
| 5 | +```sh |
| 6 | +npm i monaco-editor-textmate |
| 7 | +``` |
| 8 | + |
| 9 | +Please install peer dependencies if you haven't already |
| 10 | +```sh |
| 11 | +npm i monaco-textmate monaco-editor onigasm |
| 12 | +``` |
| 13 | +## Usage |
| 14 | + |
| 15 | +```javascript |
| 16 | +import { loadWASM } from 'onigasm' // peer dependency of 'monaco-textmate' |
| 17 | +import { Registry } from 'monaco-textmate' // peer dependency |
| 18 | +import { wireTmGrammars } from 'monaco-editor-textmate' |
| 19 | + |
| 20 | +export async function liftOff() { |
| 21 | + await loadWASM(`path/to/onigasm.wasm`) // See https://www.npmjs.com/package/onigasm#light-it-up |
| 22 | + |
| 23 | + const registry = new Registry({ |
| 24 | + getGrammarDefinition: async (scopeName) => { |
| 25 | + return { |
| 26 | + format: 'json', |
| 27 | + content: await (await fetch(`static/grammars/css.tmGrammar.json`)).text() |
| 28 | + } |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + // map of monaco "language id's" to TextMate scopeNames |
| 33 | + const grammars = new Map() |
| 34 | + grammars.set('css', 'source.css') |
| 35 | + grammars.set('html', 'text.html.basic') |
| 36 | + grammars.set('typescript', 'source.ts') |
| 37 | + |
| 38 | + // monaco's built-in themes aren't powereful enough to handle TM tokens |
| 39 | + // https://github.com/Nishkalkashyap/monaco-vscode-textmate-theme-converter#monaco-vscode-textmate-theme-converter |
| 40 | + monaco.editor.defineTheme('vs-code-theme-converted', { |
| 41 | + // ... use `monaco-vscode-textmate-theme-converter` to convert vs code theme and pass the parsed object here |
| 42 | + }); |
| 43 | + |
| 44 | + var editor = monaco.editor.create(document.getElementById('container'), { |
| 45 | + value: [ |
| 46 | + 'html, body {', |
| 47 | + ' margin: 0;', |
| 48 | + '}' |
| 49 | + ].join('\n'), |
| 50 | + language: 'css', // this won't work out of the box, see below for more info, |
| 51 | + theme: 'vs-code-theme-converted' // very important, see comment above |
| 52 | + }) |
| 53 | + |
| 54 | + await wireTmGrammars(monaco, registry, grammars, editor) |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Limitation |
| 59 | + |
| 60 | +`monaco-editor` distribution comes with built-in tokenization support for few languages. Because of this `monaco-editor-textmate` [cannot |
| 61 | +be used with `monaco-editor`](https://github.com/Microsoft/monaco-editor/issues/884) without some modification, see explanation of this problem [here](https://github.com/Microsoft/monaco-editor/issues/884#issuecomment-389778611). |
| 62 | + |
| 63 | +### Solution |
| 64 | + |
| 65 | +To get `monaco-editor-textmate` working with `monaco-editor`, you're advised to use Webpack with [`monaco-editor-webpack-plugin`](https://www.npmjs.com/package/monaco-editor-webpack-plugin) which allows you to control which of "built-in" languages should `monaco-editor` use/bundle, leaving the rest. |
| 66 | +With that control you must *exclude* any/all languages for which you'd like to use TextMate grammars based tokenization instead. |
| 67 | + |
| 68 | +## License |
| 69 | +MIT |
0 commit comments