11import type { DefaultTag , MappedComponent } from '$lib' ;
2- import type { SvelteComponent } from 'svelte' ;
2+ import { mount , type Component } from 'svelte' ;
3+ import { render } from 'svelte/server' ;
34import type * as hast from 'hast' ;
45import { BROWSER } from 'esm-env' ;
56import { 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>>(
3436export 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
9290export 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 } ) ;
0 commit comments