-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (70 loc) · 8.4 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
const libFs = require('fs');
const libPath = require('path');
const ICON_BASE64 = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAARoUlEQVR4Xu2defBv5RzH30XSrYxkCcXYJUrIlqVoGEmhZdK+WEam5VYyGvxhG8u0yG6QNkkh0hRKMRESbVOGGJWaa6Lkpghl3veen/u9377f3/es3/M85/N6Zvrjds95zvN5fT7f1z3nOec8Zw3RIACBsATWCBs5gUMAAkIAFAEEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BBAANQABAITQACBk0/oEEAA1AAEAhNAAIGTT+gQQADUAAQCE0AAgZNP6BCoI4D1JG0naRtJW0h6sqQNJC2RVKc/sgABCFQjcJ+kuyTdJul6SVdKuljSBZL+UaWrKj/YzSUdIWlnSetWOQjbQgACcyFwp6SzJB0r6eoyRywjgE2KDncp0yHbQAACvRPwGcKZkg6XdPNio5klgH0lfUqST/tpEIBAXgSWSzpI0qnThj1NAGtKOkHSO/KKl9FCAAITCPi3v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pvRI7sAkEIDAMAsu93PnC4pEnStpvGHERBQQgUILAim8mLAjAq6BeyfMAJbCxCQTyJ+DVkZ8p6drR5aPPkLRb/rERAQQgMIPA6ZL28DajAnispOskrQ8+CEBgsAR87e9Pnd8yLgD/eS9Jpww2dAKDAAT8L7/PAFa08S/I+P+dIOlgOEEAAoMjcLykpaNRTRLAmpJOlrTn4MInIAjEJXCqpH3GP482SQBGZAnYFpwJxC0YIh8OAf+WD5/0bcRpAlgI3XMC/lQSE4PDKQYiiUPAE35vG73mHw99lgC8ve8OHFPcIiyzfRy8RAqBNAn4Pv/XJB25MNs/bZhVftB+cMCnEbvy2HCaWWdU4Qn4X/wzi3+wry1Do4oAFvpbUnxCya8Qb16sqb5BIYU6/ZUZJ9tAAAKrCPhfeC/kc7uk3xbfNPRr/f5v2odPJ/LjB0tZQSAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA4BBEANQCAwAQQQOPmEDgEEQA1AIDABBBA4+YQOAQRADUAgMAEEEDj5hA6B/wHc7NGE4a68fwAAAABJRU5ErkJggg==`;
function isMarkdown(url) {
return /\.(md|markdown)($|\?)/.test(url);
}
module.exports = {
configSchema: {
auto: {
type: 'boolean',
default: true,
description: 'auto jump to the markdown you are editing'
},
theme: {
type: 'string',
default: 'dark',
description: 'theme for markdown'
}
},
assets: {
test: isMarkdown,
script: ['./assets/index.js'],
style: ['./assets/index.css','./assets/light.css','./assets/dark.css']
},
services: {
// io.call('markdown.test).then(content=> content)
async ['markdown.test'](param) {
return 'markdown test';
}
},
hooks: {
async onRoute(ctx, next, { config }) {
const theme = config.get('theme');
if (isMarkdown(ctx.path)) {
ctx.set('Content-Type', 'text/html');
ctx.status = 200;
ctx.body = `
<html>
<head>
<title>${ctx.path.slice(1)} - Markdown Viewer</title>
<link rel="icon" type="image/png" href="${ICON_BASE64}">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
</head>
<body data-theme='${theme||''}' class='${theme} markdown-body'></body>
</html>`;
}
await next();
},
async onCreate({ io, config, events }) {
const root = config.get('$.root');
events.on('file:change', (payload, ctrl)=>{
if( isMarkdown( payload.path ) ){
ctrl.stop();
// only transfer path to avoid
io.emit('markdown:change', {
path: libPath.relative(root, payload.path)
})
}
})
io.registService('markdown.content', async (payload) => {
return new Promise((resolve, reject) => {
libFs.readFile(libPath.resolve(root, decodeURIComponent(payload)), 'utf8', (err, content) => {
if (err) return reject(err);
else resolve(content);
});
});
});
}
}
};