-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreator.js
More file actions
261 lines (228 loc) · 7.33 KB
/
creator.js
File metadata and controls
261 lines (228 loc) · 7.33 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const {resolve} = require('path')
const marked = require('marked')
const {read, write} = require('flipfile')
const log = require('fliplog')
const curry = require('../../src/deps/fp/curry')
const esc = require('../../src/deps/matcher/to-regexp')
const resolver = curry(2, (base, rel) => resolve(base, rel))
const res = resolver(__dirname)
const resRoot = resolver(res(process.cwd()))
const resDocs = resolver(res('../docdown/'))
// log.quick(esc('<!--'))
const htmlComments = /\<\!\-\-/gmi
const htmlEndComments = /\-\-\>/gmi
const startTagSpace = /\<\s+div/gmi
const endTagSpace = /\div\s+>/gmi
const stringReplace = (replace, replacement) => target => {
while (target.includes(replace)) {
target = target.replace(replace, replacement)
}
return target
}
const replaceStartTag = stringReplace('< div', '<div')
const replaceEndTag = stringReplace('div >', 'div>')
const replaceSpaceTag = stringReplace('< /div>', '</div>')
const replaceHighlight = stringReplace('lang-js', 'highlight js')
const uncomment = x => {
const replaced = x
.replace(htmlComments, '<')
.replace(htmlEndComments, '>')
.replace(endTagSpace, 'div>')
const started = replaceStartTag(replaced)
const ended = replaceEndTag(started)
const spaced = replaceSpaceTag(ended)
const highlight = replaceHighlight(spaced)
return highlight
}
// @TODO:
// const make = path => {
// const FileChain = require('../../_modules/fluent-fs')
// const docsPath = resDocs(path + '.md')
// const htmlPath = res(path + '.html')
//
// const file = FileChain().src(docsPath).read()
// file.contents.replace(htmlComments, '').replace(htmlEndComments, '')
// log.quick(file)
// file.src(htmlPath).write()
// }
const styles = `
<link rel="stylesheet" type="text/css" href="styles.css">
<!--<link rel="stylesheet" type="text/css" href="styles2.css">-->
`
// <!-- <script src="https://cdn.jsdelivr.net/prism/1.6.0/prism.js"></script> -->
// <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.js"></script>
// <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.js"></script>
// <script src="https://sidecar.gitter.im/dist/sidecar.v1.js"></script>
const cdns = `
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
`
const scripts = `
<script src="main.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
/*
window.gitter = {
chat: {
options: {
room: 'fliphub/fliphub'
}
}
}
*/
</script>
`
//
const indexSpecific = `
<style>
body, html, main {
overflow: auto !important;
}
body {
padding: 2em;
}
</style>
`
const notIndex = `
<style>
h1 {
display: none;
}
</style>
`
// <a href="${docsHref}" id="docslink">documentation</a>
const playgroundHref = 'https://aretecode.github.io/chain-able-playground/'
const docHref = 'https://fluents.github.io/chain-able-site/documentation.html'
const nav = isIndex => `
<input type="checkbox" id="open-nav">
<header class="navbar navbar-fixed-top navbar-inverse container-fluid">
<div class="container-fluid">
<div class="navbar-header">
<label class="open-nav" for="open-nav"></label>
<a class="navbar-brand" href="#">
<strong>⛓</strong>
<span class="version">v5.0.0</span>
</a>
</div>
<ul class="nav navbar-nav navbar-left">
<li class="${isIndex ? 'active' : ''}"><a href="/">Home</a></li>
<li class="${isIndex ? '' : 'active'}"><a href="${docHref}">Documentation</a></li>
<li><a href="${playgroundHref}">Try ⛓</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/chain-able/chain-able">GitHub</a></li>
<li><a href="https://gitter.im/fliphub/Lobby">Discuss</a></li>
</ul>
</div>
</header>
`
const input = `
<div class="form-group has-feedback filter">
<input class="form-control"
tabindex="1"
id="name-filter"
placeholder="Filter"
type="text"
data-bind="textInput: filter"
autofocus
>
<span class="form-control-feedback">
<span class="glyphicon glyphicon-search"></span>
</span>
</div>
`
const render = (html, index = false) => {
const htmlClass = index ? '' : 'docs'
const bodyClass = index ? '' : 'layout-docs'
const docsClass = index ? '' : 'doc-main'
return `
<!DOCTYPE html>
<html class="${htmlClass}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>chain-able documentation</title>
${styles}
${cdns}
${index ? indexSpecific : notIndex}
</head>
<body class="${bodyClass}">
${nav(index)}
<nav class="nav-left">
${index ? '' : input}
</nav>
<main class="${docsClass}">
${html}
</main>
${scripts}
</body>
</html>
`
}
/**
* @TODO should render this server side inferno, for practice...
* @TODO should split TOC and MAIN to make it easier, either parse html or react
*/
const js_beautify = require('js-beautify').html
const beautify = data => js_beautify(data, {indent_size: 2, type: 'html'})
// SPLIT ON EVERY HEADER
// STRIP TAGS
//
// FUZZY SEARCH FILTER THIS IN SIDEBAR TO SHOW HEADERS FOR THE CONTENT MATCHING SEARCH...
let contents = []
let toc = []
let doctypes = []
let container = []
// should instead edit data attr of toc pieces...
// const mdParts = md.split('<!-- div class="toc-container" -->')
// toc = mdParts[0]
// contents = mdParts[1]
// const tocCommented = '<div id="toc-hidden" class="hidden">' + toc + '</div>'
// html += tocCommented
const stripTag = x => x.replace(/<[^>]+>/g, '')
// https://www.npmjs.com/package/html2commonmark
const make = (path) => {
const docsPath = resDocs(path + '.md')
const htmlPath = res(path + '.html')
const md = read(docsPath)
let html = marked(md)
html = uncomment(html)
html = render(html)
html = beautify(html)
// const parts = html.split('<div class="toc-container">')
// doctypes = parts[0]
// toc = parts[1]
// const sub = toc.split('<div class="doc-container">')
// toc = sub[0]
// contents = sub[1]
// const tocCommented = '<div id="toc-hidden" class="hidden">' + toc + '</div>'
// html += tocCommented
// toc.split('\n').map(item => {
// if (!item.includes('<li><a')) return item
//
// const itemText = stripTag(item).trim()
// item = item.replace('<li', '<li data-')
// log.quick({item, itemText})
// })
// log.data(toc.split('\n')).echo()
write(htmlPath, html)
return {make}
}
const makeIndex = (path) => {
const readmePath = resRoot('README.md')
const docsPath = resDocs(path + '.md')
const htmlPath = res(path + '.html')
const md = read(docsPath)
const readme = read(readmePath)
let mdPlusReadme = readme + '\n\n\n --- \n\n\n ' + md
mdPlusReadme = mdPlusReadme.replace(/<\/?details>/, '')
mdPlusReadme = mdPlusReadme.replace(/<\/?summary>/, '')
let html = marked(mdPlusReadme)
html = uncomment(html)
html = render(html, true)
write(htmlPath, html)
return {make}
}
make('aio')
makeIndex('README', true)