Created
October 26, 2021 15:50
-
-
Save hirozed/a2804ea5b03bd447cc4d0e8b7e6e8d45 to your computer and use it in GitHub Desktop.
Example script to pull Hashnode posts
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
const query = ` | |
{ | |
user(username: "hashnodeUser") { | |
publication { | |
posts(page: 0) { | |
slug | |
title | |
coverImage | |
} | |
} | |
} | |
} | |
`; | |
async function fetchPosts() { | |
const response = await fetch( 'https://api.hashnode.com', { | |
method: 'post', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify( { query: query } ) | |
}); | |
const body = await response.json(); | |
let markup = ''; | |
body.data.user.publication.posts.slice(0,6).forEach((post) => { | |
markup += `<li> | |
<a href="https://hashnodeURL.com/${post.slug}">`; | |
if ( post.coverImage.length !== 0 ) { | |
markup += `<img src="${post.coverImage}" alt="${post.title} cover image" />` | |
} | |
markup +=`<div class="homepage-post-content">${post.title}</div> | |
</a> | |
</li>`; | |
}); | |
document.querySelector('.homepage-posts').innerHTML = markup; | |
} | |
fetchPosts(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment