🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@cartamd/plugin-component

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cartamd/plugin-component

This plugin adds support for mapping certain elements to **components** inside the editor.

1.1.0
latest
Version published
Maintainers
0
Created

Carta Component Plugin

This plugin adds support for mapping certain elements to components inside the editor.

Installation

npm i @cartamd/plugin-component

Usage

Mapping components

The first thing to do is to map certain elements(nodes) to their corresponding components. For example, if you want to use a custom renderer for images:

<!-- Image.svelte -->
<script>
	export let src;
	export let alt;
</script>

<!-- Your custom component... -->
<img {src} {alt} />
import { Carta } from 'carta-md';
import { component } from '@cartamd/plugin-component';
import { svelte, initializeComponents } from '@cartamd/plugin-component/svelte';
import Image from './Image.svelte';

const mapped = [svelte('img', Image) /* other components ... */];

const carta = new Carta({
	extensions: [component(mapped, initializeComponents)]
	// ...
});

You can use custom logic when selecting which nodes to map:

import { svelteCustom } from '@cartamd/plugin-component/svelte';
import MyComponent from '...';

const mapped = [
	svelteCustom(
		'my-component-id',
		(node) => {
			// Do something with the node to determine whether to replace it
		},
		MyComponent
	) /* other components ... */
];

Using <slot />

To render children, you need to use a custom <Slot /> component:

<!-- Heading.svelte -->
<script>
	import { Slot } from '@cartamd/plugin-component/svelte';
</script>

<h1>
	<Slot />
</h1>

Usage with pre-rendering

When pre-rendering content on the server (using the <PreRendered> component), the components will not be mounted by default. You need to use the initializeComponents function yourself.

<script>
	import { initializeComponents } from '@cartamd/plugin-component/svelte';
	import { onMount } from 'svelte';
	// ...

	export let data;

	let container;

	// Needs access to the mapped components
	const mapped = [
		/* ... */
	];

	onMount(() => {
		initializeComponents(mapped, container);
	});
</script>

<div bind:this={container}>
	<PreRendered html={data.html} />
</div>

Limitations

  • During server-side rendering, embedded styles for the mapped components are not added to the bundle. Use a dedicated stylesheet instead.
  • <svelte:head> is not available during server-side rendering.

FAQs

Package last updated on 03 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts