Search code examples
javascriptgithub-api

How to get all the files of a Github repository using Javascript to create an index.html to access all the files


I'm trying to create an easy-access document to all the files of my repository (they are all written in Markdown).

The idea is to make an script in .js that analyzes the repository and lists all the files like links to access to them. (An easy way to see the files from mobile for example)

I've tried to use this code, but it doesn't work to me :/ :

const xhr = new XMLHttpRequest();
    const url = "https://api.github.com/repos/gitblanc/Obsidian-Notes/contents/"
    // Replace -username- with your GitHub username, -repo- with the repository name, and then :path with a path to the file or folder you want to get the content of (leave blank to ge all files of the repository)

    xhr.open('GET', URL, true);

    xhr.onload = function() {
        const data = JSON.parse(this.response);

        console.log(data);
    };
    
    xhr.send();

Solution

  • You are not passing in the url parameter.

    The request hast to look like this:

    const url = "https://api.github.com/repos/gitblanc/Obsidian-Notes/contents/"
    
    xhr.open('GET', url, true);
    

    You had a typo (all caps) with the url variable.