Skip to content

Commit e8c8204

Browse files
authored
ECMAscript 6 upgrade and options for custom browser, port (#60)
* Add darkness theme. * Node upgrade dependencies * Barely working mathjax rendering using node.js * Cache mathjax css and make it a command line option * Put darkness theme css back in * mount current working directory to serve static files * Allow use of local mathjax fonts * Use strict; var -> const and let * Move mathjax-node-page config out of function scope * No line break (word wrap) and justify * Use serif font in darkness theme * Add minimalist * Add github-markdown.css, remove deprecated send syntax, upgrade npm packages * Add markdown-it-task-lists plugin * Use minimist to parse --mathjax * Genversion * Implement minimist, help, version, browser options * Add port option * Yarn build genversion and remove github-markdown-css * Put all css together * Fix xdg-open bug again * Travis configuration and tests * Sleep more, test more * Use npm alone, avoid npx * Curl fail * Curl be silent * Clean exit on DELETE request * Add title tag * Add issue template
1 parent a024893 commit e8c8204

File tree

12 files changed

+242
-47
lines changed

12 files changed

+242
-47
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Problem summary
2+
3+
## Expected
4+
5+
6+
## Environment Information
7+
8+
* OS:
9+
* Vim / Neovim version:
10+
* Browser:
11+
12+
## Provide the output of test command (required)
13+
14+
Run `npm test&; sleep 5; npm stop` or at least equivalent of `cat
15+
tests/test_math.md | instant-markdown-d --mathjax --debug` and paste the
16+
console output here:
17+
18+
```sh
19+
20+
```
21+
22+
## Generate logfiles if appropriate
23+
24+
1. package-lock.json / yarn.lock
25+
2. minimal `~/.vimrc` or `~/.config/nvim/init.vim` where variables for `vim-instant-markdown` defined
26+
3. `debug.html`
27+
28+
## Steps to reproduce the issue (required)
29+
30+
Start with command you used to install `instant-markdown-d`
31+
32+
1.
33+
2.
34+
3.
35+
36+
37+
## Screen shot (if possible)
38+
39+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules
22
package-lock.json
3+
yarn.lock
4+
yarn*.log
5+
debug.html

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- node
4+
- lts/*
5+
- "8"
6+
7+
install:
8+
- npm install -g .
9+
before_script:
10+
- npm run-script build
11+
script:
12+
- instant-markdown-d --version
13+
- npm test &
14+
- sleep 5
15+
- npm stop
File renamed without changes.
File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* Set black background for code block */
2+
.markdown-body {
3+
font-family: serif;
4+
}
5+
.markdown-body .highlight pre,
6+
.markdown-body pre {
7+
color: #fff;
8+
background-color: #18171b;
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Remove back ground and set font color for diff items */
2+
.hljs-deletion {
3+
background: transparent;
4+
color: #900;
5+
6+
}
7+
8+
.hljs-addition {
9+
background: transparent;
10+
color: #009926;
11+
}

index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en" class="">
33
<head>
4+
<title>instant-markdown-d</title>
45
<meta charset="utf-8">
56
<meta http-equiv="X-UA-Compatible" content="IE=edge">
67
<meta http-equiv="Content-Language" content="en">
7-
<link rel="stylesheet" type="text/css" href="/github-syntax-highlight.css">
8-
<link rel="stylesheet" type="text/css" href="/github-markdown.css">
8+
<link rel="stylesheet" type="text/css" href="/css/github-syntax-highlight.css">
9+
<link rel="stylesheet" type="text/css" href="/css/github-markdown.css">
910
<link rel="stylesheet" type="text/css" href="/css/mjpage-html.css">
1011
<style>
1112
.markdown-body {
@@ -29,7 +30,7 @@
2930
setDisconnected(false);
3031
socket.on('newContent', function(newHTML) {
3132
document.querySelector(".markdown-body").innerHTML = newHTML;
32-
33+
3334
// scroll to the hyperlink with id="marker"
3435
let marker = document.getElementById("marker");
3536
if(marker) {

instant-markdown-d

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
11
#!/usr/bin/env node
2-
3-
var MarkdownIt = require('markdown-it');
4-
var hljs = require('highlight.js');
5-
var server = require('http').createServer(httpHandler),
2+
"use strict";
3+
const MarkdownIt = require('markdown-it');
4+
const hljs = require('highlight.js');
5+
const server = require('http').createServer(httpHandler),
66
exec = require('child_process').exec,
77
io = require('socket.io').listen(server),
88
os = require('os'),
9+
fs = require('fs'),
910
send = require('send');
1011

11-
var mjpage = require('mathjax-node-page').mjpage;
12-
// var fs = require('fs'); // for debugging
13-
var enableMathJax = process.argv[2] === "--mathjax";
12+
const mjpage = require('mathjax-node-page').mjpage;
13+
const taskLists = require('markdown-it-task-lists');
14+
const argv = require('minimist')(process.argv.slice(2), {
15+
string: ['browser'],
16+
default: {port: 8090, debug: false},
17+
alias: {V: 'version', h: 'help'},
18+
});
19+
20+
if (argv.version || argv.debug) {
21+
const version= require('./version');
22+
console.log(`instant-mardown-d version: v${version}`);
23+
console.log(`nodejs version: ${process.version}`);
24+
}
25+
if (argv.help) {
26+
console.log(`\
27+
Usage: instant-markdown-d [OPTIONS]
28+
29+
Options:
30+
--mathjax Enable MathJax parsing
31+
--browser BROWSER Use a custom browser
32+
--port PORT Use a custom port (default: 8090)
33+
--debug Be verbose and do not open browser
34+
-V, --version Display version
35+
-h, --help Display help\
36+
`);
37+
}
38+
if (argv.version || argv.help) process.exit(0);
1439

40+
if (argv.verbose) console.dir(argv);
41+
42+
// console.dir(argv);
1543
// WARNING: By setting this environment variable, anyone on your network may
1644
// run arbitrary code in your browser and read arbitrary files in the working
1745
// directory of the open file!
1846
if (process.env.INSTANT_MARKDOWN_OPEN_TO_THE_WORLD) {
1947
// Listen on any interface.
20-
server.listen(8090, onListening).once('error', onServerError);
48+
server.listen(argv.port, onListening).once('error', onServerError);
2149
} else {
2250
// Listen locally.
23-
server.listen(8090, '127.0.0.1', onListening).once('error', onServerError);
51+
server.listen(argv.port, '127.0.0.1', onListening).once('error', onServerError);
2452
}
2553

26-
var md = new MarkdownIt({
54+
let md = new MarkdownIt({
2755
html: true,
2856
linkify: true,
2957
highlight: function(str, lang) {
@@ -37,9 +65,9 @@ var md = new MarkdownIt({
3765
return str;
3866
}
3967
}
40-
});
68+
}).use(taskLists);
4169

42-
if (enableMathJax) md.use(require('markdown-it-mathjax')());
70+
if (argv.mathjax) md.use(require('markdown-it-mathjax')());
4371

4472
const mjPageConfig = {
4573
format: ["TeX"],
@@ -59,14 +87,17 @@ const mjNodeConfig = {
5987
};
6088

6189
function mathJaxRenderEmit(newHtml) {
62-
if(enableMathJax) {
90+
if(argv.mathjax) {
6391
mjpage(
6492
newHtml,
6593
mjPageConfig,
6694
mjNodeConfig,
6795
function(data) {
68-
// console.log(data); // resulting HTML string
69-
// fs.writeFileSync('output.html', data, 'utf-8'); // debug
96+
if (argv.debug) {
97+
console.log("Rendered html saved as debug.html")
98+
// console.log(data); // resulting HTML string
99+
fs.writeFileSync('debug.html', data, 'utf-8'); // debug
100+
}
70101
io.sockets.emit('newContent', data);
71102
}
72103
);
@@ -76,14 +107,14 @@ function mathJaxRenderEmit(newHtml) {
76107
}
77108
}
78109

79-
var lastWrittenMarkdown = '';
110+
let lastWrittenMarkdown = '';
80111
function writeMarkdown(body) {
81112
lastWrittenMarkdown = md.render(body);
82113
mathJaxRenderEmit(lastWrittenMarkdown);
83114
}
84115

85116
function readAllInput(input, callback) {
86-
var body = '';
117+
let body = '';
87118
input.on('data', function(data) {
88119
body += data;
89120
if (body.length > 1e6) {
@@ -96,12 +127,12 @@ function readAllInput(input, callback) {
96127
}
97128

98129
function addSecurityHeaders(req, res, isIndexFile) {
99-
var csp = [];
130+
let csp = [];
100131

101132
// Cannot use 'self' because Chrome does not treat 'self' as http://host
102133
// when the sandbox directive is set.
103-
var HTTP_HOST = req.headers.host || 'localhost:8090';
104-
var CSP_SELF = 'http://' + HTTP_HOST;
134+
let HTTP_HOST = req.headers.host || 'localhost:' + argv.port;
135+
let CSP_SELF = 'http://' + HTTP_HOST;
105136

106137
if (!process.env.INSTANT_MARKDOWN_ALLOW_UNSAFE_CONTENT) {
107138
if (isIndexFile) {
@@ -131,35 +162,39 @@ function httpHandler(req, res) {
131162
{
132163
case 'GET':
133164
// Example: /my-repo/raw/master/sub-dir/some.png
134-
var githubUrl = req.url.match(/\/[^\/]+\/raw\/[^\/]+\/(.+)/);
165+
let githubUrl = req.url.match(/\/[^\/]+\/raw\/[^\/]+\/(.+)/);
135166
if (githubUrl) {
136167
addSecurityHeaders(req, res, false);
137168
// Serve the file out of the current working directory
138-
send(req, githubUrl[1])
139-
.root(process.cwd())
169+
send(req, githubUrl[1], {root: process.cwd()})
140170
.pipe(res);
141171
return;
142172
}
143173

144-
var isIndexFile = /^\/(index\.html)?(\?|$)/.test(req.url);
174+
let isIndexFile = /^\/(index\.html)?(\?|$)/.test(req.url);
145175
addSecurityHeaders(req, res, isIndexFile);
146176

177+
let cwd = process.cwd();
178+
let mount = cwd && !fs.existsSync(__dirname + req.url) ? cwd : __dirname;
179+
147180
// Otherwise serve the file from the directory this module is in
148-
send(req, req.url)
149-
.root(__dirname)
181+
send(req, req.url, {root: mount})
150182
.pipe(res);
151183
break;
152184

153185
// case 'HEAD':
154186
// res.writeHead(200);
155187
// res.end();
156-
// exec('open -g http://localhost:8090', function(error, stdout, stderr){
157-
// http.request({port: 8090})
188+
// exec('open -g http://localhost:' + argv.port, function(error, stdout, stderr){
189+
// http.request({port: argv.port})
158190
// });
159191
// break;
160192

161193
case 'DELETE':
194+
res.setHeader('Content-Type', 'text/plain');
195+
res.writeHead(204, { 'Content-Type': 'text/plain' });
162196
io.sockets.emit('die');
197+
res.end('ok')
163198
process.exit();
164199
break;
165200

@@ -177,18 +212,26 @@ io.sockets.on('connection', function(sock){
177212
process.stdout.write('connection established!');
178213
if (lastWrittenMarkdown) {
179214
sock.emit('newContent', lastWrittenMarkdown); // Quick preview
180-
if (enableMathJax) mathJaxRenderEmit(lastWrittenMarkdown);
215+
if (argv.mathjax) mathJaxRenderEmit(lastWrittenMarkdown);
181216
}
182217
});
183218

184219

185220
function onListening() {
186-
if (os.platform() === 'win32') {
187-
exec('start /b http://localhost:8090', function(error, stdout, stderr){});
188-
} else if (os.platform() === 'darwin') {
189-
exec('open -g http://localhost:8090', function(error, stdout, stderr){});
190-
} else { // assume unix/linux
191-
exec('xdg-open http://localhost:8090/', function(error, stdout, stderr){});
221+
if (!argv.browser) {
222+
if (os.platform() === 'win32') {
223+
argv.browser = 'start /b';
224+
} else if (os.platform() === 'darwin') {
225+
argv.browser = 'open -g';
226+
} else { // assume unix/linux
227+
argv.browser = 'xdg-open';
228+
}
229+
}
230+
let cmd = argv.browser + ' http://localhost:' + argv.port + '/';
231+
if (argv.debug) {
232+
console.log("Run the following to manually open browser: \n " + cmd);
233+
} else {
234+
exec(cmd, function(error, stdout, stderr){});
192235
}
193236
readAllInput(process.stdin, function(body) {
194237
writeMarkdown(body);
@@ -202,7 +245,7 @@ function onServerError(e) {
202245
// Forward to existing instant-markdown-d server.
203246
require('http').request({
204247
hostname: 'localhost',
205-
port: 8090,
248+
port: argv.port,
206249
path: '/',
207250
method: 'PUT',
208251
}).end(body);

package.json

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
{
22
"author": "Suan-Aik Yeo <[email protected]>",
33
"name": "instant-markdown-d",
4+
"repository": {
5+
"type": "git",
6+
"url": "git+https://github.com/suan/instant-markdown-d.git"
7+
},
48
"description": "Instantly-updating Markdown Server",
59
"version": "0.2.0",
610
"license": "Apache-2.0",
711
"homepage": "https://github.com/suan/instant-markdown-d",
8-
"repository": {
9-
"type": "git",
10-
"url": "git+https://github.com/suan/instant-markdown-d.git"
12+
"engines": {
13+
"node": ">=8.0.0"
1114
},
12-
"engine": "node",
1315
"main": "instant-markdown-d",
1416
"preferGlobal": "true",
1517
"bin": {
1618
"instant-markdown-d": "./instant-markdown-d"
1719
},
20+
"scripts": {
21+
"start": "./instant-markdown-d",
22+
"build": "genversion ./version.js",
23+
"test": "cat tests/test_math.md | instant-markdown-d --mathjax --debug",
24+
"stop": "curl -X DELETE -s -w 'status: %{http_code}' -m 3 localhost:8090"
25+
},
1826
"dependencies": {
19-
"highlight.js": "^9.13.0",
27+
"highlight.js": "^9.15.6",
2028
"markdown-it": "^8.4.2",
2129
"markdown-it-mathjax": "^2.0.0",
22-
"mathjax-node-page": "^3.0.1",
30+
"markdown-it-task-lists": "^2.1.1",
2331
"mathjax": "^2.7.5",
24-
"send": "~0.16.2",
25-
"socket.io": "^2.1.1"
32+
"mathjax-node-page": "^3.0.2",
33+
"minimist": "^1.2.0",
34+
"send": "~0.17.1",
35+
"socket.io": "^2.2.0"
36+
},
37+
"devDependencies": {
38+
"genversion": "^2.1.1"
2639
}
2740
}

0 commit comments

Comments
 (0)