added support for both RSS and Atom feeds in getAtomFeedItem and implemented getMastodonPost function to fetch non-reply items
This commit is contained in:
parent
7d896ace0d
commit
25d770662a
1 changed files with 51 additions and 3 deletions
54
utils.js
54
utils.js
|
|
@ -53,9 +53,57 @@ export async function getAtomFeedItem(feedUrl) {
|
|||
const res = await fetch(feedUrl)
|
||||
const data = await res.text()
|
||||
const dom = new JSDOM(data)
|
||||
const entry = dom.window.document.querySelector('entry')
|
||||
const title = entry.querySelector('title').textContent
|
||||
const link = entry.querySelector('link').getAttribute('href')
|
||||
|
||||
// check if it's rss (item) or atom (entry)
|
||||
let element = dom.window.document.querySelector('entry')
|
||||
if (!element) {
|
||||
element = dom.window.document.querySelector('item')
|
||||
}
|
||||
|
||||
const title = element.querySelector('title').textContent
|
||||
|
||||
// try to get link - atom uses href attribute, rss uses text content
|
||||
const linkElement = element.querySelector('link')
|
||||
const link = linkElement.getAttribute('href') || linkElement.textContent.trim()
|
||||
|
||||
const cleanTitle = sanitizeHtml(title, {
|
||||
allowedTags: [],
|
||||
allowedAttributes: {},
|
||||
})?.trim()
|
||||
return { text: cleanTitle, url: link }
|
||||
}
|
||||
|
||||
export async function getMastodonPost(feedUrl) {
|
||||
const res = await fetch(feedUrl)
|
||||
const data = await res.text()
|
||||
const dom = new JSDOM(data)
|
||||
|
||||
// get all items from the rss feed
|
||||
const items = dom.window.document.querySelectorAll('item')
|
||||
|
||||
// find the first item that isn't a reply (doesn't start with @)
|
||||
for (const item of items) {
|
||||
const description = item.querySelector('description')?.textContent || ''
|
||||
const title = item.querySelector('title')?.textContent || ''
|
||||
|
||||
// skip if it's a reply (starts with @ mention)
|
||||
if (description.trim().startsWith('@') || title.trim().startsWith('@')) {
|
||||
continue
|
||||
}
|
||||
|
||||
const link = item.querySelector('link')?.textContent.trim()
|
||||
const cleanTitle = sanitizeHtml(title, {
|
||||
allowedTags: [],
|
||||
allowedAttributes: {},
|
||||
})?.trim()
|
||||
|
||||
return { text: cleanTitle, url: link }
|
||||
}
|
||||
|
||||
// fallback to first item if no non-reply found
|
||||
const firstItem = items[0]
|
||||
const title = firstItem.querySelector('title').textContent
|
||||
const link = firstItem.querySelector('link').textContent.trim()
|
||||
const cleanTitle = sanitizeHtml(title, {
|
||||
allowedTags: [],
|
||||
allowedAttributes: {},
|
||||
|
|
|
|||
Loading…
Reference in a new issue