on 89bad78 help sign in
webring/scripts/wiki.js
172 lines 6.3 kB history raw
1'use strict'
3const Wiki = sites => {
4 const entries = {}
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 cleanEntry = entry => {
13 const temp = document.createElement('div')
14 temp.textContent = entry
15 return temp.innerHTML
16 }
18 const storeEntry = (author, url, category, term, unCleanEntry) => {
19 const entry = Array.isArray(unCleanEntry)
20 ? unCleanEntry.map(x => cleanEntry(x))
21 : cleanEntry(unCleanEntry)
23 if (!(category in entries)) {
24 entries[category] = {}
25 entries[category][term] = [{ entry, authors: [{ author, url }] }]
26 } else if (!(term in entries[category])) {
27 entries[category][term] = [{ entry, authors: [{ author, url }] }]
28 } else {
29 const possibleIndex = entries[category][term].findIndex(existing => existing.entry === entry)
30 possibleIndex > -1
31 ? entries[category][term][possibleIndex].authors.push({ author, url })
32 : entries[category][term].push({ entry, authors: [{ author, url }] })
33 }
34 terms.add(term)
35 authors.add(author)
36 }
38 const storeEntries = (author, url, category, entries) => {
39 Array.isArray(entries)
40 ? storeEntry(author, url, category, 'list-definitions', entries)
41 : Object.keys(entries).forEach(entry => storeEntry(author, url, category, entry, entries[entry]))
42 }
44 const transform = (author, url, ndtl) => {
45 Object.keys(ndtl).forEach(category => storeEntries(author, url, category, ndtl[category]))
46 }
48 const parse = (site, content) => {
49 console.log('Wiki', 'Parsing ' + site.wiki)
50 const ndtl = indental(content)
51 transform(site.author, site.url, ndtl)
52 }
54 const selected = (key, category) => key.toLowerCase() === category.toLowerCase()
55 ? 'selected'
56 : ''
58 const randomTerm = () => {
59 const keys = Object.keys(entries)
60 return keys[Math.floor(Math.random() * keys.length)]
61 }
63 const formatSideBarCat = (key, entries) => (currentHtml, cat) => {
64 const catLength = Object.keys(entries[cat]).length
65 const newHtml = `<li class='${selected(key, cat)}'}'>
66 <a href='#${cat}' data-msgs='${catLength}'>${cat.toLowerCase()}</a>
67 </li>`
68 return `${currentHtml}${newHtml}`
69 }
71 const formatLinks = text => text.replace(
72 /\b((https?|dat):\/\/[^"\s]+)/g,
73 '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
74 )
76 const formatAuthor = (authors, author) => {
77 return `${authors}<a class='author' target='_blank' href='${author.url}'> @${author.author}</a>`
78 }
80 const formatEntry = (definitions, definition) => {
81 const formatEntryList = entryList => entryList.reduce((items, item) => `${items}<li>${formatLinks(item)}</li>`, '')
83 const newHtml = typeof definition.entry === 'string'
84 ? `<li>${formatLinks(definition.entry)}
85 - ${definition.authors.reduce(formatAuthor, '')}
86 </li>`
87 : `<li><ul>${formatEntryList(definition.entry)}
88 - ${definition.authors.reduce(formatAuthor, '')}
89 </ul></li>`
91 return `${definitions}${newHtml}`
92 }
94 const formatEntries = entries => (currentHtml, category) => {
95 const definitions = entries[category].reduce(formatEntry, '')
96 const title = category === 'list-definitions'
97 ? ''
98 : `<li class='name'><strong>${category.toLowerCase()}</strong></li>`
99 return `${currentHtml}<ul class='term'>${title}${definitions}</ul>`
100 }
102 const findRelated = key => {
103 const searchOtherCategories = entry => {
104 return Object.keys(entries)
105 .filter(category => Object.keys(entries[category]).some(otherCat => otherCat === entry))
106 }
108 const foundMatches = Object.keys(entries[key])
109 .filter(entry => entry !== 'list-definitions')
110 .map(searchOtherCategories)
111 const flattened = [].concat.apply([], foundMatches)
112 return Array.from(new Set(flattened)).filter(related => related !== key)
113 }
115 const formatRelated = (allRelated, relatedCategory) => {
116 const newHtml = `<span><a href='#${relatedCategory}'>/${relatedCategory.toLowerCase()}</a> </span>`
117 return `${allRelated}${newHtml}`
118 }
120 const refresh = key => {
121 if (key) {
122 if (entries[key] === undefined) return
123 const items = Object.keys(entries[key]).sort().reduce(formatEntries(entries[key]), '')
124 const relatedCategories = findRelated(key)
125 const relatedHtml = relatedCategories.length > 0
126 ? relatedCategories.reduce(formatRelated, 'Related categories: ')
127 : ''
128 main.innerHTML = `<div class='related'><h1>${key}</h1>${relatedHtml}</div>${items}`
129 } else {
130 const html = `Click a /topic to get started, or try a <a href='#${randomTerm()}'>random page</a>`
131 const stats = `The wiki contains ${terms.size} terms in ${Object.keys(entries).length} categories, from ${authors.size} authors.`
132 main.innerHTML = `<h1>WIKI</h1>${html}<br />${stats}`
133 }
135 const homeCat = `<li class='${selected(key, '')}'>
136 <a href='#' data-msgs='${terms.size}'>home</a>
137 </li>`
138 const cats = Object.keys(entries).sort().reduce(formatSideBarCat(key, entries), homeCat)
139 aside.innerHTML = `<ul>${cats}</ul>`
140 }
142 const refreshProgress = () => {
143 progress.innerHTML = `${progressCounter}`
144 }
146 return {
147 initialize: hash => {
148 console.log('Wiki', 'Fetching...')
149 refreshProgress()
150 sites.filter(site => site.wiki && site.author).forEach(site => {
151 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then(content => {
152 parse(site, content)
153 refresh(stripHash(hash))
154 progressCounter.success(site.wiki).then(refreshProgress)
155 }).catch((err) => {
156 console.warn(`${site.wiki}`, err)
157 progressCounter.failure(site.wiki).then(refreshProgress)
158 })
159 })
160 },
161 refresh: key => { refresh(key) }
162 }
165const stripHash = hash => {
166 const decoded = decodeURIComponent(hash)
167 return decoded.charAt(0) === '#' ? decoded.substring(1) : decoded
170window.addEventListener('hashchange', () => {
171 wiki.refresh(stripHash(window.location.hash).toUpperCase())
172})