我正在尝试构建一个网络应用程序来获取 GitHub Repo wiki 的内容并显示呈现的 HTML。
我已经知道如何为标准 repo 做到这一点:
https://api.github.com/repos/[org]/[repo]/contents/[file]
(我还需要发送此 header :Accept": "application/vnd.github.v3.html
以获取 HTML 版本)。
问题是如果 repo 是 wiki,我无法获取任何文件的内容。
有没有一种方法可以使用 API(或其他方法,我只想获取 wiki 中页面的 HTML 渲染内容)
最佳答案
我想我找到了一个可行的解决方案:
在 JavaScript 中使用 new DOMParser()
对象,我可以使用以下代码获取页面并获取 Markdown 内容:
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")
关于javascript - 使用 GitHub API 获取呈现的 html wiki 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48246333/