@@ -14,7 +14,7 @@ function Wiki (sites) {
1414
1515 this._footer = document.createElement('p')
1616 this._footer.id = 'footer'
17− this._footer.innerHTML = `The <b>Wiki</b> is a decentralized encyclopedia, to join the conversation, add a <a href="https://github.com/XXIIVV/webring#joining-the-wiki">wiki:</a> field to your entry in the <a href="https://github.com/XXIIVV/Webring/">webring</a>.`
17+ this._footer.innerHTML = `The <strong>Wiki</strong> is a decentralized encyclopedia, to join the conversation, add a <a href="https://github.com/XXIIVV/webring#joining-the-wiki">wiki:</a> field to your entry in the <a href="https://github.com/XXIIVV/Webring/">webring</a>.`
1818
1919 this.loc = ''
2020 this.byName = {}
@@ -38,17 +38,14 @@ function Wiki (sites) {
3838 // Main
3939 if (this.loc) {
4040 if (this.byCat[this.loc]) {
41− let html = ''
42− for (const id in this.byCat[this.loc]) {
43− const name = this.byCat[this.loc][id].name
44− html += `${this.templateTerm(name, this.byName[name])}<br />`
45− }
41+ const formatHtml = (entries, entry) => `${entries} ${this.templateTerm(entry.name, this.byName[entry.name])}<br />`
42+ const html = this.byCat[this.loc].reduce(formatHtml, '')
4643 this._entry.innerHTML = html
4744 } else if (this.byName[this.loc]) {
48− let html = `${this.templateTerm(this.loc, this.byName[this.loc])}<br />${this.templateRelated(this.loc, this.byName[this.loc])}`
45+ const html = `${this.templateTerm(this.loc, this.byName[this.loc])}<br />${this.templateRelated(this.loc)}`
4946 this._entry.innerHTML = html
5047 } else {
51− let html = `Unknown: ${this.loc}. Retun <a onclick='wiki.go("")'>home</a>, or try a <a onclick='wiki.go("${this.random()}")'>random page</a>.`
48+ const html = `Unknown: ${this.loc}. Retun <a onclick='wiki.go("")'>home</a>, or try a <a onclick='wiki.go("${this.random()}")'>random page</a>.`
5249 this._entry.innerHTML = html
5350 }
5451 } else {
@@ -82,16 +79,24 @@ function Wiki (sites) {
8279 return this.byName[name] ? this.byName[name][0].cat : ''
8380 }
8481
85− this.templateRelated = (name, entry) => {
86− let html = ''
87− const cat = entry.cat
82+ this.templateRelated = (name) => {
8883 const relatedWords = this.related(name)
89− html += `<ul class='col3'>${relatedWords.reduce((acc, item, id) => { return `${acc}<li onclick='wiki.go("${item.name}")' class='${wiki.at(item.name) ? 'selected' : ''}'>${item.name.toLowerCase()}</li>` }, '')}</ul>`
84+ const html = `<ul class='col3'>${relatedWords.reduce((acc, item, id) => { return `${acc}<li onclick='wiki.go("${item.name}")' class='${wiki.at(item.name) ? 'selected' : ''}'>${item.name.toLowerCase()}</li>` }, '')}</ul>`
9085 return html
9186 }
9287
93− this.templateTerm = (name, entry) => {
94− return '<ul class="term"><li class="name"><b>' + name.toLowerCase() + '</b></li>' + entry.reduce((acc, item) => { return `${acc}<li>${item.value} <a class='author' target='_blank' href='${item.origin.url}'> — @${item.origin.author}</a></li>` }, '') + '</ul>'
88+ this.templateTerm = (name, entries) => {
89+ const formatEntry = (acc, entry) => {
90+ if (typeof entry.value === 'string') {
91+ return `${acc}<li>${entry.value}<a class='author' target='_blank' href='${entry.origin.url}'> — @${entry.origin.author}</a></li>`
92+ } else {
93+ const listItems = entry.value.reduce((items, item) => `${items}<li>${item}</li>`)
94+ return `${acc}<li><ul>${listItems}<a class='author' target='_blank' href='${entry.origin.url}'> — @${entry.origin.author}</a></ul></li>`
95+ }
96+ }
97+
98+ const htmlEntries = entries.reduce(formatEntry, '')
99+ return `<ul class="term"><li class="name"><strong>${name.toLowerCase()}</strong></li>${htmlEntries}</ul>`
95100 }
96101
97102 this.fetch = () => {
@@ -108,20 +113,13 @@ function Wiki (sites) {
108113 }
109114
110115 this.add = (name, value, cat, origin) => {
111− //
112− if (!this.byName[name]) {
113− this.byName[name] = []
114− }
116+ this.byName[name] = this.byName[name] || []
115117 this.byName[name].push({ name, value, cat, origin })
116− //
117− if (!this.byCat[cat]) {
118− this.byCat[cat] = []
119− }
118+
119+ this.byCat[cat] = this.byCat[cat] || []
120120 this.byCat[cat].push({ name, value, cat, origin })
121− //
122− if (!this.byAuthor[origin.author]) {
123− this.byAuthor[origin.author] = []
124− }
121+
122+ this.byAuthor[origin.author] = this.byAuthor[origin.author] || []
125123 this.byAuthor[origin.author].push({ name, value, cat, origin })
126124 }
127125
@@ -136,5 +134,5 @@ function Wiki (sites) {
136134 }
137135 }
138136
139− String.prototype.toUrl = function () { return this.toLowerCase().replace(/ /g, '+').replace(/[^0-9a-z\+\:\-\.\/\~]/gi, '').trim() }
137+ String.prototype.toUrl = function () { return this.toLowerCase().replace(/ /g, '+').replace(/[^0-9a-z+:\-./~]/gi, '').trim() }
140138}