carta/packages/plugin-code
2023-11-19 19:37:48 +01:00
..
src add support for custom syntax highlighting 2023-09-10 14:32:12 +02:00
package.json build: sync plugins major version master 2023-11-11 16:48:37 +01:00
README.md docs: update all READMEs 2023-11-19 19:37:48 +01:00
tsconfig.json fix: remove typescript project references 2023-11-14 18:37:19 +01:00

Carta Code Plugin

This plugin adds support for code blocks syntax highlighting. Install it using:

npm i @cartamd/plugin-code

This is done using Speed-highlight JS, which supports dynamic imports. This way, languages definitions are only imported at the moment of need.

Setup

Styles

Import the default styles:

import '@cartamd/plugin-code/default.css';

Using the default highlighter

Carta comes with a default highlighter that matches the one used to highlight markdown in the editor and is used by default. The theme is the same as the one used in the main carta package (carta-md/light.css or carta-md/dark.css). Here you can find other themes.

Using a custom highlighter

You can also provide a custom highlighter, that can be either sync or async.

code({
	customHighlight: {
		highlighter: (code, lang) => myCustomHighlighter(code, lang),
		langPrefix: 'my-highlighter-'
	}
});

Extension

<script lang="ts">
	import { Carta, CartaEditor } from 'carta-md';
	import { code } from '@cartamd/plugin-code';

	const carta = new Carta({
		extensions: [code()]
	});
</script>

<CartaEditor {carta} />

Documentation

Checkout the docs for examples, options and more.