www/pages/notes/[note].tsx

72 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-04-28 16:37:12 +00:00
import Layout from '../../components/layout';
2022-10-05 03:41:59 +00:00
import { getAllNotes, getNote } from '../../lib/slug';
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';
2022-04-28 16:37:12 +00:00
function Markdown({content}: any) {
return <ReactMarkdown
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw]}
components={{
code({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || '')
return !inline && 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 name={note.title} title={note.title} ancestors={[{ name: 'Notes', path: 'notes' }]}>
<section className='block'>
<Markdown content={note.content} />
2022-04-28 16:37:12 +00:00
</section>
</Layout>
</>
);
}
export async function getStaticProps({ params }: any) {
2022-10-05 03:41:59 +00:00
const note = getNote(params.note);
2022-04-28 16:37:12 +00:00
return {
props: { note }
};
}
export async function getStaticPaths() {
const notes = getAllNotes();
return {
paths: notes.map((note: any) => {
return {
params: {
2022-10-05 03:41:59 +00:00
note: note.slug
2022-04-28 16:37:12 +00:00
}
}
}),
fallback: false
};
}
export default Note;