on e9834ee help sign in
webring/scripts/wiki.js
82 lines 2.2 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._names = document.createElement('div')
13 this._names.id = 'names'
15 this.index = {}
16 this.categories = {}
18 this.install = (host) => {
19 console.log('Wiki', 'Installing..')
20 this.fetch()
21 this._el.appendChild(this._names)
22 this._sidebar.appendChild(this._categories)
23 this._el.appendChild(this._sidebar)
24 host.appendChild(this._el)
25 }
27 this.start = () => {
28 console.log('Wiki', 'Starting..')
29 // this.el.innerHTML = 'hello.'
30 }
32 this.refresh = () => {
33 console.log(Object.keys(this.index).length + ' terms')
35 // Cats
37 let html = ''
38 for (const id in this.categories) {
39 html += `<li>${id} <span class='right'>${this.categories[id].length}</span></li>`
40 }
41 this._categories.innerHTML = `${html}`
42 }
44 this.fetch = () => {
45 console.log('Wiki', 'Fetching..')
46 for (const site of sites) {
47 if (!site.wiki || !site.author) { continue }
48 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then((content) => {
49 this.parse(site, content)
50 this.refresh(feeds)
51 }).catch((err) => {
52 console.warn(`${site.wiki}`, err)
53 })
54 }
55 }
57 this.add = (name, value, cat, origin) => {
58 if (!this.index[name]) {
59 this.index[name] = []
60 }
61 this.index[name].push({ value, cat, origin })
62 if (!this.categories[cat]) {
63 this.categories[cat] = []
64 }
65 this.categories[cat].push({ value, cat, origin })
66 }
68 this.parse = (site, content) => {
69 console.log('Wiki', 'Parsing ' + site.wiki)
70 const cats = indental(this.filter(content))
71 for (const cat in cats) {
72 const terms = cats[cat]
73 for (const name in terms) {
74 this.add(name, terms[name], cat, site)
75 }
76 }
77 }
79 this.filter = (lines) => {
80 return lines.split('\n').filter((line) => { return line.indexOf(' = `') < 0 && line.trim().length > 1 }).join('\n')
81 }