Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions core/pages/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,35 @@ export const addPage = createPage('add', () => {
$url.set(normalizedUrl)
})

/**
* Removes duplicated feed links differing only by protocol.
*/
function dedupeLinks(links: string[]): string[] {
let seen = new Set<string>()
return links.filter(link => {
let key = link.replace(/^https?:\/\//, 'https://')
if (seen.has(key)) return false
seen.add(key)
return true
})
}

/**
* Extracts links to all known feed types from the HTTP response containing
* the HTML document.
*/
function getLinksFromText(response: TextResponse): string[] {
let names = Object.keys(loaders) as LoaderName[]
return names.reduce<string[]>((links, name) => {
/* node:coverage ignore next 5 */
try {
return links.concat(loaders[name].getMineLinksFromText(response))
} catch {
return links
}
}, [])
return dedupeLinks(
names.reduce<string[]>((links, name) => {
/* node:coverage ignore next 5 */
try {
return links.concat(loaders[name].getMineLinksFromText(response))
} catch {
return links
}
}, [])
)
}

/** Guess a list of default/fallback links for all feed types */
Expand Down
39 changes: 39 additions & 0 deletions core/test/pages/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,42 @@ test('supports links with query parameters', async () => {
equal(popup.notFound, false)
equal(popup.param, 'https://example.com/news?format=rss')
})

test('deduplicates feed links differing only by protocol', async () => {
let page = openPage({
params: {},
route: 'add'
})

keepMount(page.candidates)

expectRequest('http://example.com').andRespond(
200,
`<html>
<link type="application/atom+xml" href="https://example.com/feed.atom">
<body>
<a href="/feed.atom"> subscribe </a>
</body>
</html>`
)

let rss = '<feed><title>Feed</title></feed>'
expectRequest('https://example.com/feed.atom').andRespond(
200,
rss,
'text/xml'
)

page.params.url.set('http://example.com')
await waitLoading(page.searching)

equal(page.candidates.get().length, 1)
equalWithText(page.candidates.get(), [
{
loader: loaders.atom,
name: 'atom',
title: 'Feed',
url: 'https://example.com/feed.atom'
}
])
})