aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: 745c896100b115e709f45d129460393750c5671f (plain)
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
'use-strict';

const dirTree = require('directory-tree');
const express = require('express');
const fs = require('fs');


const app = express();
const path = 'md';

process.on('uncaughtException', function(err) {
    console.error("[" + new Date() + "] > " + "Error - " + err);
});


const report = require('vfile-reporter');

const remark = require('remark');
const guide = require('remark-preset-lint-markdown-style-guide');
const html = require('remark-html');
const kbd = require('remark-kbd');
const math = require('remark-math');
const mermaid = require('remark-mermaid-simple');
const highlight = require('remark-highlight.js');
const sb = require('remark-special-box');
const qcm = require('remark-qcm');


const useLandScript = " <script> mermaid.contentLoaded(); </script>"

app.use(express.static('public'));

app.get('/' + path + '/*', function(req, res) {
    var url = decodeURI(req.url);
    console.log("[" + new Date() + "] > " + "200 - " + url);
    fs.readFile(url.substr(1), 'utf8', function(err, data) {
        if (err)
            return console.log(err);
    remark()
      .use(guide)
      .use(kbd)
      .use(math)
      .use(mermaid)
      .use(sb)
      .use(qcm)
      .use(highlight)
      .use(html)
      .process(data, function (err, file) {
        //    console.log(head + String(file) + foot);
        res.send(String(file) + useLandScript);
        console.error(report(err || file));
      });
    });
});

app.get('/data', function(req, res) {
    console.log("[" + new Date() + "] > " + "200 - " + req.url);
    res.send(dirTree(path, {extensions:/\.md/}));
});

app.get('/img/*', function (req, res) {
    console.log("[" + new Date() + "] > " + "200 - " + req.url);
    if( path == '/img/ic_info_black_48px.svg' ||
        path == '/imr/ic_error_black_48px.svg' ||
        path == '/imr/ic_good_black_48px.svg' ||
        path == '/imr/ic_bad_black_48px.svg' ||
        path == '/imr/ic_comment_black_48px.svg' ||
        path == '/imr/ic_help_black_48px.svg' ) {
      const img = fs.readFileSync(path);
      res.writeHead(200, {'Content-Type': 'image/svg' });
      res.end(img, 'binary');
    }else {
      const img = fs.readFileSync(req.url.replace('/img', path));
      res.writeHead(200, {'Content-Type': 'image/gif' });
      res.end(img, 'binary');
    }
});

app.get('/', function(req, res) {
    console.log("[" + new Date() + "] > " + "200 - " + req.url);
    fs.readFile('public/index.html', 'utf8', function(err, data) {
        if (err)
            return console.log(err);
        res.send(data);
    });
});

app.get('*', function(req, res) {
    console.error("[" + new Date() + "] > " + "404 - " + req.url);
    fs.readFile('public/404.md', 'utf8', function(err, data) {
        if (err)
            return console.log(err);
        fs.readFile('public/css/style.css', 'utf8', function(err, style) {
            var html = '<html>';
            html += '<head>';
            html += '<style>' + style + '</style>';
            html += '</head>';
            html += '<body class=\'markdown-preview\' data-use-github-style>';
            html += converter.makeHtml(data);
            html += '</body>';
            html += '</html>';
            res.status(404).send(html);
        });
    });
});

var server = app.listen(8090, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log("[" + new Date() + "] > " + "App listening at http://%s:%s", host, port);
});