Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recommanded way to purify html #58

Open
Ennoriel opened this issue Oct 26, 2022 · 3 comments
Open

recommanded way to purify html #58

Ennoriel opened this issue Oct 26, 2022 · 3 comments

Comments

@Ennoriel
Copy link

Hi! The library looks amazing!

I am using this library for rendering markdown written by users. In this use case, I would like to purify the HTML generated. What do you consider the best solution?

From what I read, svelte-markdown uses marked internally who recommands DOMPurify. Is there a way to use it as a middleware?

Should this information be written in the readme?

Thanks :)

@Ennoriel
Copy link
Author

I found a way: override the Html component to not use @html.

@MihinMUD
Copy link

MihinMUD commented Aug 10, 2023

@Ennoriel Hey, Can you tell me exactly how you did it? I am also looking for a solution to prevent users from using html tags.

@Ennoriel
Copy link
Author

Hi @MihinMUD

You can override the renderer components:

<script lang="ts">
	import Code from '$lib/components/atom/MarkdownCode.svelte';
	import Text from '$lib/components/atom/MarkdownText.svelte';
	import SvelteMarkdown from 'svelte-markdown';

	export let source: string;
</script>

<SvelteMarkdown {source} renderers={{ html: Text, code: Code }} />
<!-- MarkdownText.svelte -->
<script lang="ts">
	export let text: string;
</script>

{text}
<!-- MarkdownCode.svelte -->
<script lang="ts">
	import Prism from 'prismjs';
	import 'prism-svelte';

	export let lang: string;
	export let text: string;

	$: highlighted = Prism.highlight(text, Prism.languages.svelte, 'svelte');
</script>

<pre class="language-{lang}"><code>{@html highlighted}</code></pre>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants