-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
73 lines (66 loc) · 2.78 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 在页面加载完成后执行一些操作
chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
const currentTab = tabs[0];
const currentUrl = currentTab.url;
// 从当前页面 URL 中获取 owner 和 repo
const { owner, repo } = getOwnerAndRepoFromUrl(currentUrl);
if (owner && repo) {
// 获取 GitHub 项目的 README.md 内容
const readmeContent = await fetchReadme(owner, repo);
if (readmeContent) {
// 使用 marked.js 解析 Markdown 内容
const decodedContent = decodeURIComponent(escape(window.atob(readmeContent)));
const toc = generateTableOfContents(decodedContent);
// 将目录显示在页面上
const sidebar = document.getElementById('sidebar');
sidebar.innerHTML = toc;
}
} else {
console.error('Not a valid GitHub project page.');
}
});
// Function to extract owner and repo from GitHub URL
function getOwnerAndRepoFromUrl(url) {
const match = url.match(/github\.com\/([^\/]+)\/([^\/]+)/);
if (match && match.length === 3) {
const owner = match[1];
const repo = match[2];
return { owner, repo };
}
return { owner: null, repo: null };
}
// GitHub API endpoint to fetch README.md content
const readmeUrl = "https://api.github.com/repos/:owner/:repo/readme";
// Function to fetch README.md content from GitHub
async function fetchReadme(owner, repo) {
const url = readmeUrl.replace(":owner", owner).replace(":repo", repo);
try {
const response = await fetch(url);
const data = await response.json();
const readmeContent = data.content; // Content is already base64 encoded
return readmeContent;
} catch (error) {
console.error("Error fetching README.md:", error);
return null;
}
}
function generateTableOfContents(markdownContent) {
const tokens = marked.lexer(markdownContent); // Parse Markdown content into tokens
// let toc = "<ul>";
let toc = "";
let prevLevel = 0; // Track the previous heading level
tokens.forEach(token => {
if (token.type.startsWith('heading')) {
const title = token.text;
const level = token.depth; // Get the depth of the heading
const id = title.replace(/\s+/g, "-").toLowerCase();
// Calculate the indentation based on the heading level difference
const indent = level - prevLevel > 0 ? "<ul>".repeat(level - prevLevel) : "</ul>".repeat(prevLevel - level);
toc += `${indent}<li><a href="#${id}">${title}</a>`;
prevLevel = level; // Update the previous heading level
}
});
// Close any remaining open unordered lists
// toc += "</li>" + "</ul>".repeat(prevLevel);
return toc;
}