-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
165 lines (158 loc) · 4.53 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require('express')
const mathjax = require('mathjax-node')
mathjax.config({
MathJax: {
}
})
mathjax.start()
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json({limit: '10mb'}))
app.use(bodyParser.urlencoded({
extended: false
}))
app.disable('x-powered-by')
const appConfig = require('./config')
app.all('*',async (req,res,next) => {
res.setHeader("Content-Type","application/json;charset=utf-8")
res.setHeader("Access-Control-Allow-Origin",appConfig.ALLOW_ORIGIN)
res.setHeader("Access-Control-Allow-Method",appConfig.ALLOW_METHOD)
res.setHeader("Access-Control-Allow-Credentials",appConfig.ALLOW_CREDENTIALS)
res.setHeader("Access-Control-Allow-Headers",appConfig.ALLOW_HEADERS)
next()
})
const plantuml = require('node-plantuml')
const stream = require('stream')
const util = require('util')
function WritStream(callback){
stream.Writable.call(this)
this.callback = callback
this.content = ''
}
util.inherits(WritStream, stream.Writable)
WritStream.prototype._write = function(chunk, encode, cb){
if(typeof this.callback === 'function' && chunk.toString().indexOf('Welcome to PlantUML!') > -1 )
this.callback(this.content)
this.content += chunk.toString()
cb()
}
plantuml.useNailgun()
const getUMLEncode = text => {
return new Promise((resolve, reject) => {
plantuml.encode(text,{},(_,data) => {
resolve(data)
})
})
}
app.all('/puml',async (req,res) => {
const content = req.query.content || req.body.content
if(!content){
return res.json({
success:false,
message:'not empty'
})
}
const inputArray = content.split('\n')
let parseContent = ''
for(let str in inputArray){
parseContent += inputArray[str] + '\n'
}
const uml = await getUMLEncode(parseContent)
const decode = plantuml.decode(uml)
const gen = plantuml.generate({format: 'svg',charset:'utf-8'})
decode.out.pipe(gen.in)
const getSvgContent = () => {
return new Promise((resolve, reject) => {
const ws = new WritStream(resolve)
gen.out.pipe(ws)
})
}
const svgContent = await getSvgContent()
if(!svgContent){
return res.json({
success:false,
message:'error'
})
}
return res.json({
success:true,
svg:svgContent
})
})
app.all('/latex',async (req,res) => {
const content = req.query.content || req.body.content
if(!content){
return res.json({
success:false,
message:'not empty'
})
}
const getSvgContent = () => {
return new Promise((resolve, reject) => {
try {
mathjax.typeset({
math: content,
format: "TeX", // or "inline-TeX", "MathML"
svg:true, // or svg:true, or html:true
}, function (data) {
if (!data.errors){
resolve(data.svg)
}else{
reject()
}
})
} catch (error) {
reject()
}
})
}
let svgContent = await getSvgContent()
if(!svgContent){
return res.json({
success:false,
message:'error'
})
}
return res.json({
success:true,
svg:svgContent
})
})
app.all('/graphviz',async (req,res) => {
const content = req.query.content || req.body.content
if(!content){
return res.json({
success:false,
message:'not empty'
})
}
const inputArray = content.split('\n')
let parseContent = ''
for(let str in inputArray){
parseContent += inputArray[str] + '\n'
}
const uml = await getUMLEncode(parseContent)
const decode = plantuml.decode(uml)
const gen = plantuml.generate({format: 'svg',charset:'utf-8'})
decode.out.pipe(gen.in)
const getSvgContent = () => {
return new Promise((resolve, reject) => {
const ws = new WritStream(resolve)
gen.out.pipe(ws)
})
}
const svgContent = await getSvgContent()
if(!svgContent){
return res.json({
success:false,
message:'error'
})
}
return res.json({
success:true,
svg:svgContent
})
})
require('./flowchart')
require('./mermaid')
app.listen(3000, () => console.log('Puml/Graphviz/Latex listening on port 3000!'))