Skip to content

Commit 455ea71

Browse files
committed
fix: plugin-component
1 parent 69f194f commit 455ea71

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

packages/plugin-component/src/lib/svelte.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { DefaultTag, MappedComponent } from '$lib';
2-
import type { SvelteComponent } from 'svelte';
2+
import { mount, type Component } from 'svelte';
3+
import { render } from 'svelte/server';
34
import type * as hast from 'hast';
45
import { BROWSER } from 'esm-env';
56
import { unified } from 'unified';
@@ -17,10 +18,11 @@ export { default as Slot } from './Slot.svelte';
1718
* @param component The Svelte component to replace the tag with
1819
* @returns The mapped component
1920
*/
20-
export const svelte = <T extends Record<string, unknown>>(
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
export const svelte = <T extends Record<string, any>>(
2123
tag: DefaultTag,
22-
component: typeof SvelteComponent<T>
23-
): MappedComponent<typeof SvelteComponent> => {
24+
component: Component<T>
25+
): MappedComponent<Component> => {
2426
return svelteCustom(tag, (node) => node.tagName === tag, component);
2527
};
2628

@@ -34,12 +36,12 @@ export const svelte = <T extends Record<string, unknown>>(
3436
export const svelteCustom = <T extends Record<string, unknown>>(
3537
id: string,
3638
match: (node: hast.Element) => boolean,
37-
component: typeof SvelteComponent<T>
38-
): MappedComponent<typeof SvelteComponent> => {
39+
component: Component<T>
40+
): MappedComponent<Component> => {
3941
return {
4042
id,
4143
match,
42-
component: component as typeof SvelteComponent,
44+
component: component as Component,
4345
render(node) {
4446
if (BROWSER) {
4547
const placeholder: hast.Element = {
@@ -53,18 +55,14 @@ export const svelteCustom = <T extends Record<string, unknown>>(
5355
};
5456
return [placeholder];
5557
} else {
56-
// @ts-expect-error The .render component is not picked up by Svelte intellisense
57-
// docs here: https://svelte.dev/docs/server-side-component-api
58-
const html: string = component.render(node.properties).html;
58+
const html: string = render(component, { props: node.properties as T }).body;
5959

6060
const root = unified().use(rehypeParse).parse(html);
61-
const htmlElem = root.children[0] as hast.Element;
62-
const bodyElem = htmlElem.children[1] as hast.Element;
6361

6462
// Find the `slot` component and add children to it
6563
let slot: { elem: hast.Element; index: number; parent: hast.Element } | undefined;
6664

67-
visit(bodyElem, (node, index, parent) => {
65+
visit(root, (node, index, parent) => {
6866
if (
6967
node.type === 'element' &&
7068
node.tagName === 'template' &&
@@ -83,14 +81,14 @@ export const svelteCustom = <T extends Record<string, unknown>>(
8381
slot.parent.children.splice(slot.index, 1, ...node.children);
8482
}
8583

86-
return bodyElem.children as hast.Element[];
84+
return root.children as hast.Element[];
8785
}
8886
}
8987
};
9088
};
9189

9290
export const initializeComponents = (
93-
components: MappedComponent<typeof SvelteComponent>[],
91+
components: MappedComponent<Component>[],
9492
renderer = document.body
9593
) => {
9694
// Remove previously mounted components
@@ -128,7 +126,7 @@ export const initializeComponents = (
128126
wrapper.style.display = 'contents';
129127
placeholder.replaceWith(wrapper);
130128

131-
new component.component({
129+
mount(component.component, {
132130
target: wrapper,
133131
props
134132
});

packages/plugin-component/src/routes/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { component } from '$lib';
3-
import { Carta, Markdown, MarkdownEditor } from 'carta-md';
3+
import { Carta, MarkdownEditor } from 'carta-md';
44
import { initializeComponents, svelte } from '$lib/svelte';
55
import Heading from './Heading.svelte';
66
import Img from './Img.svelte';
@@ -33,7 +33,7 @@
3333

3434
<main>
3535
<MarkdownEditor {carta} />
36-
<Markdown {carta} value="# Heading" />
36+
<!-- <Markdown {carta} value="# Heading" /> -->
3737
</main>
3838

3939
<style>

0 commit comments

Comments
 (0)