-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-sitemap.js
525 lines (516 loc) · 17.8 KB
/
ui-sitemap.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
* The sitemaps are generated using CSS and HTML to create
* a tree diagram.
*
* First the pages are indexed by scanning the nominated
* folder's src/www folder for HTML spages. The titles
* are extracted from the page. All page-accompanying
* NodeJS files are parsed. The navigation 'navbar*.html'
* files are scanned after the pages.
*
* If a page is noncotigious it means it has no navbar
* defined in its <HTML> tag so the page NodeJS and
* HTML contents are checked to see if anything
* redirects to the page.
*
* The screenshot for each route is selected from the
* generated screenshots, prioritizing the form completion
* and viewing screenshots.
*/
const beautify = require('js-beautify').html
const fs = require('fs')
const HTML = require('./html.js')
const path = require('path')
const template = fs.readFileSync('./ui-sitemap-template.html').toString()
let idNumber = 1
const pageIndex = {}
const navbars = []
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
module.exports = async (rootPath, moduleName, documentationPath, sitemap) => {
let uiPath, title, exampleApp, srcPath
switch (moduleName) {
case '@userdashboard/dashboard':
uiPath = ''
title = 'Dashboard'
srcPath = 'node_modules/@userdashboard/dashboard/src/www'
break
case '@userdashboard/organizations':
uiPath = '/organizations'
title = 'Organizations module'
srcPath = 'node_modules/@userdashboard/organizations/src/www'
break
case '@userdashboard/maxmind-geoip':
uiPath = '/maxmind'
title = 'MaxMind GeoIP module'
srcPath = 'node_modules/@userdashboard/maxmind-geoip/src/www'
break
case '@userdashboard/stripe-connect':
uiPath = '/connect'
title = 'Stripe Connect module'
srcPath = 'node_modules/@userdashboard/stripe-connect/src/www'
break
case '@userdashboard/stripe-subscriptions':
uiPath = '/subscriptions'
title = 'Stripe Subscriptions module'
srcPath = 'node_modules/@userdashboard/stripe-subscriptions/src/www'
break
case 'example-web-app':
title = 'Example web app'
exampleApp = true
srcPath = 'src/www'
break
case 'example-subscription-web-app':
title = 'Example subscription web app'
exampleApp = true
srcPath = 'src/www'
break
}
for (const object of sitemap.guest) {
if (object.htmlFilePath && object.htmlFilePath.indexOf(rootPath) > -1) {
pageIndex[object.url] = object
} else if (object.url === '/') {
pageIndex[object.url] = object
}
}
for (const object of sitemap.user) {
if (object.htmlFilePath && object.htmlFilePath.indexOf(rootPath) > -1) {
pageIndex[object.url] = object
} else if (object.url === '/account' || object.url === `/account${uiPath}`) {
pageIndex[object.url] = object
}
}
for (const object of sitemap.administrator) {
if (object.htmlFilePath && object.htmlFilePath.indexOf(rootPath) > -1) {
pageIndex[object.url] = object
} else if (object.url === '/administrator' || object.url === `/administrator${uiPath}`) {
pageIndex[object.url] = object
}
}
console.log('page index', Object.keys(pageIndex).join('\n'))
console.log('navbars', navbars)
scanNavigationBars(uiPath, `${rootPath}/${srcPath}`)
if (exampleApp) {
scanNavigationBars(uiPath, `${rootPath}/node_modules/@userdashboard/dashboard/src/www`)
const packageJSON = require(`${rootPath}/package.json`)
if (packageJSON && packageJSON.dashboard && packageJSON.dashboard.modules && packageJSON.dashboard.modules.length) {
for (const module of packageJSON.dashboard.modules) {
scanNavigationBars(uiPath, `${rootPath}/node_modules/${module}/src/www`)
}
}
}
const tree = mapTreeIndex(moduleName, exampleApp)
const doc = HTML.parse(template)
doc.getElementsByTagName('title')[0].child = [{
node: 'text',
text: `${title} sitemap`
}]
doc.getElementsByTagName('h1')[0].child = [{
node: 'text',
text: `${title} sitemap`
}]
const navbarTemplate = doc.getElementById('navbar')
if (navbarTemplate) {
doc.getElementById('navigation').child = navbarTemplate.child
navbarTemplate.parentNode.removeChild(navbarTemplate)
}
console.log('rendering tree', JSON.stringify(tree, null, ' '))
if (exampleApp) {
const accountTree = tree['/']['/account']
const administratorTree = tree['/']['/administrator']
renderPath(doc, exampleApp, uiPath, accountTree, '/account', null, pageIndex, moduleName, exampleApp)
renderPath(doc, exampleApp, uiPath, administratorTree, '/administrator', null, pageIndex, moduleName, exampleApp)
} else if (moduleName === '@userdashboard/dashboard') {
const userStem = '/account'
const administratorStem = '/administrator'
renderPath(doc, exampleApp, uiPath, tree['/']['/account'], userStem, null, pageIndex, moduleName)
renderPath(doc, exampleApp, uiPath, tree['/']['/administrator'], administratorStem, null, pageIndex, moduleName)
} else {
const userStem = '/account' + uiPath
const administratorStem = '/administrator' + uiPath
renderPath(doc, exampleApp, uiPath, tree['/']['/account']['/account' + uiPath], userStem, null, pageIndex, moduleName)
renderPath(doc, exampleApp, uiPath, tree['/']['/administrator']['/administrator' + uiPath], administratorStem, null, pageIndex, moduleName)
}
const emptyTags = doc.getElementsByTagName('ul')
for (const tag of emptyTags) {
if (!tag.child || !tag.child.length) {
tag.parentNode.removeChild(tag)
}
}
for (let i = 1, len = idNumber + 1; i < len; i++) {
const list = doc.getElementById(`route-list-${i}`)
if (list && (!list.child || !list.child.length)) {
list.parentNode.removeChild(list)
}
}
const html = beautify(doc.toString(), {
indent_size: 2,
space_in_empty_paren: true
})
const uiFile = moduleName.split('/').pop() + '-sitemap.html'
console.log('writing ui file', uiFile)
fs.writeFileSync(`${documentationPath}/${uiFile}`, html)
for (const url in pageIndex) {
pageIndex[url].html = ''
}
}
function renderPath(doc, exampleApp, uiPath, node, urlPath, parentid, pageIndex, moduleName, exampleApp) {
console.log('renderPath', urlPath, parentid, uiPath, parentid, moduleName, exampleApp)
let pageInfo = pageIndex[urlPath] || pageIndex[urlPath + '/index'] || pageIndex[`${urlPath}/index.html`]
if (!pageInfo) {
if (urlPath === '/') {
pageInfo = {
object: 'route',
urlPath: '/',
title: 'Application home',
screenshotPath: ''
}
} else {
return console.log('exit 1', urlPath, pageIndex)
}
}
if (urlPath !== '/' && urlPath !== '/account' && urlPath !== '/administrator' && pageInfo.htmlFilePath.indexOf(moduleName) === -1) {
return console.log('exit 2', urlPath)
}
const route = {
object: 'route',
id: idNumber++,
urlPath: pageInfo.url,
title: pageInfo.title,
screenshotPath: pageInfo.src
}
if (exampleApp) {
route.urlPath = '#'
}
let target
if (parentid) {
console.log(urlPath, 'parent', parentid)
target = `route-list-${parentid}`
} else {
console.log(urlPath, 'no parent')
target = 'sitemap'
}
if (!doc.getElementById(`route-list-${route.id}`)) {
console.log('rendering route', route, target)
HTML.renderTemplate(doc, route, 'route-template', target)
} else {
console.log('element already exists', route)
}
if (!exampleApp && (urlPath === '/' || urlPath === '/home')) {
const brokenLink = doc.getElementById(`link-${route.id}`)
if (brokenLink && brokenLink.parentNode) {
brokenLink.parentNode.removeChild(brokenLink)
}
const emptyContainer = doc.getElementById(`route-container-${route.id}`)
if (emptyContainer && emptyContainer.parentNode) {
emptyContainer.parentNode.removeChild(emptyContainer)
}
}
if (!exampleApp && uiPath && (urlPath === '/account' || urlPath === '/administrator')) {
const brokenLink = doc.getElementById(`link-${route.id}`)
if (brokenLink && brokenLink.parentNode) {
brokenLink.parentNode.removeChild(brokenLink)
}
}
const nodeKeys = node ? Object.keys(node) : null
if (!nodeKeys || !nodeKeys.length) {
return console.log('no nested urls', urlPath)
}
nodeKeys.sort(indexHomeThenDashboard)
console.log('nested urls', urlPath, nodeKeys)
for (const nestedURL of nodeKeys) {
renderPath(doc, exampleApp, uiPath, node[nestedURL], nestedURL, route.id, pageIndex, moduleName, exampleApp)
}
}
function mapTreeIndex(moduleName, exampleApp) {
console.log('map tree index', moduleName, exampleApp)
const tree = {
'/': {}
}
// first map navigation bars and their links
for (const navbar of navbars) {
const pathParts = navbar.urlPath.substring(1).split('/')
let lastNode = tree['/']
let cumulative = ''
for (const part of pathParts) {
cumulative += '/' + part
if (!lastNode[cumulative]) {
lastNode[cumulative] = {}
}
lastNode = lastNode[cumulative]
}
for (const link of navbar.links) {
lastNode[link.urlPath] = {}
}
}
console.log('map part one', tree, navbars)
// map the noncontigious pages
for (const isolatedPath in pageIndex) {
if (isolatedPath === '/' || (!exampleApp && isolatedPath === '/home')) {
continue
}
const isolatedPage = pageIndex[isolatedPath]
if (!isolatedPage.detached) {
continue
}
const pathParts = isolatedPage.urlPath.substring(1).split('/')
let lastNode = tree['/']
let cumulative = ''
for (const part of pathParts) {
cumulative += '/' + part
if (!lastNode[cumulative]) {
lastNode[cumulative] = {}
}
lastNode = lastNode[cumulative]
}
}
// remap noncontigious files if another page redirects to it
for (const isolatedPath in pageIndex) {
const isolatedPage = pageIndex[isolatedPath]
if (!isolatedPage.detached ||
isolatedPath === '/' ||
isolatedPath === '/home') {
continue
}
// try and find a better parent
for (const parentPath in pageIndex) {
if (parentPath === isolatedPath) {
continue
}
const possibleParentPage = pageIndex[parentPath]
if (possibleParentPage.urlPath === '/' ||
possibleParentPage.urlPath === '/home' ||
possibleParentPage.urlPath === '/account/signin' ||
possibleParentPage.urlPath === '/account/register') {
continue
}
let matchHTML
if (possibleParentPage.pageHTML) {
if (possibleParentPage.pageHTML.indexOf('</head>') > -1) {
let pageHeader = possibleParentPage.pageHTML.substring(0, possibleParentPage.pageHTML.indexOf('</head>'))
pageHeader = pageHeader.substring(pageHeader.indexOf('<head>'))
const header = HTML.parse(pageHeader)
const metaTags = header.getElementsByTagName('meta')
if (metaTags && metaTags.length) {
for (const metaTag of metaTags) {
if (!metaTag.attr || metaTag.attr['http-equiv'] !== 'refresh') {
continue
}
matchHTML = metaTag.attr.content.indexOf(isolatedPath) > -1
}
}
}
}
let matchJS
if (!matchHTML && possibleParentPage.pageJS) {
let redirect = possibleParentPage.pageJS.substring(possibleParentPage.pageJS.indexOf('res.writeHead(302, {'))
redirect = redirect.substring(0, redirect.indexOf('res.end()'))
matchJS = redirect && redirect.indexOf(isolatedPath) > -1
}
if (!matchHTML && !matchJS) {
continue
}
const parentPathParts = parentPath.substring(1).split('/')
let lastNode = tree['/']
let cumulative = ''
for (const part of parentPathParts) {
cumulative += '/' + part
if (!lastNode[cumulative]) {
break
}
lastNode = lastNode[cumulative]
}
deleteTreePath(tree, isolatedPath)
lastNode[isolatedPath] = {}
}
}
// merge the 'single' pages with their 'plural' counterparts
for (const pluralPath in pageIndex) {
if (!pluralPath.endsWith('s')) {
continue
}
const singlePath = pluralPath.substring(0, pluralPath.length - 1)
if (!pageIndex[singlePath]) {
continue
}
let parentPath = pluralPath.substring(0, pluralPath.lastIndexOf('/'))
if (parentPath.indexOf(`/${moduleName}/`)) {
parentPath = parentPath.replace(`/${moduleName}/`, '')
}
const parentPathParts = parentPath.substring(1).split('/')
let parentNode = tree['/']
let cumulative = ''
for (const part of parentPathParts) {
cumulative += '/' + part
if (!parentNode || !parentNode[cumulative]) {
console.log(cumulative, parentNode)
throw new Error('huh')
}
parentNode = parentNode[cumulative]
}
const singleNode = parentNode[singlePath]
deleteTreePath(tree, singlePath)
parentNode[pluralPath] = parentNode[singlePath.replace(singlePath, pluralPath)] || {}
parentNode[pluralPath][singlePath] = singleNode
}
return tree
}
function deleteTreePath(tree, path) {
function scan(node) {
for (const key in node) {
if (key === path) {
delete (node[key])
break
}
if (node[key] && Object.keys(node[key]).length) {
scan(node[key])
}
}
}
scan(tree)
}
function scanNavigationBars(uiPath, directoryPath) {
console.log('scanning navbars', uiPath, directoryPath)
if (!fs.existsSync(directoryPath)) {
console.log('no navbar path', directoryPath)
return
}
const allFiles = fs.readdirSync(directoryPath)
const navbarFiles = []
for (const file of allFiles) {
if (file.startsWith('navbar') && file.endsWith('.html')) {
navbarFiles.push(file)
}
}
navbarFiles.sort(navbarThenPluralThenSingles)
for (const filename of navbarFiles) {
const filePath = path.join(directoryPath, filename)
let urlPath = filePath.substring(filePath.indexOf('/src/www') + '/src/www'.length)
urlPath = urlPath.replace('navbar-', '')
urlPath = urlPath.substring(0, urlPath.length - 5)
if (urlPath.endsWith('/navbar')) {
urlPath = urlPath.substring(0, urlPath.length - '/navbar'.length)
}
if (urlPath === '/account/account' || urlPath === '/administrator/administrator') {
urlPath = urlPath.substring(0, urlPath.lastIndexOf('/'))
}
let title
if (!pageIndex[urlPath]) {
title = urlPath.split('/').pop()
title = title.substring(0, 1).toUpperCase() + title.substring(1)
} else {
title = pageIndex[urlPath].title
if (title.startsWith('View ')) {
title = title.substring('View '.length)
}
for (const letter of alphabet) {
title = title.split(letter).join(` ${letter}`)
}
title = title.trim()
if (title.length) {
title = title.substring(0, 1).toUpperCase() + title.substring(1)
}
}
let screenshotFolder = path.join(__dirname, 'screenshots' + urlPath)
if (urlPath === `/account${uiPath}` || urlPath === `/administrator${uiPath}`) {
screenshotFolder += '/index'
}
let screenshotPath
if (fs.existsSync(screenshotFolder)) {
const screenshots = fs.readdirSync(screenshotFolder)
for (const screenshot of screenshots) {
if (!screenshot.endsWith('desktop-en.png')) {
continue
}
if (screenshot.indexOf('submit-form') > -1 || screenshot.indexOf('complete') > -1) {
screenshotPath = screenshot
break
}
}
}
const links = parseNavigationLinks(uiPath, directoryPath, filename, urlPath)
const object = { object: 'route', urlPath, links, title, screenshotPath: `/screenshots${urlPath}/${screenshotPath}` }
navbars.push(object)
}
for (const filename of allFiles) {
if (filename === 'api' || filename === 'webhooks' || filename.indexOf('.') > -1) {
continue
}
const filePath = path.join(directoryPath, filename)
const fileStat = fs.statSync(filePath)
if (fileStat.isDirectory()) {
scanNavigationBars(uiPath, filePath)
}
}
}
function parseNavigationLinks(uiPath, directoryPath, filename, urlPath) {
const filePath = path.join(directoryPath, filename)
if (!fs.existsSync(filePath)) {
return []
}
const navbarHTML = fs.readFileSync(filePath).toString()
if (!navbarHTML || !navbarHTML.length) {
return []
}
const navigationLinks = HTML.parse(`<div>${navbarHTML}</div>`).getElementsByTagName('a')
const links = []
for (const link of navigationLinks) {
if (!link.attr || !link.attr.href) {
continue
}
if (link.attr.href === '/home' ||
link.attr.href === '/account' ||
link.attr.href === `/account${uiPath}` ||
link.attr.href === '/administrator' ||
link.attr.href === `/administrator${uiPath}` ||
link.attr.href === `${urlPath}s`) {
continue
}
let linkPath = link.attr.href
const question = linkPath.indexOf('?')
if (question > 0) {
linkPath = linkPath.substring(0, question)
}
if (!pageIndex[linkPath] || linkPath === urlPath) {
continue
}
let title = pageIndex[linkPath].title
if (title.startsWith('View ')) {
title = title.substring('View '.length)
}
for (const letter of alphabet) {
title = title.split(letter).join(` ${letter}`)
}
title = title.trim()
if (title.length) {
title = title.substring(0, 1).toUpperCase() + title.substring(1)
}
const screenshotPath = pageIndex[linkPath].screenshotPath
const id = pageIndex[linkPath].id
const route = { object: 'route', urlPath: linkPath, title, id, links: [], screenshotPath }
links.push(route)
}
return links
}
function navbarThenPluralThenSingles(a, b) {
if (a === 'navbar.html') {
return -1
}
if (b === 'navbar.html') {
return 1
}
if (a.endsWith('s.html')) {
return -1
}
if (b.endsWith('s.html')) {
return 1
}
}
function indexHomeThenDashboard(a, b) {
if (a === '/home' || a === '/') {
return 1
}
if (b === '/home' || b === '/') {
return -1
}
return a < b ? 1 : 1
}