Skip to content

Commit

Permalink
fix: reactive markdown rendering (#4888)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Sep 12, 2024
1 parent a669241 commit 3a478b4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions apps/desktop/src/lib/components/Markdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import { Lexer } from 'marked';
interface Props {
content: string;
content: string | undefined;
}
let { content }: Props = $props();
const lexer = new Lexer(options);
const tokens = lexer.lex(content);
const tokens = $derived.by(() => {
const lexer = new Lexer(options);
return lexer.lex(content ?? '');
});
</script>

<div class="markdown-content">
<MarkdownContent type="init" {tokens} />
{#if tokens}
<MarkdownContent type="init" {tokens} />
{/if}
</div>

<style>
Expand Down
5 changes: 2 additions & 3 deletions apps/desktop/src/lib/components/MarkdownContent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
| Tokens.List;
const { type, ...rest }: Props = $props();
const CurrentComponent = renderers[type] as Component<Props>;
</script>

{#if (!type || type === 'init') && 'tokens' in rest && rest.tokens}
{#each rest.tokens as token}
<svelte:self {...token} />
{/each}
{:else if renderers[type]}
{@const CurrentComponent = renderers[type] as Component<Props>}
{#if type === 'list'}
{@const listItems = (rest as Extract<Props, { type: 'list' }>).items}
<CurrentComponent {...rest}>
Expand All @@ -39,7 +38,7 @@
{/each}
</CurrentComponent>
{:else}
<CurrentComponent this={renderers[type]} {...rest}>
<CurrentComponent {...rest}>
{#if 'tokens' in rest && rest.tokens}
<svelte:self tokens={rest.tokens} />
{:else if 'raw' in rest}
Expand Down
3 changes: 2 additions & 1 deletion apps/desktop/src/lib/utils/markdownRenderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const renderers = {
list_item: ListItem,
heading: Heading,
paragraph: Paragraph,
init: null
init: null,
space: null
};

export const options = {
Expand Down

0 comments on commit 3a478b4

Please sign in to comment.