0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/markdown/component/Markdown.astro
Ben Holmes 00fab4ce13
Feat: new legacy.astroFlavoredMarkdown flag (#4016)
* refactor: add legacy.jsxInMarkdown flag to config

* refactor: jsxInMarkdown -> astroFlavoredMarkdown

* refactor: remove `markdown.mode`

* feat: wire up legacy.astroFlavoredMarkdown

* test: add legacy to astro-markdown fixture

* test: remark autolinking

* test: remark components

* test: remark expressions

* test: remark strictness

* chore: remove "mode" from md component

* chore: remove "mode: md" from tests

* Fixing legacy MD tests, adding named slots tests for MDX pages

* chore: update lock file

* WIP: debugging named slots in MDX

* fix: handle named slots in MDX properly

* chore: re-enabling slots tests for MDX pages

* fixing test validation for svelte & vue

* removing unused Tailwind test

* legacy flag for Markdown component tests

* adding is:raw to Markdown component test

* adding is:raw to all Markdown component test fixtures

* can't use is:raw when nesting markdown components

* another nested test can't use is:raw

* one more <Markdown> test fix

* fixing another JSX markdown component test

* chore: add changeset

* e2e tests were missing the legacy flag

* removing the broken tailwind E2E markdown page

Co-authored-by: Tony Sullivan <tony.f.sullivan@outlook.com>
Co-authored-by: Nate Moore <nate@astro.build>
2022-07-22 22:45:16 +00:00

53 lines
1.4 KiB
Text

---
export interface Props {
content?: string;
}
// NOTE(fks): We are most likely moving this component out of Astro core
// in a few weeks. Checking the name like this is a bit of a hack, but we
// intentionally don't want to add an SSR flag for others to read from, just yet.
if (Astro.redirect.name !== '_onlyAvailableInSSR') {
console.error(`\x1B[31mThe <Markdown> component is not available in SSR. See https://github.com/withastro/rfcs/discussions/179 for more info.\x1B[39m`);
}
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 {
$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 />}