www/pages/notes/[note].tsx

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-04-28 16:37:12 +00:00
import ReactMarkdown from 'react-markdown';
2022-04-30 13:56:18 +00:00
import SyntaxHighlighter from 'react-syntax-highlighter';
2022-10-05 03:41:59 +00:00
import { monokaiSublime as hlTheme } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
2022-04-28 16:37:12 +00:00
import remarkGfm from 'remark-gfm';
2022-10-05 03:41:59 +00:00
import rehypeRaw from 'rehype-raw';
2024-10-03 02:47:34 +00:00
import remarkDirective from 'remark-directive';
import remarkGithubAdmonitionsToDirectives from 'remark-github-admonitions-to-directives';
2022-04-28 16:37:12 +00:00
2024-10-03 02:47:34 +00:00
import Layout from '../../components/layout';
import readMarkdown from '../../lib/read-markdown';
2024-10-10 05:58:33 +00:00
import { toLocaleString } from '../../lib/date';
import NotesInfo from '../../public/notes.json';
2024-10-10 05:58:33 +00:00
import style from '../../styles/note.module.css';
interface Note {
title: string,
mtime: string,
}
interface Notes {
[slug: string]: Note;
}
function Markdown({ content }: any) {
return <ReactMarkdown
2024-10-10 05:08:53 +00:00
remarkPlugins={[remarkGithubAdmonitionsToDirectives, remarkDirective, remarkGfm]}
rehypePlugins={[rehypeRaw]}
components={{
code({ node, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || '')
return match
? (
<SyntaxHighlighter
showLineNumbers={true}
language={match[1]}
//@ts-ignore
style={hlTheme}
PreTag='div'
codeTagProps={{ style: { display: 'block' } }}
customStyle={{ padding: '0', borderRadius: '1rem' }}
{...props}
>{String(children).replace(/\n$/, '')}</SyntaxHighlighter>
)
: <code className={className} {...props}>
{children}
</code>
}
}}
>{content}</ReactMarkdown>
}
2022-04-28 16:37:12 +00:00
function Note({ note }: any) {
return (<>
<Layout >
2024-10-10 05:58:33 +00:00
<span className={style['last-updated']}>
Last updated: {toLocaleString(note.mtime)}
</span>
2022-04-28 16:37:12 +00:00
<section className='block'>
<Markdown content={note.content} />
2022-04-28 16:37:12 +00:00
</section>
</Layout>
</>
);
}
export async function getStaticProps({ params }: any) {
const note: string = params.note;
const notesInfo: Notes = NotesInfo;
const noteInfo: Note = notesInfo[note];
2022-04-28 16:37:12 +00:00
return {
props: {
note: {
...noteInfo,
content: await readMarkdown('notes', note, true)
}
}
}
2022-04-28 16:37:12 +00:00
}
export async function getStaticPaths() {
return {
paths: Object.keys(NotesInfo).map((note: string) => {
2022-04-28 16:37:12 +00:00
return {
params: {
note
2022-04-28 16:37:12 +00:00
}
}
}),
fallback: false
};
}
export default Note;