admin管理员组文章数量:1405516
I am migrating my codebase from Svelte 4 to Svelte 5.
I had the following code:
import MarkdownLinkRenderer from '$lib/MarkdownLinkRenderer.svelte';
export function createMarkdownLinkRenderer(styles: string) {
return class extends MarkdownLinkRenderer {
constructor(options: { target: Element; props: { href: string; text: string } }) {
super(options);
this.$set({ className: styles });
}
};
}
Which was used as renderer with the svelte-markdown
package (replaced by @humanspeak/svelte-markdown
for compatibility with Svelte 5):
<script lang="ts">
const renderers = {
link: createMarkdownLinkRenderer('text-blue-56 hover:underline')
};
</script>
<Markdown source={notification.message} options={{ gfm: true, breaks: true }} {renderers} />
How can I convert my createMarkdownLinkRenderer
to work with Svelte 5, since components are no longer classes?
I am migrating my codebase from Svelte 4 to Svelte 5.
I had the following code:
import MarkdownLinkRenderer from '$lib/MarkdownLinkRenderer.svelte';
export function createMarkdownLinkRenderer(styles: string) {
return class extends MarkdownLinkRenderer {
constructor(options: { target: Element; props: { href: string; text: string } }) {
super(options);
this.$set({ className: styles });
}
};
}
Which was used as renderer with the svelte-markdown
package (replaced by @humanspeak/svelte-markdown
for compatibility with Svelte 5):
<script lang="ts">
const renderers = {
link: createMarkdownLinkRenderer('text-blue-56 hover:underline')
};
</script>
<Markdown source={notification.message} options={{ gfm: true, breaks: true }} {renderers} />
How can I convert my createMarkdownLinkRenderer
to work with Svelte 5, since components are no longer classes?
1 Answer
Reset to default 1I am not sure there is a really clean solution to this. One way of doing it would be to rely on the internal function structure of components. Since this is an implementation detail, there are no guarantees that the structure will stay this way.
Component functions currently take two arguments, the first is the node to render the component to, the second is a collection of props. You could directly join your styling property into those:
export function createMarkdownLinkRenderer(styles: string) {
return (anchor, props, ...rest) => {
const joinedProps = { ...props, className: styles };
return MarkdownLinkRenderer(anchor, joinedProps, ...rest);
};
}
Playground example
本文标签: Convert component inheritance syntax to Svelte 5 function componentStack Overflow
版权声明:本文标题:Convert component inheritance syntax to Svelte 5 function component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744917508a2632082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论