This repository has been archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
134 lines (117 loc) · 4.23 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
;(function($) {
/* Utilities */
const fuseOptions = {
shouldSort: true,
tokenize: true,
threshold: 0.3,
ignoreLocation: true,
minMatchCharLength: 3,
keys: ['href', 'title', 'tags', 'description'],
}
const tagAliases = [
['abolition', 'police', 'prison', 'cop', 'policing', 'jail'],
['anarchist', 'anarchism', 'anarchy'],
['british', 'english', 'britain', 'england', 'uk'],
['castro', 'fidel', 'cuba', 'che'],
['china', 'prc', 'deng', 'xiaoping', 'ccp', 'cpc'],
['covid', 'covid19', 'covid-19', 'coronavirus'],
['debunked', 'debunk', 'debunking', 'myth', 'lie'],
['deng', 'xiaoping'],
['glossary', 'definition', 'dictionary'],
['health', 'healthcare', 'health care'],
['hk', 'hongkong', 'hong-kong', 'hong kong'],
['imperialism', 'anti-imperialism', 'decolonization', 'colonization'],
['israel', 'palestine'],
['latin', 'latam'],
['library', 'libraries'],
['list', 'collection'],
['mao', 'zedong', 'tsetung'],
['ml', 'marxist-leninist', 'leninist', 'stalinist', 'marxist leninist',
'leninism', 'stalinism', 'marxism-leninism', 'marxism leninism'],
['mlm', 'maoist', 'mao', 'maoism'],
['news', 'periodical', 'media', 'msm'],
['north-korea', 'dprk', 'korea', 'juche'],
['soviet-union', 'ussr', 'soviet'],
['sw', 'sex-work', 'sex work'],
['tibet', 'tibetan'],
['trostky', 'trot', 'troskyite', 'troskyism'],
['usa', 'us', 'america', 'united states', 'united-states'],
['uyghur', 'uighur', 'xinjiang'],
['xi', 'jinping'],
['zapatista', 'ezln'],
]
const uniq = (xs) => [...new Set(xs)]
const addPlurals = (tags) =>
uniq(tags.flatMap((tag) =>
[pluralize.plural(tag), pluralize.singular(tag)]))
const addTagAliases = (tags) => {
const pluralized = addPlurals(tags)
const possibleAliases = tagAliases
.filter((xs) =>
xs.find((x) =>
pluralized.includes(x)))
.flat()
return uniq([...pluralized, ...possibleAliases])
}
const enrichResources = (resources) => (
resources.map((a) =>
({ ...a, extraTags: addTagAliases(a.tags) }))
)
const buildResourceItem = ({ href, title, tags, description }) => {
const link = `<a href="${href}" rel="noopener noreferrer nofollower" target="_blank">${title}</a/>`
const tagList = `<small>tags: ${tags.join(' ')}</small>`
return `<li>${[link, description, tagList].filter(Boolean).join('<br>')}</li>`
}
const fetchy = (resources) => {
// enrich resource with plurals and tag aliases from functions above
const enrichedResources = enrichResources(resources)
const fuse = new window.Fuse(enrichedResources, fuseOptions)
// set up filtering code
const addListFilter = () => {
const filterButton = $('#filter')
const ul = $(document.body).find('ul')
// actual filtering function
const filterRows = () => {
const completeList = items
$('ul').html(completeList)
const query = document.getElementById("query").value.trim()
// show all items if no query
if (query === '') {
ul.find('li').show()
} else {
const fuseResults = fuse.search(query)
.map((a) => a.item)
.map((a) => a.href)
const allLis = ul.find('li')
// assume nothing found, then just show found rows
// this should be refactored
ul.find('li').hide()
allLis.filter((_i, element) => {
const href = $(element).find('a').attr('href')
return fuseResults.find((l) => l === href)
}).show()
}
}
$('#filter').on('click', filterRows)
$('#query').on('keyup', (e) => {
if (e.keyCode === 13) {
filterRows()
}
})
}
// build the elements from the resources, and flip it since new
// links are added to the end of the yaml file
const items = enrichedResources.map(buildResourceItem).reverse()
// append all the items and add the filter
$('ul').html(items)
addListFilter()
}
// get the db, convert to js
window.fetch('/db.yml')
.then((res) => res.text())
.then((text) => jsyaml.load(text))
.then((obj) => obj.resources)
.then((resources) => {
fetchy(resources)
})
})(jQuery);