Import Attachment Plugin Styles
masterThe attachment plugin requires CSS to render correctly. You can import the default theme provided by the plugin:
import '@cartamd/plugin-attachment/default.css';repository·master·Indexed 20 days ago
https://github.com/beartocode/cartaA lightweight, fast, and extensible Markdown editor and viewer built for Svelte, leveraging the unified/remark/rehype ecosystem. Includes a suite of plugins for extended functionality: @cartamd/plugin-anchor for permalinks, @cartamd/plugin-attachment for file uploads, @cartamd/plugin-code for Shiki-powered syntax highlighting, @cartamd/plugin-component for mapping HTML to Svelte components, @cartamd/plugin-emoji for emoji support, @cartamd/plugin-math for KaTeX expressions, @cartamd/plugin-slash for slash commands, and @cartamd/plugin-tikz for TikzJax illustrations.
The attachment plugin requires CSS to render correctly. You can import the default theme provided by the plugin:
import '@cartamd/plugin-attachment/default.css';To add id attributes and permalinks to headings in your Carta project, install the @cartamd/plugin-anchor package via npm.
npm i @cartamd/plugin-anchorTo ensure the syntax highlighting is rendered correctly, you must import the default CSS styles provided by the plugin.
import '@cartamd/plugin-code/default.css';When using the <PreRendered> component for server-side rendering, mapped components are not mounted automatically. You must manually call initializeComponents inside an onMount lifecycle hook, passing the array of mapped components and the container element.
<script>
import { initializeComponents } from '@cartamd/plugin-component/svelte';
import { onMount } from 'svelte';
export let data;
let container;
// This must match the mappings used in the editor
const mapped = [
/* ... your svelte() mappings ... */
];
onMount(() => {
initializeComponents(mapped, container);
});
</script>
<div bind:this={container}>
<PreRendered html={data.html} />
</div>The anchor plugin requires CSS to function correctly. You can import the default theme provided by the plugin:
import '@cartamd/plugin-anchor/default.css';Carta supports standard Markdown syntax for content authoring. Key features include:
_italic_, **bold**, and `monospace`.- item) and numbered lists (1. item).~~text~~.> at the start of lines for blockquoting.[text](url) and images .--- to create a horizontal rule.```js).**bold text**
- Bullet list item
1. Numbered list item
[Link text](http://example.com)
| Header | Header |
| ------ | ------ |
| Cell | Cell |Install the @cartamd/plugin-component package via npm to enable mapping specific HTML elements to Svelte components within the Carta editor.
npm i @cartamd/plugin-componentTo use custom renderers for specific nodes (like images), you must map the HTML tag name to a Svelte component. Use the svelte helper from @cartamd/plugin-component/svelte to create these mappings and pass them to the component extension in your Carta configuration.
For more complex selection logic, use svelteCustom which allows you to provide a predicate function to determine if a node should be replaced by the component.
import { Carta } from 'carta-md';
import { component } from '@cartamd/plugin-component';
import { svelte, initializeComponents, svelteCustom } from '@cartamd/plugin-component/svelte';
import Image from './Image.svelte';
import MyComponent from './MyComponent.svelte';
// Standard mapping: maps <img> tags to the Image component
const mapped = [svelte('img', Image)];
// Custom mapping: uses a predicate to decide when to use MyComponent
const customMapped = [
svelteCustom(
'my-component-id',
(node) => {
// Logic to determine if this node should be replaced
return node.id === 'special';
},
MyComponent
)
];
const carta = new Carta({
extensions: [component(mapped, initializeComponents)]
});To use the Slash plugin, import the slash function from @cartamd/plugin-slash and include it in the extensions array of your Carta instance configuration. You should also import the default CSS to ensure the slash command UI is styled correctly.
<script lang="ts">
import { Carta, MarkdownEditor } from 'carta-md';
import { slash } from '@cartamd/plugin-slash';
import '@cartamd/plugin-slash/default.css';
const carta = new Carta({
extextensions: [slash()]
});
</script>
<MarkdownEditor {carta} />To set up a basic Markdown editor, import Carta and MarkdownEditor from carta-md. You must also import the default CSS for styling.
Important Security Note: Carta does not handle sanitization automatically. You must provide a sanitizer in the Carta constructor options (e.g., using isomorphic-dompurify) to prevent XSS attacks.
Styling Requirement: You must set a monospace font for the .carta-font-code class in your CSS to ensure the editor functions correctly.
<script lang="ts">
import { Carta, MarkdownEditor } from 'carta-md';
// Component default theme
import 'carta-md/default.css';
const carta = new Carta({
// Remember to use a sanitizer to prevent XSS attacks
// sanitizer: mySanitizer
});
let value = $state('');
</script>
<MarkdownEditor bind:value {carta} />
<style>
/* Or in global stylesheet */
/* Set your monospace font */
/* Required to have the editor working correctly! */
:global(.carta-font-code) {
font-family: '...', monospace;
font-size: 1.1rem;
line-height: 1.1rem;
letter-spacing: normal;
}
</style>To add support for code block syntax highlighting to your Carta project, install the @cartamd/plugin-code package via npm.
npm i @cartamd/plugin-codeTo add KaTeX expression support to your Carta project, install the @cartamd/plugin-math package via npm.
npm i @cartamd/plugin-math