-
Notifications
You must be signed in to change notification settings - Fork 2
/
route.js
64 lines (54 loc) · 1.31 KB
/
route.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require("express");
const router = express.Router();
const OpenCC = require("opencc");
const SCHEMES = [
"s2t",
"t2s",
"s2tw",
"tw2s",
"s2hk",
"hk2s",
"s2twp",
"tw2sp",
"t2tw",
"t2hk",
];
const reconvert = (str) =>
str
.replace(/(\\u)(\w{1,4})/gi, ($0, $1, $2) =>
String.fromCharCode(parseInt($2, 16))
)
.replace(/&#(x)?(\w+);/g, ($, $1, $2) =>
String.fromCharCode(parseInt($2, $1 ? 16 : 10))
);
const convert = (req, res) => {
let type = req.path.substr(1);
if (type.length === 0) {
type = "t2s";
} else if (!SCHEMES.includes(type)) {
return res.status(200).json({
title: "Error occured",
content: "Wrong conversion scheme specified.",
});
}
try {
const opencc = new OpenCC(`${type}.json`);
const title = opencc.convertSync(reconvert(req.body.title || ""));
const content = opencc.convertSync(reconvert(req.body.content || ""));
return res.status(200).json({
title,
content,
});
} catch (error) {
return res.status(200).json({
title: "Error occured",
content: error.message,
});
}
};
const redirect = (req, res) => {
return res.redirect(301, "https://github.com/HenryQW/OpenCC.henry.wang");
};
router.post("/*", convert);
router.get("/", redirect);
module.exports = router;