Skip to content

Commit

Permalink
fix(cc-article-list): fix broken XML with the new website
Browse files Browse the repository at this point in the history
Fixes #867
  • Loading branch information
Galimede committed Oct 19, 2023
1 parent 2126a85 commit 0319784
Show file tree
Hide file tree
Showing 3 changed files with 1,962 additions and 4,649 deletions.
15 changes: 12 additions & 3 deletions src/lib/xml-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@
* @returns {Article[]}
*/
export function parseRssFeed (xmlStr, limit = 9) {

if (limit <= 0) {
return [];
}

const xmlStrTrimmed = xmlStr.trim();
const doc = new DOMParser().parseFromString(xmlStrTrimmed, 'application/xml');
const error = doc.querySelector('parsererror');

if (error != null) {
throw new Error(error.innerHTML);
}

return Array
.from(doc.querySelectorAll('item'))
.from(doc.documentElement.querySelectorAll('item'))
.map((node) => {
const title = node.querySelector('title').textContent;
const link = node.querySelector('link').textContent;
const dateRaw = node.querySelector('pubDate').textContent;
const date = new Date(dateRaw).toISOString();

const descriptionText = node.querySelector('description').childNodes[0].data;
const descriptionNode = new DOMParser().parseFromString(descriptionText, 'text/html');
const banner = descriptionNode.body?.querySelector('.featured-image img')?.src ?? null;
const description = descriptionNode.body.querySelector('p').textContent;
const banner = descriptionNode.body?.querySelector('.wp-post-image')?.src ?? null;
// TODO: we shouldn't have to do the `??` part here but somehow as of 2023-10-18 an article is causing some trouble. We'll have to keep track of this.
const description = descriptionNode.body?.querySelectorAll('p')?.[1]?.textContent ?? descriptionNode.body.textContent;

return { title, link, date, banner, description };
})
.slice(0, limit);
Expand Down

0 comments on commit 0319784

Please sign in to comment.