on 580b66a help sign in
webring/scripts/wiki.js
60 lines 1.5 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'
9 this.index = {}
11 this.install = (host) => {
12 console.log('Wiki', 'Installing..')
13 this.fetch()
14 host.appendChild(this.el)
15 }
17 this.start = () => {
18 console.log('Wiki', 'Starting..')
19 this.el.innerHTML = 'hello.'
20 }
22 this.fetch = () => {
23 console.log('Wiki', 'Fetching..')
24 for (const site of sites) {
25 if (!site.wiki || !site.author) { continue }
26 fetch(site.wiki, { cache: 'no-store' }).then(x => x.text()).then((content) => {
27 this.parse(site, content)
28 this.refresh(feeds)
29 }).catch((err) => {
30 console.warn(`${site.wiki}`, err)
31 })
32 }
33 }
35 this.refresh = () => {
36 console.log(Object.keys(this.index).length + ' terms')
37 }
39 this.add = (name, value, cat, origin) => {
40 if (!this.index[name]) {
41 this.index[name] = []
42 }
43 this.index[name].push({ value, cat, origin })
44 }
46 this.parse = (site, content) => {
47 console.log('Wiki', 'Parsing ' + site.wiki)
48 const cats = indental(this.filter(content))
49 for (const cat in cats) {
50 const terms = cats[cat]
51 for (const name in terms) {
52 this.add(name, terms[name], cat, site)
53 }
54 }
55 }
57 this.filter = (lines) => {
58 return lines.split('\n').filter((line) => { return line.indexOf(' = `') < 0 && line.trim().length > 1 }).join('\n')
59 }