www/pages/notes/[page].tsx

66 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-04-28 16:37:12 +00:00
import Layout from '../../components/layout';
import { getAllNotes, getNote } from '../../util/slug';
import ReactMarkdown from 'react-markdown';
2022-04-30 13:56:18 +00:00
import SyntaxHighlighter from 'react-syntax-highlighter';
import { monokaiSublime } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
2022-04-28 16:37:12 +00:00
import remarkGfm from 'remark-gfm';
function Note({ note }: any) {
return (<>
<Layout name={note.title} title={note.title} ancestors={[{ name: 'Notes', path: 'notes' }]}>
<section className='block'>
<ReactMarkdown
2022-04-30 13:56:18 +00:00
remarkPlugins={[remarkGfm]}
2022-04-28 16:37:12 +00:00
components={{
code({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || '')
2022-04-30 13:56:18 +00:00
return !inline && match
? (
<SyntaxHighlighter
showLineNumbers={true}
language={match[1]}
2022-09-28 02:04:44 +00:00
//@ts-ignore
2022-04-30 13:56:18 +00:00
style={monokaiSublime}
PreTag='div'
2022-05-15 13:56:45 +00:00
codeTagProps={{ style: { display: 'block' } }}
customStyle={{ padding: '0', borderRadius: '1rem' }}
2022-04-30 13:56:18 +00:00
{...props}
2022-09-28 02:04:44 +00:00
>{String(children).replace(/\n$/, '')}</SyntaxHighlighter>
2022-04-30 13:56:18 +00:00
)
: <code className={className} {...props}>
{children}
</code>
2022-04-28 16:37:12 +00:00
}
}}
>{note.content}</ReactMarkdown>
</section>
</Layout>
</>
);
}
export async function getStaticProps({ params }: any) {
const note = getNote(params.page);
return {
props: { note }
};
}
export async function getStaticPaths() {
const notes = getAllNotes();
return {
paths: notes.map((note: any) => {
return {
params: {
page: note.slug
}
}
}),
fallback: false
};
}
export default Note;