feat: server and fix inline code

This commit is contained in:
Artemy 2023-04-02 10:53:32 +03:00
parent 8bdc53f45d
commit bff9c3875c
9 changed files with 1391 additions and 59 deletions

2
.gitignore vendored
View file

@ -125,3 +125,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
notes/

61
index.js Normal file
View file

@ -0,0 +1,61 @@
const sha3 = require("js-sha3").sha3_512;
const express = require("express");
const bodyParser = require("body-parser");
const isValidNote = require("./note_validator");
const fs = require("fs");
const path = require("path");
const cryptojs = require("crypto-js");
require("dotenv").config();
const app = express();
app.use(bodyParser.json());
app.post("/publish", function (req, res) {
if (isValidNote(req.body)) {
let hash = sha3(JSON.stringify(req.body));
req.body.time = Date.now();
try {
fs.writeFileSync(
`./notes/${hash}.json`,
cryptojs.AES.encrypt(
JSON.stringify(req.body),
process.env.KEY
).toString()
);
res.send({ id: hash });
} catch {
res.status(500).send("Failed to write file");
}
} else {
res.status(403).send("Invalid body!");
}
});
app.get("/get-note/:delorno/:id", function (req, res) {
let path = `./notes/${req.params.id}.json`;
try {
let data = JSON.parse(
cryptojs.AES.decrypt(
fs.readFileSync(path, "utf-8"),
process.env.KEY
).toString(cryptojs.enc.Utf8)
);
res.send(data);
if (req.params.delorno === "del") fs.unlinkSync(path);
} catch {
res.status(404).send("There is no such note");
}
});
app.use(express.static("dist"));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "./dist", "index.html"));
});
app.listen(process.env.PORT, () => {
console.log(`Listening on port ${process.env.PORT}`);
});

15
note_validator.js Normal file
View file

@ -0,0 +1,15 @@
const Ajv = require("ajv");
const ajv = new Ajv();
const note_schema = {
type: "object",
properties: {
name: { type: "string", maxLength: 64 },
text: { type: "string", maxLength: 5000 },
},
required: ["name", "text"],
additionalProperties: false,
};
module.exports = (data) => {
return ajv.validate(note_schema, data);
};

1249
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,15 @@
{
"name": "anopaper",
"private": true,
"version": "0.0.0",
"type": "module",
"version": "1.0.0",
"author": "anopaper",
"license": "MIT",
"scripts": {
"dev": "vite",
"build": "vite build --emptyOutDir",
"preview": "vite preview"
"preview": "vite preview",
"start": "node index",
"bstart": "npm run build && npm run start"
},
"dependencies": {
"@heroicons/react": "^2.0.12",
@ -16,7 +19,13 @@
"react-router-dom": "^6.4.2",
"react-syntax-highlighter": "^15.5.0",
"rehype-mathjax": "^4.0.2",
"remark-math": "^5.1.1"
"remark-math": "^5.1.1",
"ajv": "^8.11.0",
"body-parser": "^1.20.1",
"crypto-js": "^4.1.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"js-sha3": "^0.8.0"
},
"devDependencies": {
"@types/react": "^18.0.17",

View file

@ -33,7 +33,7 @@ function CopyToClipboard(props) {
);
}
function CodeCopyBtn(props) {
function CodeCopyBtn({ text }) {
let [copied, setCopied] = useState(false);
useEffect(() => {
@ -48,7 +48,7 @@ function CodeCopyBtn(props) {
<div
className="code-copy-btn"
onClick={() => {
navigator.clipboard.writeText(props.children[0].props.children[0]);
navigator.clipboard.writeText(text);
setCopied(true);
}}
>

View file

@ -12,12 +12,17 @@ let theme = window.matchMedia("(prefers-color-scheme: dark)").matches
: "light";
function CodeBlock(props) {
let text = props.children[0];
let oneline = text.indexOf("\n") <= 1;
if (oneline) {
return <code className="md-code">{text}</code>;
} else
return (
<SyntaxHighlighter
className={`md-code ${props.className}`}
style={theme == "light" ? github : darcula}
>
{props.children[0]}
{text}
</SyntaxHighlighter>
);
}
@ -37,9 +42,12 @@ function RenderMarkdown(props) {
}
function Pre({ children }) {
let text = children[0].props.children[0];
let oneline = text.indexOf("\n") <= 1;
console.log(oneline);
return (
<pre className="blog-pre">
<CodeCopyBtn>{children}</CodeCopyBtn>
<pre className={oneline ? "" : "blog-pre"}>
{!oneline && <CodeCopyBtn text={text} />}
{children}
</pre>
);

View file

@ -3,9 +3,7 @@ import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline";
import printDate from "../components/utils";
function Notes() {
return (
<div>
{Object.entries(localStorage.getObj("Notes"))
let notes = Object.entries(localStorage.getObj("Notes"))
.sort((a, b) => {
return b[1].time - a[1].time;
})
@ -35,9 +33,15 @@ function Notes() {
</div>
</div>
);
})}
});
if (notes.length === 0)
return (
<div className="md">
<h3>Заметок пока нет</h3>
</div>
);
return notes;
}
export default Notes;

View file

@ -5,6 +5,6 @@ import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
build: {
outDir: "D:/node-js/anopaper-server/public",
outDir: "./dist",
},
});