Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/readme #99

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@
"react-cropper": "^2.3.3",
"react-dom": "^18.2.0",
"react-icons": "^5.2.1",
"react-katex": "^3.0.1",
"react-live": "^4.1.5",
"react-pdf": "^7.7.3",
"react-router-dom": "6.22.3",
"react-shepherd": "^4.3.0",
"react-sizeme": "^3.0.2",
"recharts": "2.12.3",
"rehype-katex": "^7.0.0",
Copy link
Member

@Sparkenstein Sparkenstein Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ThijsZijdel have we included example tempaltes of how to use these libraries in the the module? Ideally it'd be great if user knows somehow that they can write math markup.

also why two different plugins? isn't rehype-katex same as remark-math?

"rehype-raw": "^7.0.0",
"remark-math": "^6.0.0",
"simple-base-converter": "^1.0.19",
"tauri-plugin-store-api": "https://github.com/tauri-apps/tauri-plugin-store",
"terser": "^5.30.0",
Expand All @@ -96,6 +100,7 @@
"@types/react": "^18.2.73",
"@types/react-color": "^3.0.12",
"@types/react-dom": "^18.2.23",
"@types/react-katex": "^3.0.4",
"@types/tinycolor2": "^1.4.6",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.4.0",
Expand All @@ -121,4 +126,4 @@
"vite": "^5.2.7",
"vite-tsconfig-paths": "^5.0.1"
}
}
}
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const RegexTester = loadable(() => import("./Features/regex/RegexTester"));
const TextDiff = loadable(() => import("./Features/text/TextDiff"));
const TextUtils = loadable(() => import("./Features/text/TextUtils"));
const Markdown = loadable(() => import("./Features/markdown/Markdown"));
const Readme = loadable(() => import("./Features/markdown/Readme"));
const YamlJson = loadable(() => import("./Features/json-yaml/Yaml"));
const Pastebin = loadable(() => import("./Features/pastebin/Pastebin"));
const Repl = loadable(() => import("./Features/repl/Repl"));
Expand Down Expand Up @@ -236,6 +237,7 @@ function App() {
<Route path="/diff" element={<TextDiff />}></Route>
<Route path="/text" element={<TextUtils />}></Route>
<Route path="/markdown" element={<Markdown />}></Route>
<Route path="/readme" element={<Readme />}></Route>
<Route path="/yamljson" element={<YamlJson />}></Route>
<Route path="/pastebin" element={<Pastebin />}></Route>
<Route path="/repl" element={<Repl />}></Route>
Expand Down
64 changes: 64 additions & 0 deletions src/Features/markdown/Editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// @ts-nocheck
import "./markdown.css";

import { Box, Group } from "@mantine/core";
import MarkdownPreview from "@uiw/react-markdown-preview";
import { Monaco } from "@/Components/MonacoWrapper";

import rehypeKatex from "rehype-katex";
import rehypeRaw from "rehype-raw";
import remarkMath from "remark-math";

import "katex/dist/katex.min.css";

const MarkdownEditor = ({
file,
previewFile,
setFile,
showPreview = true,
showEditor = true,
}: {
file: string;
previewFile?: string;
setFile: (file: string) => void;
showPreview?: boolean;
showEditor?: boolean;
}) => {
return (
<Group style={{ width: "100%", height: "95%" }} grow gap={10}>
{showEditor ? (
<Box style={{ width: showPreview ? "50%" : "100%", height: "100%" }}>
<Monaco
setValue={(e) => setFile(e || "")}
value={file}
language="markdown"
onEditorMounted={(editor) => {
// eslint-disable-next-line
editor.getContribution(
"editor.linkDetector"
).openerService._defaultExternalOpener.openExternal = () => {};
}}
/>
</Box>
) : null}
{showPreview ? (
<Box style={{ width: showEditor ? "50%" : "100%", height: "100%" }}>
<MarkdownPreview
source={previewFile ?? file}
style={{ padding: "15px", height: "100%", overflow: "scroll" }}
remarkPlugins={[remarkMath]}
rehypePlugins={[rehypeKatex, rehypeRaw]}
rehypeRewrite={(node) => {
if (node.tagName === "a") {
node.properties.target = "_blank";
node.properties.rel = "noopener noreferrer";
}
}}
/>
</Box>
) : null}
</Group>
);
};

export default MarkdownEditor;
80 changes: 17 additions & 63 deletions src/Features/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
// @ts-nocheck
import "./markdown.css";

import { Box, Button, Group, Stack } from "@mantine/core";
import MarkdownPreview from "@uiw/react-markdown-preview";
import { Button, Group, Stack } from "@mantine/core";
import { demoMdFile } from "@/Features/markdown/constants";
import { useState } from "react";

import { Monaco } from "@/Components/MonacoWrapper";
import { openFileAndGetData, saveDataToFile } from "@/utils/functions";
import { useFile } from "@/hooks";
import MarkdownEditor from "@/Features/markdown/Editor";

const Markdown = () => {
const [source, setSource] = useState(`
# Markdown
- Supports every markdown feature + GitHub Flavored Markdown
- Refer [GFM](https://github.github.com/gfm/)
- Supports footnotes
- ⛳️ Emoji support

\`\`\`js
const codeblock = () => {
// Code with syntax highlighting
}
\`\`\`
`);

const openFile = async () => {
const data = await openFileAndGetData(
"Open Markdown File",
[{ name: "Markdown", extensions: ["md"] }],
"text"
);
setSource(data);
};

const saveFile = async () => {
saveDataToFile(source, "Save Markdown File", [
{ name: "Markdown", extensions: ["md"] },
]);
};
const [preview, setPreview] = useState(true);
const {
file: source,
setFile: setSource,
openFile,
saveFile,
} = useFile({
initialFile: demoMdFile,
});

return (
<Stack
Expand All @@ -47,34 +24,11 @@ const codeblock = () => {
<Group>
<Button onClick={openFile}>Open md file</Button>
<Button onClick={saveFile}>Save md file</Button>
<Button onClick={() => setPreview((p) => !p)}>
{preview ? "Hide" : "Show"} preview
</Button>
</Group>
<Group style={{ width: "100%", height: "100%" }} grow gap={10}>
<Box style={{ width: "50%", height: "100%" }}>
<Monaco
setValue={(e) => setSource(e || "")}
value={source}
language="markdown"
onEditorMounted={(editor) => {
// eslint-disable-next-line
editor.getContribution(
"editor.linkDetector"
).openerService._defaultExternalOpener.openExternal = () => {};
}}
/>
</Box>
<Box style={{ width: "50%", height: "100%" }}>
<MarkdownPreview
source={source}
style={{ padding: "15px", height: "100%", overflow: "scroll" }}
rehypeRewrite={(node) => {
if (node.tagName === "a") {
node.properties.target = "_blank";
node.properties.rel = "noopener noreferrer";
}
}}
/>
</Box>
</Group>
<MarkdownEditor file={source} setFile={setSource} showPreview={preview} />
</Stack>
);
};
Expand Down
Loading