Arthur Guiot
Arthur Guiot

Reputation: 721

Get rendered html wiki page using the GitHub API

I'm trying to build a web app that gets the content of a GitHub Repo wiki and display the rendered HTML.

I already know how to do that for standard repo:

https://api.github.com/repos/[org]/[repo]/contents/[file]

(I also need to send this header: Accept": "application/vnd.github.v3.html to get the HTML version).

The problem is that I'm not able to get the content of any file if the repo is a wiki.

Here is what I get: Postman: API response

Is there a way of doing it using the API (or another method, I just wanna get the HTML rendered content of a page in a wiki)

Upvotes: 3

Views: 761

Answers (1)

Arthur Guiot
Arthur Guiot

Reputation: 721

I think I found a working solution:

Using the new DOMParser() object in JavaScript, I can fetch the page and get the markdown content using this code:

function getPage(name, repo, file) {
    fetch(`https://github.com/${name}/${repo}/${file}`, {mode: 'cors'})
        .then(data => data.text())
        .then(data => {
            const parser = new DOMParser();
            const dataEl = parser.parseFromString(data, "text/html");
            const el = dataEl.querySelector(".wiki-body > .markdown-body")
            $.html(".content", el.innerHTML)
        })
}
getPage("arguiot", "Glottologist", "wiki")

Upvotes: 1

Related Questions