Created
March 3, 2017 12:25
-
-
Save serbrech/a27129a2cf72d5737c77fb83e64039aa to your computer and use it in GitHub Desktop.
make a list of blogposts based on a rss feed
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
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