www/pages/notes/[note].tsx

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-04-28 16:37:12 +00:00
import Layout from '../../components/layout';
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';
2022-04-28 16:37:12 +00:00
import readMarkdown from '../../lib/read-markdown';
import NotesInfo from '../../public/notes.json';
interface Note {
title: string,
mtime: string,
}
interface Notes {
[slug: string]: Note;
}
function Markdown({ content }: any) {
return <ReactMarkdown
remarkPlugins={[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 >
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;