Skip to content

Commit 3a478b4

Browse files
authored
fix: reactive markdown rendering (#4888)
1 parent a669241 commit 3a478b4

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

apps/desktop/src/lib/components/Markdown.svelte

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
import { Lexer } from 'marked';
55
66
interface Props {
7-
content: string;
7+
content: string | undefined;
88
}
99
1010
let { content }: Props = $props();
1111
12-
const lexer = new Lexer(options);
13-
const tokens = lexer.lex(content);
12+
const tokens = $derived.by(() => {
13+
const lexer = new Lexer(options);
14+
return lexer.lex(content ?? '');
15+
});
1416
</script>
1517

1618
<div class="markdown-content">
17-
<MarkdownContent type="init" {tokens} />
19+
{#if tokens}
20+
<MarkdownContent type="init" {tokens} />
21+
{/if}
1822
</div>
1923

2024
<style>

apps/desktop/src/lib/components/MarkdownContent.svelte

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
| Tokens.List;
2020
2121
const { type, ...rest }: Props = $props();
22-
23-
const CurrentComponent = renderers[type] as Component<Props>;
2422
</script>
2523

2624
{#if (!type || type === 'init') && 'tokens' in rest && rest.tokens}
2725
{#each rest.tokens as token}
2826
<svelte:self {...token} />
2927
{/each}
3028
{:else if renderers[type]}
29+
{@const CurrentComponent = renderers[type] as Component<Props>}
3130
{#if type === 'list'}
3231
{@const listItems = (rest as Extract<Props, { type: 'list' }>).items}
3332
<CurrentComponent {...rest}>
@@ -39,7 +38,7 @@
3938
{/each}
4039
</CurrentComponent>
4140
{:else}
42-
<CurrentComponent this={renderers[type]} {...rest}>
41+
<CurrentComponent {...rest}>
4342
{#if 'tokens' in rest && rest.tokens}
4443
<svelte:self tokens={rest.tokens} />
4544
{:else if 'raw' in rest}

apps/desktop/src/lib/utils/markdownRenderers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const renderers = {
2222
list_item: ListItem,
2323
heading: Heading,
2424
paragraph: Paragraph,
25-
init: null
25+
init: null,
26+
space: null
2627
};
2728

2829
export const options = {

0 commit comments

Comments
 (0)