forked from m5stack/m5-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (76 loc) · 2.74 KB
/
server.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
75
76
77
78
79
80
// Dependencies
// =============================================================================
const browserSync = require('browser-sync').create();
const compression = require('compression');
browserSync.init({
files: [
'./dist/**/*.*',
'./docs/**/*.*'
],
ghostMode: {
clicks: false,
forms : false,
scroll: false
},
open: false,
notify: false,
cors: true,
reloadDebounce: 1000,
reloadOnRestart: true,
server: {
baseDir: [
'./docs/'
],
middleware: [
compression()
]
},
serveStatic: [
'./dist/'
],
snippetOptions: {
rule: {
match: /<\/body>/i,
fn: function (snippet, match) {
// Fix CSS injection for alternate stylesheets
const styleSwitchFix = `
<script>
(function() {
if (window.MutationObserver) {
window.browsersyncObserver = new MutationObserver(function(mutationsList) {
mutationsList.forEach(function(mutation) {
Array.apply(null, mutation.addedNodes).forEach(function(node) {
var isLink = node.tagName === 'LINK';
var isStylesheet = isLink && (node.getAttribute('rel') || '').indexOf('stylesheet') !== -1;
if (isStylesheet) {
node.disabled = !node.disabled;
node.disabled = !node.disabled;
}
});
});
});
browsersyncObserver.observe(document.documentElement, {
childList: true,
subtree: true
});
}
})();
</script>
`;
return snippet + styleSwitchFix + match;
}
}
},
rewriteRules: [
{
// Replace CDN default with local js path
match : /"https:\/\/unpkg\.com\/docsify-themeable"/g,
replace: '"/js/docsify-themeable.min.js"'
},
{
// Replace CDN URLs with local paths
match : /https:\/\/unpkg\.com\/docsify-themeable\/dist\//g,
replace: '/'
}
]
});