Skip to content

Commit

Permalink
feat: support markdown in chat and resource
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Dec 23, 2024
1 parent 1ffa745 commit 9119a01
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/rfc2047": "^2.0.3",
"autoprefixer": "^10.4.20",
"debug": "^4.3.7",
"dompurify": "^3.2.3",
"dotenv": "^16.4.5",
"eslint": "^9.15.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -45,6 +46,7 @@
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"lucide-svelte": "^0.454.0",
"marked": "^15.0.4",
"mdsvex": "^0.11.2",
"openai": "^4.73.0",
"prettier": "^3.3.3",
Expand Down
35 changes: 35 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/lib/components/Chatroom.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Button, Card, Textarea } from 'flowbite-svelte';
import { Mic, Send, Square } from 'lucide-svelte'; // Added Square icon import
import AudioPlayer from './AudioPlayer.svelte';
import { renderMarkdown } from '$lib/utils/renderMarkdown';
interface Conversation {
name: string;
Expand Down Expand Up @@ -96,7 +97,14 @@
{/if}
<Card class="w-fit max-w-[80%] {conv.self ? 'bg-primary-50' : ''}">
<div class="-my-2">
<p class="text-gray-600">{conv.content}</p>
<p class="prose prose-hina text-gray-600">
{#await renderMarkdown(conv.content)}
Loading ...
{:then content}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html content}
{/await}
</p>
{#if conv.audio}
<AudioPlayer src={conv.audio} />
{/if}
Expand Down
10 changes: 9 additions & 1 deletion src/lib/components/session/HostView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import Chatroom from '$lib/components/Chatroom.svelte';
import { X } from 'lucide-svelte';
import { SvelteMap } from 'svelte/reactivity';
import { renderMarkdown } from '$lib/utils/renderMarkdown';
let { session }: { session: Readable<Session> } = $props();
let code = $state('Code generate error');
Expand Down Expand Up @@ -416,7 +417,14 @@
{#each $session?.resources as resource}
<div class="rounded-lg border p-4">
<h3 class="font-medium">{resource.name}</h3>
<p class="mt-2 text-gray-700">{resource.content}</p>
<p class="prose prose-hina mt-2 text-gray-700">
{#await renderMarkdown(resource.content)}
Loading ...
{:then content}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html content}
{/await}
</p>
</div>
{/each}
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/stores/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export async function signInWithGoogle() {
}

// Sign out function
export async function signOut() {
export async function signOut(f: typeof fetch = fetch) {
try {
await auth.signOut();
// Clear the session cookie
await fetch('/api/auth/signout', { method: 'POST' });
await f('/api/auth/signout', { method: 'POST' });
// Redirect to home page
await goto('/');
} catch (error) {
Expand Down
22 changes: 22 additions & 0 deletions src/lib/utils/renderMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import DOMPurify from 'dompurify';
import { marked } from 'marked';

/**
* Renders the given markdown string to HTML and sanitizes it.
* @param markdown - The markdown string to be rendered.
* @returns A promise that resolves to the sanitized HTML string.
*/
export async function renderMarkdown(markdown: string): Promise<string> {
const renderer = new marked.Renderer();

// prevent rendering of hidden links
renderer.link = (token) => {
return token.text ? `${token.text} (${token.href})` : token.href;
};

// prevent showing images
renderer.image = () => '';

const dirty = await marked(markdown, { renderer });
return DOMPurify.sanitize(dirty);
}
4 changes: 2 additions & 2 deletions src/routes/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { signOut } from '$lib/stores/auth';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals }) => {
export const load: PageServerLoad = async ({ locals, fetch }) => {
if (locals.user) {
throw redirect(303, '/dashboard');
} else {
signOut();
signOut(fetch);
}

return {};
Expand Down
10 changes: 8 additions & 2 deletions src/routes/template/[id]/ResourceList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { page } from '$app/stores';
import type { Template } from '$lib/schema/template';
import { notifications } from '$lib/stores/notifications';
import { renderMarkdown } from '$lib/utils/renderMarkdown';
// Constants
const LIMITS = {
Expand Down Expand Up @@ -215,10 +216,15 @@
</div>
<div class="relative">
<p
class="mt-1 overflow-y-auto whitespace-pre-wrap break-words text-sm text-gray-600 transition-[max-height] duration-300 ease-in-out"
class="prose prose-hina mt-1 overflow-y-auto break-words text-sm text-gray-600 transition-[max-height] duration-300 ease-in-out"
style="max-height: {expandedResources.has(resource.id) ? '500px' : '100px'}"
>
{resource.content}
{#await renderMarkdown(resource.content)}
{resource.content}
{:then content}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html content}
{/await}
</p>
{#if resource.content.length > 100}
<Button
Expand Down
24 changes: 24 additions & 0 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ export default {
800: '#CC4522',
900: '#A5371B'
}
},
typography: {
hina: {
css: {
maxWidth: '100%',
p: {
marginTop: '0.25em',
marginBottom: '0.25em'
},
'blockquote p:first-of-type::before': { content: 'none' },
'blockquote p:last-of-type::after': { content: 'none' },
h1: { marginTop: '0.75em', marginBottom: '0.125em', fontSize: '1.5em' },
h2: { marginTop: '0.5em', marginBottom: '0.125em', fontSize: '1.25em' },
h3: { marginTop: '0.5em', marginBottom: '0.125em', fontSize: '1.1em' },
h4: { marginTop: '0.375em', marginBottom: '0.125em', fontSize: '1em' },
li: { marginTop: '0.1em', marginBottom: '0.1em' },
'ul > li': {
paddingLeft: '0.75em'
},
'ol > li': {
paddingLeft: '0.75em'
}
}
}
}
}
},
Expand Down

0 comments on commit 9119a01

Please sign in to comment.