Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gatsby-node.js adding alternative creator from RSS feed #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

gatsby-node.js adding alternative creator from RSS feed #6

wants to merge 6 commits into from

Conversation

lp-mkdir
Copy link

@lp-mkdir lp-mkdir commented Mar 28, 2021

https://github.com/sofa-boys/gatsby-theme-podcast

I was trying to use the theme with a specific RSS which did not has the property "dc:creator" instead it's possible to take it from "itunes:author". I did edit the file like this in order to work:

const axios = require('axios');
const xml2js = require('xml2js');

module.exports = {
  sourceNodes: async (
    { actions, createNodeId, createContentDigest },
    options,
  ) => {
    const { createNode } = actions;

    const rssRes = await axios.get(options.rssUrl);
    const xmlParser = new xml2js.Parser();
    const rssJson = await xmlParser.parseStringPromise(rssRes.data);
    // check for author in dc:creator or itunes:author
    // due that not all RSS contain "dc:creator" and will generate an error
    const creator = rssJson.rss.channel[0].item.hasOwnProperty('dc:creator') ? 'dc:creator' : 'itunes:author'

    rssJson.rss.channel[0].item.forEach(item => {
      const nodeData = {
        creator: item[creator][0],
        pubDate: item.pubDate[0],
        num: item['itunes:episode'] ? parseInt(item['itunes:episode'][0]) : 0,
        title: item.title[0],
        descriptionHtml: item.description[0],
        enclosureUrl: item.enclosure[0].$.url,
        coverImgUrl: item['itunes:image'][0].$.href,
      };
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants