summaryrefslogtreecommitdiff
path: root/src/components/Markdown.astro
blob: e8996c55af739826dcff35fc27f72d7d366bfd46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
---
import { Marked } from 'marked';
import markedShiki from 'marked-shiki';
import { getHighlighter } from 'shiki';

interface Props {
    content: string;
    class?: string;
}

const { content, class: className } = Astro.props;

// `getHighlighter` is async, it initializes the internal and
// loads the languages and themes specified.
// Then later you can use `highlighter.codeToHtml` synchronously
// with the loaded themes and languages.
const highlighter = await getHighlighter({
    langs: ['jsx', 'js'],
    themes: ['min-dark']
});
const html = await new Marked()
    .use(
        markedShiki({
            highlight(code, lang, props) {
                return highlighter.codeToHtml(code, {
                    lang,
                    theme: 'min-dark'
                });
            }
        })
    )
    .parse(content);
---

<div class:list={['markdown', className]} set:html={html} />