我需要能够从粘贴到表单中的 github url(到 repo 中的文件)获取用户、repo 和路径,以便能够对其发出 api 请求。例如https://github.com/HubSpot/BuckyClient/blob/master/README.md
我已经开始编写一些正则表达式来尝试定位 url 的部分,但这似乎是一个脆弱的解决方案,因为 github url 有多种格式。
有没有我没有找到的 api 方法,比如 getRepoFromUrl(),或者我可以从提供的 url 中提取此信息的其他更可靠的方法?我正在使用节点/javascript。
谢谢
最佳答案
不使用正则表达式,您是否可以使用 GitHub API 列出特定的 repo url? ,为了解析 JSON 答案,获取应包含您要查找的信息的各种字段:
"owner": {
"login": "octocat",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
由于输入的数据是一个url,查看该url的html页面的元数据包括什么。
对于我的 repo https://github.com/VonC/compileEverything ,我明白了:
<title>VonC/compileEverything</title>
<meta content="VonC/compileEverything" property="og:title" />
...
这应该至少允许取回用户名和存储库名称。
关于javascript - 从 github url 中查找用户、repo 和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28852056/