Skip to content
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"react-hot-loader": "^4.13.0",
"react-i18next": "^14.1.2",
"react-modal": "3.15.1",
"stackedit-js": "^1.0.7",
"stream": "^0.0.3",
"strip-indent": "^4.0.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/api/githubApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getToken, saveToken } from '../helpers/github-token';
import { getGithubToken, saveGithubToken } from '../helpers/github-token';

export const githubRequest = async (endpoint: string, options: RequestInit = {}): Promise<any | null> => {
const token = getToken();
const token = await getGithubToken();
if (!token) {
return null;
}
Expand All @@ -16,4 +16,4 @@ export const githubRequest = async (endpoint: string, options: RequestInit = {})
}
};

export { saveToken, getToken };
export { saveGithubToken, getGithubToken };
24 changes: 24 additions & 0 deletions src/helpers/OSS101Textbooks-url-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const baseUrl = 'https://www.x-lab.info/oss101-bok/textbook/'; // Document URL prefix
const repoName = 'X-lab2017/oss101-bok'; // repository name
const branch = 'master'; // repository branch
const platform = 'Github'; // repository platform
export function OSS101TextbooksUrlParser(url: string) {
// Determine if the URL starts with the specified open finger path
console.log(url.startsWith(baseUrl));
if (url.startsWith(baseUrl)) {
// Extract the remaining path
const docPath = url.replace(baseUrl, '').split('#')[0];

// Splicing together the corresponding file path for repository
const filePath = `docs/textbook/${docPath.slice(0, -1)}.md`;

return {
filePath: filePath,
repoName: repoName,
branch: branch,
platform: platform,
};
}
//If there is no match, return null
return null;
}
23 changes: 23 additions & 0 deletions src/helpers/digitalTextbooks-url-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const baseUrl = 'https://www.x-lab.info/digital-textbooks/textbooks/'; // Document URL prefix
const repoName = 'X-lab2017/digital-textbooks'; // repository name
const branch = 'main'; // repository branch
const platform = 'Github'; // repository platform
export function digitalTextbooksUrlParser(url: string) {
// Determine if the URL starts with the specified open finger path
if (url.startsWith(baseUrl)) {
// Extract the remaining path
const docPath = url.replace(baseUrl, '').split('#')[0];

// Splicing together the corresponding file path for repository
const filePath = `docs/textbooks/${docPath}index.md`;

return {
filePath: filePath,
repoName: repoName,
branch: branch,
platform: platform,
};
}
//If there is no match, return null
return null;
}
32 changes: 32 additions & 0 deletions src/helpers/fastPR-url-rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { digitalTextbooksUrlParser } from './digitalTextbooks-url-parser';
import { openDiggerUrlParser } from './openDigger-url-parser';
import { OSS101TextbooksUrlParser } from './OSS101Textbooks-url-parser';

