Skip to content

Instantly share code, notes, and snippets.

@serbrech
Created March 3, 2017 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serbrech/a27129a2cf72d5737c77fb83e64039aa to your computer and use it in GitHub Desktop.
Save serbrech/a27129a2cf72d5737c77fb83e64039aa to your computer and use it in GitHub Desktop.
make a list of blogposts based on a rss feed
function buildList(ulId, posts) {
var lis = posts.map(t => {
var li = document.createElement("li");
var link = document.createElement("a");
link.href = t.url;
link.appendChild(document.createTextNode(t.title));
li.appendChild(link);
return li;
});
var ul = document.getElementById(ulId);
//clear ul
while( ul.firstChild ){
ul.removeChild( ul.firstChild );
}
lis.forEach(it=> ul.appendChild(it));
}
fetch("http://www.erbrech.com/blog/feed.xml")
.then( resp => resp.text() )
.then( txt => {
parser = new DOMParser();
return parser.parseFromString(txt,"text/xml");
})
.then(xml => xml.getElementsByTagName("item"))
.then(items => [].slice.call(items).map(it=>({ "title": it.children[0].textContent, "url": it.children[3].textContent})))
.then(posts => buildList("blogfeed", posts));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment