Refactor main code

James Peret 7 years ago
parent
commit
ff73df89f9
2 changed files with 34 additions and 72 deletions
  1. 33 71
      index.js
  2. 1 1
      package.json

+ 33 - 71
index.js

@@ -12,15 +12,8 @@ app.set('view engine', 'html');
12 12
 //app.use('/public', express.static(path.join(__dirname + '/node_modules')));
13 13
 app.use(express.static('public'))
14 14
 
15
-
16
-
17
-var get_file = function(req, res){
18
-
15
+var getPath = function(req){
19 16
   var path = "";
20
-  var file_type = "";
21
-  var parts;
22
-  var extension;
23
-
24 17
   if(req.params['folder_10'] != undefined){
25 18
     path = path + req.params['folder_10'] + "/"
26 19
   }
@@ -51,6 +44,15 @@ var get_file = function(req, res){
51 44
   if(req.params['folder_1'] != undefined){
52 45
     path = path + req.params['folder_1'] + "/"
53 46
   }
47
+}
48
+
49
+var get_file = function(req, res){
50
+
51
+  var path = getPath(req);
52
+  var file_type = "";
53
+  var parts;
54
+  var extension;
55
+
54 56
   // Load file or look for index?
55 57
   if(req.params['file'] != undefined){
56 58
     // Set filetype
@@ -78,79 +80,39 @@ var get_file = function(req, res){
78 80
 }
79 81
 
80 82
 var markdown_parser = function(data){
81
-  //var markdown = require( "markdown" ).markdown;
82
-  // var marked = require('marked');
83
-  // marked.setOptions({
84
-  //   renderer: new marked.Renderer(),
85
-  //   gfm: true,
86
-  //   tables: true,
87
-  //   breaks: true,
88
-  //   pedantic: true,
89
-  //   sanitize: false,
90
-  //   smartLists: true,
91
-  //   smartypants: true
92
-  // });
93
-  //return markdown.toHTML(data)
94
-  //return marked(data)
95
-  // var md = require('markdown-it')()
96
-  //             .use(require('markdown-it-mathjax')());
97
-
98 83
   var md = require('markdown-it')({
99 84
       html: true,
100 85
       linkify: true,
101 86
       typographer: true
102 87
     }).use(require('markdown-it-math'), {
103
-      inlineOpen: '\\(',
104
-      inlineClose: '\\)',
105
-      blockOpen: '\\[',
106
-      blockClose: '\\]'
88
+      inlineOpen: '$$',
89
+      inlineClose: '$$',
90
+      blockOpen: '$$',
91
+      blockClose: '$$'
107 92
     }).use(require('markdown-it-highlightjs'), {auto: true, code: false})
108 93
   return md.render(data)
109 94
 }
110 95
 
111
-app.get('/:folder_10/:folder_9/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
112
-  get_file(req, res);
113
-})
114
-
115
-app.get('/:folder_9/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
116
-  get_file(req, res);
117
-})
118
-
119
-app.get('/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
120
-  get_file(req, res);
121
-})
122
-
123
-app.get('/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
124
-  get_file(req, res);
125
-})
126
-
127
-app.get('/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
128
-  get_file(req, res);
129
-})
130
-
131
-app.get('/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
132
-  get_file(req, res);
133
-})
134
-
135
-app.get('/:folder_4/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
136
-  get_file(req, res);
137
-})
138
-
139
-app.get('/:folder_3/:folder_2/:folder_1/:file', function (req, res) {
140
-  get_file(req, res);
141
-})
142
-
143
-app.get('/:folder_2/:folder_1/:file', function (req, res) {
144
-  get_file(req, res);
145
-})
146
-
147
-app.get('/:folder_1/:file', function (req, res) {
148
-  get_file(req, res);
149
-})
96
+var url_paths = [
97
+  '/:file',
98
+  '/:folder_1/:file',
99
+  '/:folder_2/:folder_1/:file',
100
+  '/:folder_3/:folder_2/:folder_1/:file',
101
+  '/:folder_4/:folder_3/:folder_2/:folder_1/:file',
102
+  '/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
103
+  '/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
104
+  '/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
105
+  '/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
106
+  '/:folder_9/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
107
+  '/:folder_10/:folder_9/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
108
+]
109
+
110
+for (var i = 0; i < url_paths.length; i++) {
111
+  app.get(url_paths[i], function (req, res) {
112
+    get_file(req, res);
113
+  })
114
+}
150 115
 
151
-app.get('/:file', function (req, res) {
152
-  get_file(req, res);
153
-})
154 116
 
155 117
 app.listen(3000, function () {
156 118
   console.log('Codex Server – listening on port 3000')

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "@jamesperet/codex-server",
3
-  "version": "0.0.2",
3
+  "version": "0.0.3",
4 4
   "description": "A node file server with super powers.",
5 5
   "main": "index.js",
6 6
   "scripts": {