5 const main = document.getElementById('main')
6 const aside = document.getElementById('aside')
7 const progress = document.getElementById('progress')
8 const progressCounter = Progress(sites.map(site => site.wiki))
9 const authors = new Set()
10 const terms = new Set()
12 const storeEntry = (author, url, category, term, entry) => {
13 if (!(category in entries)) {
14 entries[category] = {}
15 entries[category][term] = [{ entry, authors: [{ author, url }] }]
16 } else if (!(term in entries[category])) {
17 entries[category][term] = [{ entry, authors: [{ author, url }] }]
19 const possibleIndex = entries[category][term].findIndex(existing => existing.entry === entry)
21 ? entries[category][term][possibleIndex].authors.push({ author, url })
22 : entries[category][term].push({ entry, authors: [{ author, url }] })
28 const storeEntries = (author, url, category, entries) => {
29 Array.isArray(entries)
30 ? storeEntry(author, url, category, 'list-definitions', entries)
31 : Object.keys(entries).forEach(entry => storeEntry(author, url, category, entry, entries[entry]))
34 const transform = (author, url, ndtl) => {
35 Object.keys(ndtl).forEach(category => storeEntries(author, url, category, ndtl[category]))
38 const parse = (site, content) => {
39 console.log('Wiki', 'Parsing ' + site.wiki)
40 const ndtl = indental(content)
41 transform(site.author, site.url, ndtl)
44 const selected = (key, category) => key.toLowerCase() === category.toLowerCase()
48 const randomTerm = () => {
49 const keys = Object.keys(entries)
50 return keys[Math.floor(Math.random() * keys.length)]
53 const formatSideBarCat = (key, entries) => (currentHtml, cat) => {
54 const catLength = Object.keys(entries[cat]).length
55 const newHtml = `<li class='${selected(key, cat)}'}'>
56 <a href='#${cat}' data-msgs='${catLength}'>${cat.toLowerCase()}</a>
58 return `${currentHtml}${newHtml}`
61 const formatLinks = text => text.replace(
62 /\b((https?|dat):\/\/[^"\s]+)/g,
63 '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
66 const formatAuthor = (authors, author) => {
67 return `${authors}<a class='author' target='_blank' href='${author.url}'> @${author.author}</a>`
70 const formatEntry = (definitions, definition) => {
71 const formatEntryList = entryList => entryList.reduce((items, item) => `${items}<li>${formatLinks(item)}</li>`, '')
73 const newHtml = typeof definition.entry === 'string'
74 ? `<li>${formatLinks(definition.entry)}
75 - ${definition.authors.reduce(formatAuthor, '')}
77 : `<li><ul>${formatEntryList(definition.entry)}
78 - ${definition.authors.reduce(formatAuthor, '')}
81 return `${definitions}${newHtml}`
84 const formatEntries = entries => (currentHtml, category) => {
85 const definitions = entries[category].reduce(formatEntry, '')
86 const title = category === 'list-definitions'
88 : `<li class='name'><strong>${category.toLowerCase()}</strong></li>`
89 return `${currentHtml}<ul class='term'>${title}${definitions}</ul>`
92 const findRelated = key => {
93 const searchOtherCategories = entry => {
94 return Object.keys(entries)
95 .filter(category => Object.keys(entries[category]).some(otherCat => otherCat === entry))
98 const foundMatches = Object.keys(entries[key])
99 .filter(entry => entry !== 'list-definitions')
100 .map(searchOtherCategories)
101 const flattened = [].concat.apply([], foundMatches)
102 return Array.from(new Set(flattened)).filter(related => related !== key)
105 const formatRelated = (allRelated, relatedCategory) => {
106 const newHtml = `<span><a href='#${relatedCategory}'>/${relatedCategory.toLowerCase()}</a> </span>`
107 return `${allRelated}${newHtml}`
110 const refresh = key => {
112 if (entries[key] === undefined) return
113 const items = Object.keys(entries[key]).sort().reduce(formatEntries(entries[key]), '')
114 const relatedCategories = findRelated(key)
115 const relatedHtml = relatedCategories.length > 0
116 ? relatedCategories.reduce(formatRelated, 'Related categories: ')
118 main.innerHTML = `<div class='related'><h1>${key}</h1>${relatedHtml}</div>${items}`
120 const html = `Click a /topic to get started, or try a <a href='#${randomTerm()}'>random page</a>`
121 const stats = `The wiki contains ${terms.size} terms in ${Object.keys(entries).length} categories, from ${authors.size} authors.`
122 main.innerHTML = `<h1>WIKI</h1>${html}<br />${stats}`
125 const homeCat = `<li class='${selected(key, '')}'>
126 <a href='#' data-msgs='${terms.size}'>home</a>
128 const cats = Object.keys(entries).sort().reduce(formatSideBarCat(key, entries), homeCat)
129 aside.innerHTML = `<ul>${cats}</ul>`
132 const refreshProgress = () => {
133 progress.innerHTML = `${progressCounter}`
137 initialize: hash => {
138 console.log('Wiki', 'Fetching...')
140 sites.filter(site => site.wiki && site.author).forEach(site => {
141 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then(content => {
143 refresh(stripHash(hash))
144 progressCounter.success(site.wiki).then(refreshProgress)
146 console.warn(`${site.wiki}`, err)
147 progressCounter.failure(site.wiki).then(refreshProgress)
151 refresh: key => { refresh(key) }
155const stripHash = hash => {
156 const decoded = decodeURIComponent(hash)
157 return decoded.charAt(0) === '#' ? decoded.substring(1) : decoded
160window.addEventListener('hashchange', () => {
161 wiki.refresh(stripHash(window.location.hash).toUpperCase())
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list