显示README.Md文件内容在我的网站



我想显示README的内容。我的网站上的GitHub存储库的md文件。据我所知,我需要输入存储库信息(所有者和库),它将返回原始README。在我的HTML页面上使用。

我用的是Django,如果有帮助的话。

您将使用GitHub的API访问存储库文件,然后在HTML中使用保留换行符的元素显示README.md文件内容。API请求将默认到存储库的默认分支(通常是main)。你可以这样做:

function getReadme(user, repo) {
fetch(`https://api.github.com/repos/${user}/${repo}/contents/README.md`) // Fetch the file from GitHub's api
.then(response => response.json())
.then(data => {
const content = atob(data.content); // Convert from base64 to readable text
document.getElementById("readme-text").textContent = content; // Apply content to the document
console.log(content); // Log the content to the console
})
.catch(error => console.log(error)); // Catch any errors
}
getReadme("facebook", "react");
<h1>My README</h1>
<pre id="readme-text"></pre> <!-- "<pre>" preserves line breaks -->

最新更新