-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
render.js
62 lines (55 loc) · 1.72 KB
/
render.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
#!/usr/bin/env node
// @ts-check
'use strict';
const fs = require('fs');
const url = require('url');
const markdown = require('markdown-it')({linkify: true, html: true});
const open = require('open');
const rtd = require('rich-text-diff');
const colour = process.env.NODE_DISABLE_COLORS ?
{ red: '', yellow: '', green: '', normal: '' } :
{ red: '\x1b[31m', yellow: '\x1b[33;1m', green: '\x1b[32m', normal: '\x1b[0m' };
function processConflicts(md) {
let lines = md.split('\r').join('').split('\n');
for (let i=0;i<lines.length;i++) {
if (lines[i].startsWith('<<<<<<')) {
i++;
let start = i;
let before = '';
let after = '';
while (!lines[i].startsWith('======')) {
before = before + lines[i]+'\n';
i++;
}
i++;
while (!lines[i].startsWith('>>>>>>')) {
after = after + lines[i]+'\n';
i++;
}
let diff = rtd(before,after);
diff = diff.split('<ins>').join(colour.green);
diff = diff.split('</ins>').join(colour.normal);
diff = diff.split('<del>').join(colour.red);
diff = diff.split('</del>').join(colour.normal+' ');
const diffLines = diff.split('\n');
lines.splice(start,i-start,...diffLines);
console.log(diff);
i = start;
}
}
return lines.join('\n');
}
if (process.argv.length > 2) {
const outfile = process.argv.length > 3 ? process.argv[3] : './markdown.html';
let md = fs.readFileSync(process.argv[2],'utf8');
let output;
if (md.indexOf('<<<<<<') >= 0) {
output = processConflicts(md);
}
output = markdown.render(md);
fs.writeFileSync(outfile,output,'utf8');
open(url.pathToFileURL(outfile).toString())
.catch(function(ex){
console.warn(ex.message);
});
}