type UrlRule = {
domains: string[];
ruleFunction: (url: string) => { filePath: string; repoName: string; branch: string; platform: string } | null;
};
const urlRules: UrlRule[] = [
{
domains: ['open-digger.cn', 'open-digger'],
ruleFunction: openDiggerUrlParser,
},
{
domains: ['digital-textbooks', 'x-lab.info/digital-textbooks'],
ruleFunction: digitalTextbooksUrlParser,
},
{
domains: ['x-lab.info/oss101-bok'],
ruleFunction: OSS101TextbooksUrlParser,
},
];
export function matchFastPrUrl(url: string) {
for (const rule of urlRules) {
const isMatch = rule.domains.some((domain) => url.includes(domain));
if (isMatch) {
const result = rule.ruleFunction(url);
return result;
}
}
return null;
}
12 changes: 8 additions & 4 deletions src/helpers/github-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export const saveToken = (token: string) => {
localStorage.setItem('github_token', token);
export const saveGithubToken = (token: string) => {
chrome.storage.sync.set({ github_token: token });
};

export const getToken = (): string => {
return localStorage.getItem('github_token') || '';
export const getGithubToken = (): Promise<string | null> => {
return new Promise((resolve) => {
chrome.storage.sync.get('github_token', (result) => {
resolve(result.github_token || null);
});
});
};
23 changes: 23 additions & 0 deletions src/helpers/openDigger-url-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const baseUrl = 'https://open-digger.cn/docs/'; // Document URL prefix
const repoName = 'X-lab2017/open-digger-website'; // repository name
const branch = 'master'; // repository branch
const platform = 'Github'; // repository platform
export function openDiggerUrlParser(url: string) {
// Determine if the URL starts with the specified open finger path
if (url.startsWith(baseUrl)) {
// Extract the remaining path
const docPath = url.replace(baseUrl, '').split('#')[0];

// Splicing together the corresponding file path for repository
const filePath = `docs/${docPath}.md`;

return {
filePath: filePath,
repoName: repoName,
branch: branch,
platform: platform,
};
}
//If there is no match, return null
return null;
}
30 changes: 29 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,33 @@
"github_token_error_empty": "Token cannot be empty",
"github_token_success_save": "Token saved successfully",
"github_token_success_valid": "Token is valid. Username: {{username}}",
"github_token_error_invalid": "Invalid token or request failed"
"github_token_error_invalid": "Invalid token or request failed",
"fast_pr": "FastPR",
"pr_title_label": "PR Title",
"pr_title_rule": "Please enter the PR title",
"pr_title_placeholder": "Enter PR title",
"pr_content_label": "PR Content",
"pr_content_rule": "Please enter the PR content",
"pr_content_placeholder": "Enter PR content",
"pr_submit": "Submit",
"pr_cancel": "Cancel",
"network_error_load_file": "Network was not able to load the file: {{status}}",
"fetch_error_file": "Failed to fetch file: {{error}}",
"error_fetch_repo_info": "Failed to fetch repository info: {{status}}",
"error_get_or_create_fork": "Failed to get or create fork: {{status}}",
"error_get_latest_sha": "Failed to get the latest SHA submission: {{status}}",
"error_create_branch": "Failed to create a new branch: {{status}}",
"error_get_file_sha": "Failed to get the SHA value of the file: {{status}}",
"error_update_file_content": "Failed to update file content: {{status}}",
"error_create_pr": "Failed to create a new PR: {{status}}",
"status_get_github_token": "Get GitHub token...",
"error_get_github_token": "Failed to get GitHub token.",
"status_check_repo_permission": "Checking repository permission...",
"status_get_or_create_fork": "Get or create fork...",
"status_get_latest_commit_sha": "Get latest commit SHA of the base branch...",
"status_create_branch": "Creating new branch...",
"status_get_file_sha": "Get file SHA...",
"status_update_file_content": "Creating or updating file content...",
"status_create_pr": "Creating PR...",
"success_create_pr": "PR created successfully."
}
30 changes: 29 additions & 1 deletion src/locales/zh_CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,33 @@
"github_token_error_empty": "令牌不能为空",
"github_token_success_save": "令牌保存成功",
"github_token_success_valid": "令牌有效。用户名:{{username}}",
"github_token_error_invalid": "令牌无效或请求失败"
"github_token_error_invalid": "令牌无效或请求失败",
"fast_pr": "FastPR",
"pr_title_label": "PR 标题",
"pr_title_rule": "请输入PR的标题",
"pr_title_placeholder": "输入PR的标题",
"pr_content_label": "PR 内容",
"pr_content_rule": "请输入PR的内容",
"pr_content_placeholder": "输入PR的内容",
"pr_submit": "提交",
"pr_cancel": "取消",
"network_error_load_file": "网络问题无法加载文件:{{status}}",
"fetch_error_file": "获取文件失败:{{error}}",
"error_fetch_repo_info": "获取仓库信息失败:{{status}}",
"error_get_or_create_fork": "获取或创建分叉失败:{{status}}",
"error_get_latest_sha": "获取最新SHA提交失败:{{status}}",
"error_create_branch": "创建新分支失败:{{status}}",
"error_get_file_sha": "获取文件SHA值失败:{{status}}",
"error_update_file_content": "更新文件内容失败:{{status}}",
"error_create_pr": "创建新PR失败:{{status}}",
"status_get_github_token": "获取GitHub令牌...",
"error_get_github_token": "获取GitHub令牌失败",
"status_check_repo_permission": "检查仓库权限...",
"status_get_or_create_fork": "获取或创建分叉...",
"status_get_latest_commit_sha": "获取基准分支的最新提交SHA...",
"status_create_branch": "创建新分支...",
"status_get_file_sha": "获取文件SHA...",
"status_update_file_content": "创建或更新文件内容...",
"status_create_pr": "创建PR...",
"success_create_pr": "PR创建成功"
}
Loading