Skip to content

Commit

Permalink
feat: add react ssr example
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Jun 21, 2020
1 parent 147c343 commit 80fa166
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/react-ssr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
3 changes: 3 additions & 0 deletions examples/react-ssr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example-react-ssr

An example to use ByteMD in React SSR with Next.js
15 changes: 15 additions & 0 deletions examples/react-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "example-react-ssr",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "9.4.4",
"react": "16.13.1",
"react-dom": "16.13.1"
}
}
8 changes: 8 additions & 0 deletions examples/react-ssr/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'github-markdown-css';
import 'highlight.js/styles/vs.css';
import 'katex/dist/katex.css';
import 'bytemd/dist/index.css';

export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
85 changes: 85 additions & 0 deletions examples/react-ssr/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Head from 'next/head';
import { Editor } from 'bytemd/react';
import highlight from '@bytemd/plugin-highlight';
import math from '@bytemd/plugin-math';
// import mermaid from '@bytemd/plugin-mermaid';
import { useMemo, useState, useEffect, Fragment } from 'react';

export default function Home() {
const [value, setValue] = useState('');
const [enabled, setEnabled] = useState({
mermaid: true,
highlight: true,
math: true,
});

useEffect(() => {
const init = async () => {
const res = await fetch(
'https://raw.githubusercontent.com/bytedance/bytemd/master/assets/demo.md'
);
const text = await res.text();
setValue(text);
};

init();
}, []);

const plugins = useMemo(
() =>
[
// enabled.mermaid && mermaid(), // TODO: https://github.com/vercel/next.js/issues/706
enabled.highlight && highlight(),
enabled.math && math(),
].filter((x) => x),
[enabled]
);

return (
<div>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<div style={{ padding: '10px 0' }}>
Plugins:
{['math', 'highlight', 'mermaid'].map((p) => (
<Fragment key={p}>
{' '}
<label>
<input
type="checkbox"
checked={enabled[p]}
onChange={(e) => {
const { checked } = e.target;
setEnabled((v) => ({
...v,
[p]: checked,
}));
}}
/>
{p}
</label>
</Fragment>
))}
</div>
<Editor
value={value}
plugins={plugins}
onChange={(v) => {
setValue(v);
}}
/>

<style jsx global>{`
body {
margin: 0 10px;
}
.bytemd {
height: 90vh !important;
}
`}</style>
</div>
);
}
Binary file added examples/react-ssr/public/favicon.ico
Binary file not shown.

0 comments on commit 80fa166

Please sign in to comment.