-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (75 loc) · 2.21 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const postcss = require('postcss')
const tailwind = require('tailwindcss')
const cssvars = require('postcss-css-variables')
var Haikunator = require('haikunator')
var haikunator = new Haikunator({
defaults: {
tokenLength: 0,
}
});
const app = express();
app.use(bodyParser.json());
app.use(express.static('public'))
let port = process.env.PORT;
if (port == null || port == "") {
port = 8000;
}
const config = {
theme: {
extend: {
// colors: {
// white: 'blue'
// }
},
},
}
function parse(html = '') {
return new Promise((resolve, reject) => {
// well maybe thats not the smartest idea to parse html but YOLO:
const classes = html.match(/class="([^"]+)"/g);
const cleanClasses = classes.map(d => d.replace(/(\r\n|\n|\r)/gm, "").replace('class="', '').replace(/"$/, '').trim());
const uniqueClasses = [...new Set(cleanClasses)];
const classmap = uniqueClasses.reduce((prev, curr) => {
prev[curr] = haikunator.haikunate();
return prev;
}, {})
let css = ``;
let resulthtml = html;
uniqueClasses.sort((a, b) => b.length - a.length).forEach(twclass => {
const name = classmap[twclass];
css += `
.${name} {
@apply ${twclass}
}
`;
resulthtml = resulthtml.split(twclass).join(name);
})
postcss([tailwind(config), cssvars])
.process(css, {
from: undefined
})
.then(result => {
resolve({
css: result.css,
html: resulthtml
})
}).catch((e) => {
reject(e);
})
})
}
app.post('/convert', (req, res) => {
console.log('Processing convert')
const html = req.body.html;
parse(html).then(result => {
res.send(JSON.stringify(result));
}).catch(err => {
console.error(err);
res.sendStatus(500);
})
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})