jsdelivr加速Github文件 - plus studio

2024-02-28 21:25:23 浏览数 (1)

假设GitHub路径为https://github.com/username/repo-name/path/to/file ,用jsdelivr可以加速或者在html中引用,路径为https://cdn.jsdelivr.net/gh/username/repo-name/path/to/file

这里实现了一个简单的链接转换函数,可以将GitHub路径转换为jsdelivr路径

代码语言:text复制
def find_first_occurrence(string, substring):
    return string.find(substring)
def gh_path2jsdelivr(path):
    if(find_first_occurrence(path,"/blob/") != -1):
        path = path.replace("/blob/", "/")
    if(find_first_occurrence(path,"/main/") != -1):
        path = path.replace("/main/", "/")
    if(find_first_occurrence(path,"/master/") != -1):
        path = path.replace("/master/", "/")
    return path.replace("github.com", "cdn.jsdelivr.net/gh", 1)

print(gh_path2jsdelivr(""))

0 人点赞