Skip to content

Commit

Permalink
feat: add events page
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanyawat-Arsaga committed Jul 5, 2024
1 parent 14b1017 commit 13a842e
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 82 deletions.
16 changes: 8 additions & 8 deletions apps/astro/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ const config = tseslint.config(
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
"@typescript-eslint/no-unsafe-enum-comparison": "off"
'@typescript-eslint/no-unsafe-enum-comparison': 'off'
}
},
...compat.config({ extends: ['plugin:@pandacss/recommended'] }),
{
rules: {
'@pandacss/no-unsafe-token-fn-usage': 'off',
'@pandacss/no-hardcoded-color': 'off'
}
},
// ...compat.config({ extends: ['plugin:@pandacss/recommended'] }),
// {
// rules: {
// '@pandacss/no-unsafe-token-fn-usage': 'off',
// '@pandacss/no-hardcoded-color': 'off'
// }
// },
...eslintPluginAstro.configs['flat/recommended'],
{
files: ['**/*.astro', '**/*.ts', '**/*.tsx', '**/*.js'],
Expand Down
5 changes: 4 additions & 1 deletion apps/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"react-markdown": "^9.0.1",
"remark-gfm": "^4.0.0",
"remark-textr": "^6.1.0",
"typescript": "^5.5.3"
"typescript": "^5.5.3",
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-gfm": "^3.0.0",
"micromark-extension-gfm": "^3.0.0"
},
"devDependencies": {
"@pandabox/prettier-plugin": "^0.1.3",
Expand Down
2 changes: 1 addition & 1 deletion apps/astro/panda.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineConfig({
accentColor: 'amber',
grayColor: 'neutral',
borderRadius: 'lg',
additionalColors: ['red', 'blue', 'orange', 'green', 'purple', 'gray']
additionalColors: ['red', 'blue', 'orange', 'green', 'purple', 'gray', 'sand']
})
],

Expand Down
1 change: 1 addition & 0 deletions apps/astro/src/components/layout/Navigation.astro
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const NAVIGATION_LINKS = [
{ label: t('common.home'), value: '/' },
{ label: t('common.about-me'), value: '/about' },
{ label: t('common.projects'), value: '/projects' },
{ label: t('common.event'), value: '/events' },
{ label: t('common.note'), value: '/notes' },
{ label: t('common.contact'), value: '/contact' }
];
Expand Down
128 changes: 78 additions & 50 deletions apps/astro/src/components/lib/Markdown.tsx
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>
);
};
1 change: 1 addition & 0 deletions apps/astro/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface ImportMetaEnv {
readonly PRIVATE_BACKEND_API_URL: string;
readonly PRIVATE_OUTLINE_SERVER: string;
readonly PRIVATE_OUTLINE_API_TOKEN: string;
readonly PRIVATE_OUTLINE_SETTINGS_DOCUMENT_ID: string;
// more env variables...
}

Expand Down
77 changes: 77 additions & 0 deletions apps/astro/src/pages/[locale]/events/index.astro
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>
2 changes: 1 addition & 1 deletion apps/astro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"verbatimModuleSyntax": false,
"disableSizeLimit": true
},
"include": ["src", "styled-system", "./panda.config.ts", "../../libs"]
"include": ["src", "styled-system", "./panda.config.ts"]
}
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const compat = new FlatCompat({
recommendedConfig: js.configs.recommended
});

module.exports = [
module.exports = tseslint.config(
{ plugins: { '@nx': nxEslintPlugin } },
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
Expand Down Expand Up @@ -36,4 +36,4 @@ module.exports = [
'@typescript-eslint/triple-slash-reference': 'off'
}
}
];
);
1 change: 1 addition & 0 deletions libs/i18n/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"blog": "Blog",
"hobbies": "Hobbies",
"contact": "Contact",
"event": "Events",
"experiences": "Experiences",
"education": "Education",
"note": "Notes",
Expand Down
1 change: 1 addition & 0 deletions libs/i18n/ja/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"education": "学歴",
"note": "日記",
"contact": "お問い合わせ",
"event": "イベント",
"tags": "タグ",
"under-construction": "準備中",
"page-not-found": "ページが見つからないようです",
Expand Down
1 change: 1 addition & 0 deletions libs/outline/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import removeMarkdown from 'remove-markdown';
export const cleanArticleContent = (content?: string) => {
return content?.replaceAll('\\\n', '')?.replaceAll('\\n', '\n\n');
};

export const getArticleBanner = (content?: string) => {
return (
/!\[(.*)\]\((.+?)(?:".*?")?\)/
Expand Down
14 changes: 14 additions & 0 deletions libs/utils/markdown.ts
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;
}
Loading

0 comments on commit 13a842e

Please sign in to comment.