on 6c3d783 help sign in
webring/scripts/wiki.js
119 lines 4.3 kB history raw
1'use strict'
3function Wiki (sites) {
4 const feeds = {}
5 this.sites = sites
6 this._el = document.createElement('div')
7 this._el.id = 'wiki'
8 this._sidebar = document.createElement('div')
9 this._sidebar.id = 'sidebar'
10 this._categories = document.createElement('ul')
11 this._categories.id = 'categories'
12 this._entry = document.createElement('div')
13 this._entry.id = 'entry'
15 this.loc = ''
16 this.index = {}
17 this.categories = {}
19 this.install = (host) => {
20 console.log('Wiki', 'Installing..')
21 this.fetch()
22 this._el.appendChild(this._entry)
23 this._sidebar.appendChild(this._categories)
24 this._el.appendChild(this._sidebar)
25 host.appendChild(this._el)
26 }
28 this.start = () => {
29 console.log('Wiki', 'Starting..')
30 this.loc = window.location.hash.substr(1).replace(/\+/g, ' ').toUpperCase()
31 this._entry.innerHTML = 'Loading..'
32 }
34 this.refresh = () => {
35 console.log(Object.keys(this.index).length + ' terms')
36 // Main
37 if (this.loc) {
38 if (this.categories[this.index]) {
39 let html = `term: ${this.loc}`
40 this._entry.innerHTML = html
41 } else if (this.categories[this.loc]) {
42 let html = ''
43 for (const id in this.categories[this.loc]) {
44 const name = this.categories[this.loc][id].name
45 html += `${this.templateTerm(name, this.index[name])}<br />`
46 }
47 this._entry.innerHTML = html
48 } else {
49 let html = `Unknown: ${this.loc}. Retun <a onclick='wiki.go("")'>home</a>, or try a <a onclick='wiki.go("${this.random()}")'>random page</a>.`
50 this._entry.innerHTML = html
51 }
52 } else {
53 this._entry.innerHTML = `Click a /topic to get started, or try a <a onclick='wiki.go("${this.random()}")'>random page</a>.`
54 }
55 // Sidebar
56 this._categories.innerHTML = Object.keys(this.categories).reduce((acc, id) => { return this.categories[id].length > 5 ? `${acc}<li onclick='wiki.go("${id}")' class='${wiki.at(id) ? 'selected' : ''}'>${id.substr(0, 20)} <span class='right'>${this.categories[id].length}</span></li>` : acc }, '')
57 this._entry.innerHTML += `<p id='footer'>The <b>Wiki</b> is a decentralized encyclopedia, to join the conversation, simply create yourself an <a href="https://wiki.xxiivv.com/Indental">twtxt</a> feed and <a href="https://github.com/XXIIVV/Webring/">add it</a> to your entry in the <a href="index.html">webring</a>.</p>`
58 }
60 this.go = (q = this.loc) => {
61 this.loc = q
62 window.location.hash = q.toUrl()
63 this.refresh()
64 }
66 this.at = (q = '') => {
67 return this.loc.toUpperCase() === q.toUpperCase()
68 }
70 this.random = () => {
71 const keys = Object.keys(this.categories)
72 const target = Math.floor(Math.random() * keys.length)
73 return keys[target]
74 }
76 this.templateTerm = (name, entry) => {
77 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>'
78 }
80 this.fetch = () => {
81 console.log('Wiki', 'Fetching..')
82 for (const site of sites) {
83 if (!site.wiki || !site.author) { continue }
84 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then((content) => {
85 this.parse(site, content)
86 this.refresh(feeds)
87 }).catch((err) => {
88 console.warn(`${site.wiki}`, err)
89 })
90 }
91 }
93 this.add = (name, value, cat, origin) => {
94 if (!this.index[name]) {
95 this.index[name] = []
96 }
97 this.index[name].push({ name, value, cat, origin })
98 if (!this.categories[cat]) {
99 this.categories[cat] = []
100 }
101 this.categories[cat].push({ name, value, cat, origin })
102 }
104 this.parse = (site, content) => {
105 console.log('Wiki', 'Parsing ' + site.wiki)
106 const cats = indental(this.filter(content))
107 for (const cat in cats) {
108 const terms = cats[cat]
109 for (const name in terms) {
110 this.add(name, terms[name], cat, site)
111 }
112 }
113 }
115 this.filter = (lines) => {
116 return lines.split('\n').filter((line) => { return line.indexOf(' = `') < 0 && line.trim().length > 1 }).join('\n')
117 }
118 String.prototype.toUrl = function () { return this.toLowerCase().replace(/ /g, '+').replace(/[^0-9a-z\+\:\-\.\/\~]/gi, '').trim() }