简体   繁体   English

Github 语言颜色基于 React 应用程序中的 repo 语言

[英]Github language colour based on repo language in a React App

I am currently building a portfolio website for myself, and I have a section for all my GitHub projects, with public repos fetched automatically from the GitHub API.我目前正在为自己建立一个投资组合网站,我的所有 GitHub 项目都有一个部分,公共回购自动从 GitHub API 获取。

What I am interested in doing, is displaying a coloured blob next to each repository, depending on the language used in it.我感兴趣的是在每个存储库旁边显示一个彩色 blob,具体取决于其中使用的语言。 If for example repo.language = 'javascript' then I would like a yellow blob next to it (which is GitHub's colour for JavaScript).例如,如果repo.language = 'javascript'那么我希望它旁边有一个黄色斑点(这是 GitHub 的 JavaScript 颜色)。 I know that GitHub uses a library called linguist, but don't know how the best implementation of it would be with my web app.我知道 GitHub 使用一个名为 linguist 的库,但不知道我的 web 应用程序的最佳实现方式。

I know the simplest way would be to use for example the language fetched for each repo as the className and specify a colour in CSS, but I am looking for a more advanced option that would not require me to manually update my CSS files every time I make a repo with a new language that I have not used before (and clutter up my CSS)我知道最简单的方法是使用例如为每个 repo 获取的语言作为 className 并在 CSS 中指定颜色,但我正在寻找一个更高级的选项,不需要我每次手动更新我的 CSS 文件用我以前没有使用过的新语言创建一个 repo(并弄乱我的 CSS)

If any of you have ever done it before or have got any ideas, drop them down below;)如果你们中的任何人以前做过或有任何想法,请将它们放在下面;)

For a given repository, you can list its languages through GitHub API v3对于给定的存储库,您可以通过GitHub API v3列出其语言

GET /repos/:owner/:repo/languages

That helps on the HTML content generation side.这有助于 HTML 内容生成方面。

But that still require an up-to-date CSS file, preferably one with all supported languages , like this one for instance .但这仍然需要一个最新的 CSS 文件,最好是一个包含所有支持的语言的文件,例如这个

The OP PRR adds in the comments : OP PRR 在评论中添加:

I just ended up grabbing the top language for each repository, and then storing the hex codes for all the languages I use in a CSS file, so I can then just update it and add more languages when I start using them.我刚刚为每个存储库获取了顶级语言,然后将我使用的所有语言的十六进制代码存储在 CSS 文件中,这样我就可以在开始使用它们时更新它并添加更多语言。

For those struggling with the same, this is the absolute simplest way to do it.对于那些苦苦挣扎的人来说,这绝对是最简单的方法。

Set the CSS classes to be the exact same as the incoming language data coming from the query, eg if repo.language is 'JavaScript', then the CSS class should be .JavaScript { color: #XXXXXX } where color is the hex value corresponding to that languages (those can be found in the Gist @VonC linked to) Set the CSS classes to be the exact same as the incoming language data coming from the query, eg if repo.language is 'JavaScript', then the CSS class should be .JavaScript { color: #XXXXXX } where color is the hex value corresponding到那种语言(可以在链接到的 Gist @VonC 中找到)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM