-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14b1017
commit 13a842e
Showing
14 changed files
with
217 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,91 @@ | ||
import { Code } from 'astro:components'; | ||
import { join } from 'path'; | ||
import ReactMarkdown from 'react-markdown'; | ||
import remarkGfm from 'remark-gfm'; | ||
import remarkTextr from 'remark-textr'; | ||
import { Divider, styled } from 'styled-system/jsx'; | ||
import { Divider, Stack, styled } from 'styled-system/jsx'; | ||
import { Heading } from '../ui/heading'; | ||
import { Link } from '../ui/link'; | ||
import * as Table from '../ui/table'; | ||
import { Text } from '../ui/text'; | ||
|
||
// https://github.com/remarkjs/react-markdown | ||
export const Markdown = ({ content, assetsPrefix }: { content: string; assetsPrefix?: string }) => { | ||
export const Markdown = ({ | ||
content, | ||
assetsPrefix, | ||
linksPrefix, | ||
disableLinks, | ||
disableInternalLinks | ||
}: { | ||
content: string; | ||
assetsPrefix?: string; | ||
linksPrefix?: string; | ||
disableLinks?: string; | ||
disableInternalLinks?: boolean; | ||
}) => { | ||
return ( | ||
<ReactMarkdown | ||
remarkPlugins={[remarkTextr, remarkGfm]} | ||
components={{ | ||
h1: ({ node: _, ...props }) => <Heading as="h1" size="3xl" fontWeight="bold" {...props} />, | ||
h2: ({ node: _, ...props }) => <Heading as="h2" size="3xl" {...props} />, | ||
h3: ({ node: _, ...props }) => <Heading as="h3" size="2xl" fontWeight="bold" {...props} />, | ||
h4: ({ node: _, ...props }) => <Heading as="h4" size="2xl" {...props} />, | ||
h5: ({ node: _, ...props }) => <Heading as="h5" size="xl" fontWeight="bold" {...props} />, | ||
h6: ({ node: _, ...props }) => <Heading as="h6" size="xl" {...props} />, | ||
p: ({ ref: _ref, node: _, ...props }) => <Text as="p" {...props} />, | ||
strong: ({ ref: _, node: __, ...props }) => <Text as="span" fontWeight="bold" {...props} />, | ||
a: ({ ref: _, node: __, ...props }) => <Link {...props} />, | ||
hr: ({ ref: _, node: __, ...props }) => <Divider {...props} />, | ||
blockquote: ({ ref: __, node: _, ...props }) => ( | ||
<styled.blockquote | ||
borderLeftWidth="4px" | ||
borderLeftColor="border.default" | ||
padding={4} | ||
borderLeftStyle="solid" | ||
{...props} | ||
/> | ||
), | ||
ul: ({ ref: _, node: __, ...props }) => ( | ||
<styled.ul pl="4" listStyleType="disc" {...props} /> | ||
), | ||
ol: ({ ref: _, node: __, ...props }) => ( | ||
<styled.ol pl="4" listStyleType="decimal" {...props} /> | ||
), | ||
li: ({ ref: _, node: __, ...props }) => <styled.li {...props} />, | ||
code: ({ ref: _, node: __, lang, ...props }) => ( | ||
<Code lang={lang as 'javascript'} code={props.children?.toString() ?? ''} /> | ||
), | ||
table: ({ ref: _, node: __, ...props }) => <Table.Root {...props} />, | ||
thead: ({ ref: _, node: __, ...props }) => <Table.Head {...props} />, | ||
th: ({ ref: _, node: __, ...props }) => <Table.Header {...props} />, | ||
tbody: ({ ref: _, node: __, ...props }) => <Table.Body {...props} />, | ||
tr: ({ ref: _, node: __, ...props }) => <Table.Row {...props} />, | ||
td: ({ ref: _, node: __, ...props }) => <Table.Cell {...props} />, | ||
img: ({ ref: _, node: __, ...props }) => { | ||
const rawUrl = props.src; | ||
const url = rawUrl?.[0] === '/' && assetsPrefix ? `${assetsPrefix}${rawUrl}` : rawUrl; | ||
return <img {...props} src={url} />; | ||
} | ||
}} | ||
> | ||
{/* {data} */} | ||
{content} | ||
</ReactMarkdown> | ||
<Stack gap={2}> | ||
<ReactMarkdown | ||
remarkPlugins={[remarkTextr, remarkGfm]} | ||
components={{ | ||
h1: ({ node: _, ...props }) => ( | ||
<Heading as="h1" size="3xl" fontWeight="bold" {...props} /> | ||
), | ||
h2: ({ node: _, ...props }) => <Heading as="h2" size="3xl" {...props} />, | ||
h3: ({ node: _, ...props }) => ( | ||
<Heading as="h3" size="2xl" fontWeight="bold" {...props} /> | ||
), | ||
h4: ({ node: _, ...props }) => <Heading as="h4" size="2xl" {...props} />, | ||
h5: ({ node: _, ...props }) => <Heading as="h5" size="xl" fontWeight="bold" {...props} />, | ||
h6: ({ node: _, ...props }) => <Heading as="h6" size="xl" {...props} />, | ||
p: ({ ref: _ref, node: _, ...props }) => <Text as="p" {...props} />, | ||
strong: ({ ref: _, node: __, ...props }) => ( | ||
<Text as="span" fontWeight="bold" {...props} /> | ||
), | ||
a: ({ ref: _, node: __, ...props }) => { | ||
const { href, ...propsWithoutHref } = props; | ||
const dest = linksPrefix && href?.startsWith('/') ? join(linksPrefix, href) : href; | ||
if (disableLinks || (disableInternalLinks && href?.startsWith('/'))) { | ||
return <Text as="p">{props.children}</Text>; | ||
} | ||
return <Link target="_blank" fontWeight="bold" {...props} href={dest} />; | ||
}, | ||
hr: ({ ref: _, node: __, ...props }) => <Divider {...props} />, | ||
blockquote: ({ ref: __, node: _, ...props }) => ( | ||
<styled.blockquote | ||
borderLeftWidth="4px" | ||
borderLeftColor="border.default" | ||
padding={4} | ||
borderLeftStyle="solid" | ||
{...props} | ||
/> | ||
), | ||
ul: ({ ref: _, node: __, ...props }) => ( | ||
<styled.ul pl="4" listStyleType="disc" {...props} /> | ||
), | ||
ol: ({ ref: _, node: __, ...props }) => ( | ||
<styled.ol pl="4" listStyleType="decimal" {...props} /> | ||
), | ||
li: ({ ref: _, node: __, ...props }) => <styled.li {...props} />, | ||
code: ({ ref: _, node: __, lang, ...props }) => ( | ||
<Code lang={lang as 'javascript'} code={props.children?.toString() ?? ''} /> | ||
), | ||
table: ({ ref: _, node: __, ...props }) => <Table.Root {...props} />, | ||
thead: ({ ref: _, node: __, ...props }) => <Table.Head {...props} />, | ||
th: ({ ref: _, node: __, ...props }) => <Table.Header {...props} />, | ||
tbody: ({ ref: _, node: __, ...props }) => <Table.Body {...props} />, | ||
tr: ({ ref: _, node: __, ...props }) => <Table.Row {...props} />, | ||
td: ({ ref: _, node: __, ...props }) => <Table.Cell {...props} />, | ||
img: ({ ref: _, node: __, ...props }) => { | ||
const rawUrl = props.src; | ||
const url = rawUrl?.[0] === '/' && assetsPrefix ? `${assetsPrefix}${rawUrl}` : rawUrl; | ||
return <img {...props} src={url} />; | ||
} | ||
}} | ||
> | ||
{/* {data} */} | ||
{content} | ||
</ReactMarkdown> | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
import { localePaths } from '~/i18n/paths'; | ||
import MainLayout from '~/layouts/MainLayout.astro'; | ||
import type { Link, Table, Text } from 'mdast'; | ||
import { cleanArticleContent } from 'outline/article'; | ||
import { Container, Stack } from 'styled-system/jsx'; | ||
import { parseMarkdown } from 'utils/markdown'; | ||
import { Markdown } from '~/components/lib/Markdown'; | ||
import { Heading } from '~/components/ui/heading'; | ||
import { useTranslations } from '~/i18n/utils'; | ||
import { outlineClient } from '~/utils/outline-api'; | ||
export function getStaticPaths() { | ||
return localePaths; | ||
} | ||
const { locale } = Astro.params; | ||
const t = useTranslations(locale); | ||
const events = await outlineClient.POST('/documents.info', { | ||
body: { id: import.meta.env.PRIVATE_OUTLINE_SETTINGS_DOCUMENT_ID } | ||
}); | ||
const settingsContent = cleanArticleContent(events.data?.data?.text); | ||
if (!settingsContent) { | ||
return Astro.redirect(404); | ||
} | ||
const tree = await parseMarkdown(settingsContent ?? ''); | ||
const settings = Object.fromEntries( | ||
tree.children | ||
?.find((c): c is Table => c.type === 'table') | ||
?.children.slice(1) | ||
.map((c) => [ | ||
(c.children[0].children[0] as Text)?.value, | ||
(c.children[1].children[0] as Link)?.url ?? (c.children[1].children[0] as Text)?.value | ||
]) as [string, string][] | ||
); | ||
if (!settings.events) { | ||
return Astro.redirect(404); | ||
} | ||
const eventData = await outlineClient.POST('/documents.info', { | ||
body: { id: settings.events.replace('/doc/', '') } | ||
}); | ||
const content = cleanArticleContent(eventData.data?.data?.text); | ||
if (!content) { | ||
return Astro.redirect(404); | ||
} | ||
--- | ||
|
||
<MainLayout> | ||
<Container w="full" p="4"> | ||
<Stack> | ||
<Heading as="h1" fontSize="2xl">{t('common.event')}</Heading> | ||
<Markdown | ||
content={content} | ||
linksPrefix={import.meta.env.PUBLIC_OUTLINE_URL} | ||
assetsPrefix={import.meta.env.PUBLIC_OUTLINE_URL} | ||
disableInternalLinks | ||
/> | ||
</Stack> | ||
</Container> | ||
</MainLayout> | ||
|
||
<style> | ||
@page { | ||
margin: 0; | ||
size: 91mm 55mm; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Root } from 'mdast'; | ||
|
||
export async function parseMarkdown(text: string) { | ||
const { fromMarkdown } = await import('mdast-util-from-markdown'); | ||
const { gfmFromMarkdown } = await import('mdast-util-gfm'); | ||
const { gfm } = await import('micromark-extension-gfm'); | ||
|
||
const tree: Root = fromMarkdown(text, { | ||
extensions: [gfm()], | ||
mdastExtensions: [gfmFromMarkdown()] | ||
}); | ||
|
||
return tree; | ||
} |
Oops, something went wrong.