on c318af1 help sign in
webring/scripts/wiki.js
101 lines 2.9 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.el.innerHTML = 'hello.'
31 }
33 this.refresh = () => {
34 console.log(Object.keys(this.index).length + ' terms')
35 // Main
36 if (this.loc) {
37 if (this.categories[this.index]) {
38 let html = `term: ${this.loc}`
39 this._entry.innerHTML = html
40 } else if (this.categories[this.loc]) {
41 let html = `category: ${this.loc}`
42 this._entry.innerHTML = html
43 } else {
44 let html = `Unknown: ${this.loc}`
45 this._entry.innerHTML = html
46 }
47 } else {
48 this._entry.innerHTML = 'Click a /topic to get started.'
49 }
50 // Sidebar
51 this._categories.innerHTML = Object.keys(this.categories).reduce((acc, id) => { return `${acc}<li onclick='wiki.go("${id}")' class='${wiki.at(id) ? 'selected' : ''}'>${id} <span class='right'>${this.categories[id].length}</span></li>` }, '')
52 }
54 this.go = (q) => {
55 this.loc = q
56 this.refresh()
57 }
59 this.at = (q = '') => {
60 return this.loc.toUpperCase() === q.toUpperCase()
61 }
63 this.fetch = () => {
64 console.log('Wiki', 'Fetching..')
65 for (const site of sites) {
66 if (!site.wiki || !site.author) { continue }
67 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then((content) => {
68 this.parse(site, content)
69 this.refresh(feeds)
70 }).catch((err) => {
71 console.warn(`${site.wiki}`, err)
72 })
73 }
74 }
76 this.add = (name, value, cat, origin) => {
77 if (!this.index[name]) {
78 this.index[name] = []
79 }
80 this.index[name].push({ value, cat, origin })
81 if (!this.categories[cat]) {
82 this.categories[cat] = []
83 }
84 this.categories[cat].push({ value, cat, origin })
85 }
87 this.parse = (site, content) => {
88 console.log('Wiki', 'Parsing ' + site.wiki)
89 const cats = indental(this.filter(content))
90 for (const cat in cats) {
91 const terms = cats[cat]
92 for (const name in terms) {
93 this.add(name, terms[name], cat, site)
94 }
95 }
96 }
98 this.filter = (lines) => {
99 return lines.split('\n').filter((line) => { return line.indexOf(' = `') < 0 && line.trim().length > 1 }).join('\n')
100 }