| 1 | --- |
| 2 | relates: |
| 3 | - features/monaco-write |
| 4 | - features/monaco-editor |
| 5 | since: v0.47.0 |
| 6 | tags: [codeblock, syntax] |
| 7 | description: | |
| 8 | Import code snippets from existing files into your slides. |
| 9 | --- |
| 10 | |
| 11 | # Import Code Snippets |
| 12 | |
| 13 | You can import code snippets from existing files via the following syntax: |
| 14 | |
| 15 | ```md |
| 16 | <<< @/snippets/snippet.js |
| 17 | ``` |
| 18 | |
| 19 | ::: tip |
| 20 | The value of `@` corresponds to your package's root directory. It's recommended to put snippets in `@/snippets` in order to be compatible with the Monaco editor. Alternatively, you can also import from relative paths. |
| 21 | ::: |
| 22 | |
| 23 | You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/codebasics#_folding) to only include the corresponding part of the code file: |
| 24 | |
| 25 | ```md |
| 26 | <<< @/snippets/snippet.js#region-name |
| 27 | ``` |
| 28 | |
| 29 | To explicitly specify the language of the imported code, you can add a language identifier after: |
| 30 | |
| 31 | ```md |
| 32 | <<< @/snippets/snippet.js ts |
| 33 | ``` |
| 34 | |
| 35 | Any code block features like [line highlighting](#line-highlighting) and [Monaco editor](#monaco-editor) are also supported: |
| 36 | |
| 37 | ```md |
| 38 | <<< @/snippets/snippet.js {2,3|5}{lines:true} |
| 39 | <<< @/snippets/snippet.js ts {monaco}{height:200px} |
| 40 | ``` |
| 41 | |
| 42 | Note that you can use `{*}` as a placeholder of <LinkInline link="features/line-highlighting" />: |
| 43 | |
| 44 | <!-- eslint-skip --> |
| 45 | |
| 46 | ```md |
| 47 | <<< @/snippets/snippet.js {*}{lines:true} |
| 48 | ``` |
| 49 |