-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
146 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { RssFeedEntries } from './types'; | ||
import type { XMLDocument } from 'linkedom/types/xml/document'; | ||
|
||
export function getAtomEntries(doc: XMLDocument): RssFeedEntries { | ||
const entries = []; | ||
|
||
const entryElements = doc.querySelectorAll('entry') || []; | ||
|
||
for (const entryElement of entryElements) { | ||
const url = entryElement.querySelector('link')?.getAttribute('href') ?? entryElement.querySelector('id')?.textContent; | ||
const title = entryElement.querySelector('title')?.textContent; | ||
const published = entryElement.querySelector('published, updated')?.textContent; | ||
|
||
const entry = { | ||
url, | ||
title, | ||
published | ||
}; | ||
|
||
entries.push(entry); | ||
} | ||
|
||
return entries; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { RssFeedEntries } from './types'; | ||
import type { XMLDocument } from 'linkedom/types/xml/document'; | ||
|
||
export function getRssEntries(doc: XMLDocument): RssFeedEntries { | ||
const entries = []; | ||
|
||
const entryElements = doc.querySelectorAll('item') || []; | ||
|
||
for (const entryElement of entryElements) { | ||
const url = entryElement.querySelector('link')?.textContent; | ||
const title = entryElement.querySelector('title')?.textContent; | ||
const published = entryElement.querySelector('pubDate')?.textContent; | ||
|
||
const entry = { | ||
url, | ||
title, | ||
published | ||
}; | ||
|
||
entries.push(entry); | ||
} | ||
|
||
return entries; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { test, expect } from 'vitest'; | ||
import { DOMParser } from 'linkedom'; | ||
import { getAtomEntries } from '../../src/lib/get-atom-entries'; | ||
|
||
const parser = new DOMParser(); | ||
|
||
const atomPath = path.resolve(__dirname, '../fixture/atom.xml'); | ||
const atomXml = fs.readFileSync(atomPath, 'utf-8'); | ||
const atomDoc = parser.parseFromString(atomXml, 'text/xml'); | ||
const emptyAtomDoc = parser.parseFromString('<feed></feed>', 'text/xml'); | ||
const emptyRandomDoc = parser.parseFromString('<hello></hello>', 'text/xml'); | ||
|
||
const atomEntry = { | ||
url: 'atom-url', | ||
title: 'atom-title', | ||
published: 'atom-published' | ||
}; | ||
|
||
test('should handle atom feeds', () => { | ||
const entries = getAtomEntries(atomDoc); | ||
expect(entries).toEqual([atomEntry, atomEntry]); | ||
}); | ||
|
||
test('should return no atom feed entries if there are none', () => { | ||
const entries = getAtomEntries(emptyAtomDoc); | ||
expect(entries).toEqual([]); | ||
}); | ||
|
||
test('should return no atom feed entries if the document is not an atom document', () => { | ||
const entries = getAtomEntries(emptyRandomDoc); | ||
expect(entries).toEqual([]); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { test, expect } from 'vitest'; | ||
import { DOMParser } from 'linkedom'; | ||
import { getRssEntries } from '../../src/lib/get-rss-entries'; | ||
|
||
const parser = new DOMParser(); | ||
|
||
const rssPath = path.resolve(__dirname, '../fixture/rss.xml'); | ||
const rssXml = fs.readFileSync(rssPath, 'utf-8'); | ||
const rssDoc = parser.parseFromString(rssXml, 'text/xml'); | ||
const emptyRssDoc = parser.parseFromString('<rss></rss>', 'text/xml'); | ||
const emptyRandomDoc = parser.parseFromString('<hello></hello>', 'text/xml'); | ||
|
||
const rssEntry = { | ||
url: 'rss-url', | ||
title: 'rss-title', | ||
published: 'rss-published' | ||
}; | ||
|
||
test('should handle rss feeds', () => { | ||
const entries = getRssEntries(rssDoc); | ||
expect(entries).toEqual([rssEntry]); | ||
}); | ||
|
||
test('should return no rss feed entries if there are none', () => { | ||
const entries = getRssEntries(emptyRssDoc); | ||
expect(entries).toEqual([]); | ||
}); | ||
|
||
test('should return no rss feed entries if the document is not an rss document', () => { | ||
const entries = getRssEntries(emptyRandomDoc); | ||
expect(entries).toEqual([]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters