Frank
Frank

Reputation: 2220

Is there a way to dynamically access files and folders hosted on my Github Pages server?

I understand that the web takes major security measures to prevent programs from accessing files on client computers. What I want to do is use AJAX or some other functionality to dynamically access file and folder names in my own public directory located in my Github repository to be served on my static Github page.

To give a little insight into the project I am working on I will give this example: say I want to easily add content to my blog and my directory looks like this:

- website (folder)
-- articles.html
-- articles (folder)
--- top-10-diys.txt
--- child-falls-in-well.txt
--- my-biography.txt
--- new-article.txt

When I add "new-article.txt" to the "articles" folder, I do not want to have to edit the corresponding "articles.html" page to add a link to the new article. I simply want AJAX or a similar functionality to get all the file names in the folder and generate links to my articles.

Is this possible? I simply want to create this functionality for my static Github page. Using AJAX is not a necessity, but I would like a pure Javascript or AJAX method.

EDIT:

I have come across two possible options to give me something like what I am looking for.

Option 1: request a directory on my server with AJAX, which will return a full HTML document with links to all the files in the directory.

Option 2: maintain a text file with all the file names in the directory and use AJAX to load all the files on the list.

However there are problems with both these methods. Option 1 requires me to parse out file names from the HTML document. This is not a clean solution, and depending on the server permissions, it may not work at all. I would rather not rely on this hack.

The problem with Option 2 is that it still requires me to hardcode file names into a text document, and so the solution isn't really dynamic. Sure, I'll save some time maintaining each individual link, but I will still have to maintain the text file. On top of that, this just seems like a sloppy solution.

EDIT AGAIN

I came across an option I didn't see before which is to use a Static Site Generator. The one used by Github is currently Jekyll. If I can't find any other way to get what I want on the fly, I may go with an SSG, but it's really not ideal for my purposes. I'm really just including this edit so people with my problem know their options.

Upvotes: 1

Views: 1163

Answers (1)

jsw324
jsw324

Reputation: 812

I would create a separate Node server API with an endpoint (to be hit from your github pages front-end using AJAX) that fetches the files from that folder and sends them to your front end.

Upvotes: 0

Related Questions