on 2d12097 help sign in
webring/scripts/wiki.js
90 lines 2.4 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.loc = ''
16 this.index = {}
17 this.categories = {}
19 this.install = (host) => {
20 console.log('Wiki', 'Installing..')
21 this.fetch()
22 this._el.appendChild(this._names)
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 // Sidebar
36 let html = ''
37 for (const id in this.categories) {
38 html += `<li onclick='wiki.go("${id}")' class='${wiki.at(id) ? 'selected' : ''}'>${id} <span class='right'>${this.categories[id].length}</span></li>`
39 }
40 this._categories.innerHTML = `${html}`
41 }
43 this.go = (q) => {
44 this.loc = q
45 this.refresh()
46 }
48 this.at = (q = '') => {
49 return this.loc.toLowerCase() === q.toLowerCase()
50 }
52 this.fetch = () => {
53 console.log('Wiki', 'Fetching..')
54 for (const site of sites) {
55 if (!site.wiki || !site.author) { continue }
56 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then((content) => {
57 this.parse(site, content)
58 this.refresh(feeds)
59 }).catch((err) => {
60 console.warn(`${site.wiki}`, err)
61 })
62 }
63 }
65 this.add = (name, value, cat, origin) => {
66 if (!this.index[name]) {
67 this.index[name] = []
68 }
69 this.index[name].push({ value, cat, origin })
70 if (!this.categories[cat]) {
71 this.categories[cat] = []
72 }
73 this.categories[cat].push({ value, cat, origin })
74 }
76 this.parse = (site, content) => {
77 console.log('Wiki', 'Parsing ' + site.wiki)
78 const cats = indental(this.filter(content))
79 for (const cat in cats) {
80 const terms = cats[cat]
81 for (const name in terms) {
82 this.add(name, terms[name], cat, site)
83 }
84 }
85 }
87 this.filter = (lines) => {
88 return lines.split('\n').filter((line) => { return line.indexOf(' = `') < 0 && line.trim().length > 1 }).join('\n')
89 }