Skip to content

Commit

Permalink
refactor(remark): attach decklists to tree directly
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybacon committed May 19, 2024
1 parent c426be4 commit b0b8653
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 86 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"remark-parse": "11.0.0",
"remark-stringify": "11.0.0",
"remark-toc": "9.0.0",
"simple-icons": "11.14.0",
"simple-icons": "11.15.0",
"unified": "11.0.4",
"unist-util-remove": "4.0.0",
"unist-util-select": "5.1.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
Youtube,
} from '@/components/Markdown/renderers';
import { SpoilsCalculator } from '@/components/SpoilsCalculator/SpoilsCalculator';
import { type Decklists } from '@/tools/decklists/types';
import {
type Article,
type Chapter,
Expand Down Expand Up @@ -77,15 +76,13 @@ const COMPONENTS_EXTRA = {
} as const;

type Props = {
decklists: Decklists;
markdown: Article | Chapter | Partial;
/** Whether the component should scroll to the current anchor. */
withScroll?: boolean;
withWrapper?: boolean;
};

export const Markdown: FunctionComponent<Props> = ({
decklists,
markdown,
withScroll = true,
withWrapper = true,
Expand All @@ -100,7 +97,7 @@ export const Markdown: FunctionComponent<Props> = ({
// NOTE Our own remarkers
remarkBase,
remarkCard,
[remarkDecklist, { decklists }],
[remarkDecklist, { decklists: markdown.decklists }],
[remarkRow, { scries: markdown.scries }],
],
} as const satisfies Record<string, PluggableList>;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Props = {
menu: MenuEntry[];
};

const NotFoundPage: NextPage<Props> = ({ menu }) => (
const Page: NextPage<Props> = ({ menu }) => (
<Layout background="/404.jpg" menu={menu} title="404">
<Box fontSize="10em" fontWeight="fontWeightLight" textAlign="center">
404
Expand All @@ -23,4 +23,4 @@ export const getStaticProps: GetStaticProps<Props> = () => ({
},
});

export default NotFoundPage;
export default Page;
10 changes: 3 additions & 7 deletions src/pages/[category]/[chapter].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
import { Banner } from '@/components/Banner/Banner';
import { Layout } from '@/components/Layout/Layout';
import { Markdown } from '@/components/Markdown/Markdown';
import { getDecklists } from '@/tools/decklists/getDecklists';
import { type Decklists } from '@/tools/decklists/types';
import { getChapterCards } from '@/tools/markdown/getChapterCards';
import { getChapter } from '@/tools/markdown/getMarkdown';
import { getMenu } from '@/tools/markdown/getMenu';
Expand All @@ -23,16 +21,15 @@ import {

type Props = {
chapter: Chapter;
decklists: Decklists;
menu: MenuEntry[];
};

const ChapterPage: NextPage<Props> = ({ chapter, decklists, menu }) => (
const Page: NextPage<Props> = ({ chapter, menu }) => (
<Layout menu={menu} title={chapter.matter.title} withBackToTop withProgress>
<Card>
<Banner banner={chapter.banner} title={chapter.matter.title} />
<CardContent>
<Markdown decklists={decklists} markdown={chapter} />
<Markdown markdown={chapter} />
</CardContent>
</Card>
</Layout>
Expand All @@ -59,10 +56,9 @@ export const getStaticProps: GetStaticProps<Props, Query> = async ({
return {
props: {
chapter: await getChapter(category, chapter),
decklists: getDecklists(),
menu: getMenu(),
},
};
};

export default ChapterPage;
export default Page;
4 changes: 2 additions & 2 deletions src/pages/articles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Props = {
menu: MenuEntry[];
};

const ArticlesPage: NextPage<Props> = ({ articles, menu }) => (
const Page: NextPage<Props> = ({ articles, menu }) => (
<Layout menu={menu} title="Articles" withBackToTop>
<Card>
<List disablePadding>
Expand Down Expand Up @@ -49,4 +49,4 @@ export const getStaticProps: GetStaticProps<Props> = async () => ({
},
});

export default ArticlesPage;
export default Page;
12 changes: 4 additions & 8 deletions src/pages/articles/[year]/[month]/[day]/[article].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
import { Banner } from '@/components/Banner/Banner';
import { Layout } from '@/components/Layout/Layout';
import { Markdown } from '@/components/Markdown/Markdown';
import { getDecklists } from '@/tools/decklists/getDecklists';
import { type Decklists } from '@/tools/decklists/types';
import { getArticleCards } from '@/tools/markdown/getArticleCards';
import { getArticle, getMarkdown } from '@/tools/markdown/getMarkdown';
import { getMenu } from '@/tools/markdown/getMenu';
Expand All @@ -24,12 +22,11 @@ import {

type Props = {
article: Article;
decklists: Decklists;
footer: Partial;
menu: MenuEntry[];
};

const ArticlePage: NextPage<Props> = ({ article, decklists, footer, menu }) => (
const Page: NextPage<Props> = ({ article, footer, menu }) => (
<Layout menu={menu} title={article.matter.title} withBackToTop withProgress>
<Card>
<Banner
Expand All @@ -41,11 +38,11 @@ const ArticlePage: NextPage<Props> = ({ article, decklists, footer, menu }) => (
title={article.matter.title}
/>
<CardContent>
<Markdown decklists={decklists} markdown={article} />
<Markdown markdown={article} />
</CardContent>
<Divider />
<CardContent>
<Markdown decklists={decklists} markdown={footer} withScroll={false} />
<Markdown markdown={footer} withScroll={false} />
</CardContent>
</Card>
</Layout>
Expand Down Expand Up @@ -74,11 +71,10 @@ export const getStaticProps: GetStaticProps<Props, Query> = async ({
return {
props: {
article: await getArticle(year, month, day, article),
decklists: getDecklists(),
footer: await getMarkdown('partials', 'article-footer.md'),
menu: getMenu(),
},
};
};

export default ArticlePage;
export default Page;
10 changes: 3 additions & 7 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { useEffect, useRef, useState } from 'react';
import { ArticleCard } from '@/components/ArticleCard/ArticleCard';
import { Layout } from '@/components/Layout/Layout';
import { Markdown } from '@/components/Markdown/Markdown';
import { getDecklists } from '@/tools/decklists/getDecklists';
import { type Decklists } from '@/tools/decklists/types';
import { getArticleCards } from '@/tools/markdown/getArticleCards';
import { getMarkdown } from '@/tools/markdown/getMarkdown';
import { getMenu } from '@/tools/markdown/getMenu';
Expand All @@ -20,12 +18,11 @@ const ARTICLES_INITIAL_SIZE = 5;

type Props = {
articles: ArticleCardModel[];
decklists: Decklists;
menu: MenuEntry[];
welcome: Partial;
};

const HomePage: NextPage<Props> = ({ articles, decklists, menu, welcome }) => {
const Page: NextPage<Props> = ({ articles, menu, welcome }) => {
const articlesRoot = useRef<HTMLDivElement>(null);
const [size, setSize] = useState(ARTICLES_INITIAL_SIZE);

Expand All @@ -49,7 +46,7 @@ const HomePage: NextPage<Props> = ({ articles, decklists, menu, welcome }) => {
<Grid item lg={7}>
<Card>
<CardContent>
<Markdown decklists={decklists} markdown={welcome} />
<Markdown markdown={welcome} />
</CardContent>
</Card>
</Grid>
Expand Down Expand Up @@ -84,10 +81,9 @@ const HomePage: NextPage<Props> = ({ articles, decklists, menu, welcome }) => {
export const getStaticProps: GetStaticProps<Props> = async () => ({
props: {
articles: await getArticleCards(),
decklists: getDecklists(),
menu: getMenu(),
welcome: await getMarkdown('partials', 'welcome.md'),
},
});

export default HomePage;
export default Page;
10 changes: 3 additions & 7 deletions src/pages/license.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,33 @@ import { type GetStaticProps, type NextPage } from 'next';

import { Layout } from '@/components/Layout/Layout';
import { Markdown } from '@/components/Markdown/Markdown';
import { getDecklists } from '@/tools/decklists/getDecklists';
import { type Decklists } from '@/tools/decklists/types';
import { getMarkdown } from '@/tools/markdown/getMarkdown';
import { getMenu } from '@/tools/markdown/getMenu';
import { type MenuEntry, type Partial } from '@/tools/markdown/types';

type Props = {
decklists: Decklists;
license: Partial;
menu: MenuEntry[];
};

const LicensePage: NextPage<Props> = ({ decklists, license, menu }) => (
const Page: NextPage<Props> = ({ license, menu }) => (
<Layout menu={menu} title="License">
<Card>
<CardContent>
<Typography align="center" variant="h1">
License
</Typography>
<Markdown decklists={decklists} markdown={license} />
<Markdown markdown={license} />
</CardContent>
</Card>
</Layout>
);

export const getStaticProps: GetStaticProps<Props> = async () => ({
props: {
decklists: getDecklists(),
license: await getMarkdown('partials', 'license.md'),
menu: getMenu(),
},
});

export default LicensePage;
export default Page;
4 changes: 2 additions & 2 deletions src/pages/sandbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Props = {
menu: MenuEntry[];
};

const SandboxPage: NextPage<Props> = ({ menu }) => {
const Page: NextPage<Props> = ({ menu }) => {
const [input, setInput] = useState<string>(DEFAULT_INPUT);
const [output, setOutput] = useState<{ id: number; text: string }[]>([]);

Expand Down Expand Up @@ -126,4 +126,4 @@ export const getStaticProps: GetStaticProps<Props> = async () => ({
},
});

export default SandboxPage;
export default Page;
8 changes: 2 additions & 6 deletions src/pages/sandbox/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,27 @@ import { type GetStaticProps, type NextPage } from 'next';

import { Layout } from '@/components/Layout/Layout';
import { Markdown } from '@/components/Markdown/Markdown';
import { getDecklists } from '@/tools/decklists/getDecklists';
import { type Decklists } from '@/tools/decklists/types';
import { getMarkdown } from '@/tools/markdown/getMarkdown';
import { getMenu } from '@/tools/markdown/getMenu';
import { type MenuEntry, type Partial } from '@/tools/markdown/types';

type Props = {
decklists: Decklists;
markdown: Partial;
menu: MenuEntry[];
};

const Page: NextPage<Props> = ({ decklists, markdown, menu }) => (
const Page: NextPage<Props> = ({ markdown, menu }) => (
<Layout menu={menu} title="Sandbox">
<Card>
<CardContent>
<Markdown decklists={decklists} markdown={markdown} />
<Markdown markdown={markdown} />
</CardContent>
</Card>
</Layout>
);

export const getStaticProps: GetStaticProps<Props> = async () => ({
props: {
decklists: getDecklists(),
markdown: await getMarkdown('partials', 'sandbox.md'),
menu: getMenu(),
},
Expand Down
8 changes: 0 additions & 8 deletions src/tools/decklists/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { join } from 'node:path';

/** File extension to consider for decklist files. */
export const DECKLISTS_EXTENSION = '.txt';

/** Base URL for decklists. */
export const BASE_DECKLISTS_URL = join(process.cwd(), 'decklists');

/** Collection of regular expression used to parse a decklist file. */
export const DECK_RE = {
card: /(\d+) +(.+)\b */,
Expand Down
18 changes: 18 additions & 0 deletions src/tools/decklists/getDecklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { parse } from '@/tools/decklists/parse';
import { type Decklist, type DecklistExtra } from '@/tools/decklists/types';
import { formatDate } from '@/tools/io/formatDate';

/** Base file URL for decklists. */
const BASE_URL = join(process.cwd(), 'decklists');

/** Read decklist file found at the path crumbs. */
export const getDecklist = (...crumbs: string[]): Decklist & DecklistExtra => {
const path = join(BASE_URL, ...crumbs) + '.txt';
const decklist = parse(readFileSync(path, 'utf8'));
const [title, ...dateCrumbs] = crumbs.reverse();
const date: null | string = formatDate(...dateCrumbs.reverse());
return { ...decklist, date, titleFromPath: title as string };
};
27 changes: 0 additions & 27 deletions src/tools/decklists/getDecklists.ts

This file was deleted.

Loading

0 comments on commit b0b8653

Please sign in to comment.