summaryrefslogtreecommitdiff
path: root/packages/markdown/component/Markdown.astro
blob: ac0cf58618efe86342651ceb1dbc271b8d58ee99 (plain) (blame)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
export interface Props {
	content?: string;
}

const dedent = (str: string) => {
	const _str = str.split('\n').filter(s => s.trimStart().length > 0);
	if (_str.length === 0) {
		return str.trimStart();
	}
	const trimmedSpace = _str[0].replace(_str[0].trimStart(), '');
	return str
		.split('\n')
		.map((ln) => ln.startsWith(trimmedSpace) ? ln.replace(trimmedSpace, '') : ln)
		.join('\n');
}

// Internal props that should not be part of the external interface.
interface InternalProps extends Props {
	class: string;
	$scope: string;
}

let { content, class: className } = Astro.props as InternalProps;
let html = null;
let htmlContent = '';

// If no content prop provided, use the slot.
if (!content) {
	content = await Astro.slots.render('default');
	if (content !== undefined && content !== null) {
		content = dedent(content);
	}
}

if (content) {
	htmlContent = await (Astro as any).__renderMarkdown(content, {
		$: {
			scopedClassName: className,
		},
	});
}

html = htmlContent;
---

<>
{html ? <Fragment set:html={html} /> : <slot />}
</>