-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrss.js
175 lines (153 loc) · 4.89 KB
/
rss.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// jshint esversion:10
// This module will handle the reading of the risks feed.
// Presenting back to the server the data it needs to act as a foreign wiki server.
import { deserializeFeed } from 'https://deno.land/x/[email protected]/mod.ts'
import miniSearch from 'https://cdn.skypack.dev/[email protected]'
const refreshInterval = 43200000 // 12 hours
let lastUpdate
async function fetchAndExtract(url) {
let sitemap = []
let pageData = new Map()
let lastBuildDate
let siteIndex = new miniSearch({
fields: ['title', 'content']
})
function addPage(spec) {
console.log('*** addPage : ', spec)
const { title, story, created } = spec
const slug = title.replace(/\s/g, '-').replace(/[^A-Za-z0-9-]/g, '').toLowerCase()
if (pageData.has(slug)) {
console.log('ignoring duplicate title')
return
}
pageData.set(slug, {
title,
story,
created
})
sitemap.push({
slug,
title,
date: created.date,
synopsis: story[0]
})
siteIndex.add({
'id': slug,
'title': title,
'content': story.join(' ')
})
}
await fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch feed')
}
return response
})
.then(response => {
lastUpdate = Date.parse(response.headers.get('last-modified'))
return response
})
.then(response => response.text())
.then(str => {
return deserializeFeed(str)
})
.then(deserialized => {
const { feed /* , feedType */ } = deserialized
return feed
})
.then(feed => {
const { channel } = feed
const { title, description } = channel
lastBuildDate = Date.parse(channel.lastBuildDate)
addPage({
title: title,
story: [ channel.description, `Content created from The Risks Digest [${channel.link} ${channel.link}]` ],
created: {
date: lastBuildDate
}
})
// also add a fake welcome visitors
addPage({
title: 'Welcome Visitors',
story: [ 'Welcome to this [[Federated Wiki]] site. From this page you can find who we are and what we do. New sites provide this information and then claim the site as their own. You will need your own site to participate.' ],
created: {
date: lastBuildDate
}
})
return channel
})
.then(channel => {
channel.items.forEach(function(item, no) {
let story = item.description
// trim pre tags, if present
if (story.startsWith('<pre>')) {
story = story.replace(/(^\<pre\>|\<\/pre\>$)/g , '')
}
// remove shield and flashlight
story = story.replace(/(\<i class="shield fad fa-shield"\>\<\/i\>)|\<i class="flashlight fad fa-flashlight"\>\<\/i\>/g, '')
// wiki-ify links
function linkReplacer(match, p1, p2, offset, string) {
return `[${p1} ${p2}]`
}
// - risk digest links appear to have a consistent format, so a simple picking appart.
story = story.replace(/(?:\<)?\<a href="(.*)"\>(.*)\<\/a\>(?:\>)?/g, linkReplacer)
// split the story into item on double line break.
const storyArray = story.split('\n\n')
// remove line breaks in the story items
storyArray.forEach(function(item, i) {
item = item.replace(/\n/g, ' ')
if (item.includes('&')) {
item = {type: 'markdown', text: item}
}
storyArray[i] = item
})
storyArray.push({
type: 'markdown',
text: `Source: ${item['dc:creator']} via [${item['link']} The Risks Digest]`
})
//
addPage({
title: item.title,
story: storyArray,
created: {
date: Date.parse(item.pubDate),
source: item['dc:creator'],
link: item.link
}
})
})
})
return {
sitemap,
siteIndex,
pageData,
lastUpdate
}
}
export async function rssWikiConstructor(spec) {
const { feedURL } = spec
let { sitemap, siteIndex, pageData, lastUpdate } = await fetchAndExtract(feedURL)
let lastBuildDate = Date.now()
// need to add a refresh function
// but first worth keeping an eye on if deploy is restarting frequently enough not to need worry about refresh
async function refresh() {
console.info('refresh called', lastBuildDate, refreshInterval, Date.now())
if (Date.now() < lastBuildDate + refreshInterval) {
console.info('refresh not needed')
return
} else {
console.log('refreshing content')
let { sitemap, siteIndex, pageData, lastUpdate } = await fetchAndExtract(feedURL)
lastBuildDate = Date.now()
return
}
}
return Object.freeze({
get sitemap () { return sitemap },
get siteIndex () { return siteIndex },
get pageData () { return pageData },
get lastUpdate () { return lastUpdate},
refresh
})
}