zl程序教程

您现在的位置是:首页 >  工具

当前栏目

添加Github徽标

GitHub 添加 徽标
2023-06-13 09:14:09 时间

github徽标

  1. github徽标可以直接通过shields.io在线生成。
  2. 理论上可以放在页面的任何地方。教程案例是添加在页脚。
  3. 工具网站包括:
  4. 预览效果:

具体步骤

通过shields.io在线生成。

  • label:标签,徽标左侧内容
  • message:信息,徽标右侧内容
  • color:色值,支持支持十六进制、rgb、rgba、hsl、hsla和 css 命名颜色。十六进制记得删除前面的#号

输入相关信息后,点击make badge即可得到徽标的URL。可以用img标签引用,写法简单。不过正式写法建议用object标签引用,写法示例如下。

<!-- label=Frame,Message=Hexo,color=blue -->
https://img.shields.io/badge/Frame-Hexo-blue
<!-- 在页面上可以使用img标签来引用 -->
<img src="https://img.shields.io/badge/Frame-Hexo-blue">
<!-- 部分属性例如link需要用object标签来引用 -->
<object data="https://img.shields.io/badge/Frame-Hexo-blue?link=https://hexo.io"></object>

拓展写法示例

  • 属性说明
  • 示例源码
  • 样式预览

仅仅如此肯定是不能令人满意的,还可以继续添加样式。 shields.io提供直接在URL内添加样式属性的功能。使用?引用,使用&连接各属性。 | 属性 | 说明 | 示例 | | :——–: | :—————————————————————————————————–: | :——————————————–: | | style | 徽标样式,默认提供了五种样式: plastic,flat,flat-square, for-the-badge,social | ?style=flat-square | | label | 覆盖默认的左侧文本 (空格或特殊字符需要转URL编码!) | ?label=healthinesses | | logo | 给左侧标签前插入图标 可以访问simpleicons查询图标 | ?logo=data:image/png;base64,url | | logo | 自定义图标,限制较多,不推荐 | 示例 | | logoColor | 设置徽标的颜色(支持十六进制、rgb、rgba、hsl、hsla和 css 命名颜色)。支持命名徽标,但不支持自定义徽标。 | ?logoColor=violet | | logoWidth | 给图标提供的水平空间 | ?logoWidth=40 | | link | 徽标指向的链接,此时需要用object标签引用才能生效写法看示例代码 | ?link=http://example.com | | labelColor | 左侧部分背景色,(支持十六进制、rgb、rgba、hsl、hsla和 css 命名颜色) | ?labelColor=abcdef或者?colorA=abcdef | | color | 右侧部分背景色,(支持十六进制、rgb、rgba、hsl、hsla和 css 命名颜色) | ?color=fedcba或者?colorB=fedcba |

<!-- 普通样式 -->
<img src="https://img.shields.io/badge/Frame-Hexo-blue">
<!-- 五种style预览 -->
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&style=plastic">
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&style=flat">
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&style=flat-square">
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&style=for-the-badge">
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&style=social">
<!-- 添加图标和自定义label -->
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&label=框架">
<!-- 添加图标和图标宽度 -->
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&logoWidth=30">
<!-- 图标、label、message三部分颜色自定义 -->
<img src="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&label=框架&logoColor=violet&labalColor=#1fd041&color=rgb(222, 31, 31)">
<!-- 给标签添加链接 -->
<object data="https://img.shields.io/badge/Frame-Hexo-blue?logo=Hexo&link=https://hexo.io/&https://hexo.io/zh-cn/docs/"></object>
<!-- 也可以通过嵌套a标签来实现添加链接 -->
<a target="_blank" href="https://hexo.io" title="框架采用Hexo"><img src="https://img.shields.io/badge/Frame-Hexo-blue"></a>

在主题配置文件_config.butterfly.ymlfooter配置项中添加徽标,注意事先压缩一下,使他们只留一行。为了不至于太过紧凑,用 (空格的转义字符)隔开。

      footer:
        owner:
          enable: true
          since: 2020
-       custom_text:
+       custom_text: <p><a target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a>&nbsp;<a target="_blank" href="https://demo.jerryc.me/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a>&nbsp;<a target="_blank" href="https://metroui.org.ua/index.html "><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a> &nbsp;<a target="_blank" href="https://vercel.com/ "><img src="https://img.shields.io/badge/Hosted-Vervel-brightgreen?style=flat&logo=Vercel" title="本站采用双线部署,默认线路托管于Vercel"></a>&nbsp;<a target="_blank" href="https://vercel.com/ "><img src="https://img.shields.io/badge/Hosted-Coding-0cedbe?style=flat&logo=Codio" title="本站采用双线部署,联通线路托管于Coding"></a>&nbsp;<a target="_blank" href="https://github.com/"><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&logo=GitHub" title="本站项目由Gtihub托管"></a>&nbsp;<a target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris" title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可"></a></p>
        copyright: false # Copyright of theme and framework
        ICP: # Chinese ICP License

插件化写法

修改BlogRoot/node_modules/hexo-theme-butterfly/layout/includes/footer.pug,添加页脚标签循环节:

    if theme.footer.custom_text
      .footer_custom_text!=`${theme.footer.custom_text}`
    //v3.4.0以下版本可能还有ICP的配置项。此处只要保证p和上方的if缩进平级就好。
+   p#ghbdages
+     if theme.ghbdage.enable
+       each item in theme.ghbdage.bdageitem
+         a.github-badge(target='_blank' href=url_for(item.link)  style='margin-inline:5px')
+           img(src=url_for(item.shields) title=item.massage)

在主题配置文件_config.butterfly.yml中添加相关配置项:

ghbdage:
  enable: true
  bdageitem:
    - link: https://hexo.io/  # 标签跳转链接
      shields: https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo #shields的API链接,填法参考本篇教程
      message: 博客框架为Hexo_v5.3.0 #鼠标悬停时显示的信息
    - link: https://demo.jerryc.me/
      shields: https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender
      message: 主题版本Butterfly_v3.4.2
    - link: https://metroui.org.ua/index.html
      shields: https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr
      message: 本站使用JsDelivr为静态资源提供CDN加速
    - link: https://vercel.com/
      shields: https://img.shields.io/badge/Hosted-Vervel-brightgreen?style=flat&logo=Vercel
      message: 本站采用双线部署,默认线路托管于Vercel
    - link: https://vercel.com/
      shields: https://img.shields.io/badge/Hosted-Coding-0cedbe?style=flat&logo=Codio
      message: 本站采用双线部署,联通线路托管于Coding
    - link: https://github.com/
      shields: https://img.shields.io/badge/Source-Github-d021d6?style=flat&logo=GitHub
      message: 本站项目由Gtihub托管
    - link: http://creativecommons.org/licenses/by-nc-sa/4.0/
      shields: https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris
      message: 本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可

外挂标签

考虑到对shields.io的全部参数支持,外挂标签使用object标签来引用。

新建BlogRoot/node_modules/hexo-theme-butterfly/scripts/tag/ghbdage.js

function bdage (args) {

  args = args.join(' ').split('||')

  let base= args[0]?args[0].split(','):''
  let right = base[0]?encodeURI(base[0].trim()):''
  let left = base[1]?encodeURI(base[1].trim()):''
  let logo = base[2]?base[2].trim():''

  let message = args[1]?args[1].split(','):''
  let color = message[0]?message[0].trim():'orange'
  let link = message[1]?message[1].trim():''
  let title = message[2]?message[2].trim():''

  let option = args[2]?args[2].trim():''

  return `<object title="${title}" standby="loading..." data="https://img.shields.io/badge/${left}-${right}-orange?logo=${logo}&color=${color}&link=${link}&${option}"></object>`
}
hexo.extend.tag.register('bdage',bdage);

具体用法

  • 标签语法
  • 配置参数
  • 样式预览
  • 示例源码
{% bdage [right],[left],[logo]||[color],[link],[title]||[option] %}
  1. left:徽标左边的信息,必选参数。
  2. right: 徽标右边的信息,必选参数,
  3. logo:徽标图标,图标名称详见simpleicons,可选参数。
  4. color:徽标右边的颜色,可选参数。
  5. link:指向的链接,可选参数。
  6. title:徽标的额外信息,可选参数。主要用于优化 SEO,但 object 标签不会像 a 标签一样在鼠标悬停显示 title 信息。
  7. option:自定义参数,支持shields.io的全部API参数支持,具体参数可以参看上文中的拓展写法示例。形式为 name1=value2&name2=value2

本外挂标签的参数分为三组,用||分割。

本外挂标签的参数分为三组,用||分割。

基本参数,定义徽标左右文字和图标

{% bdage Theme,Butterfly %}
{% bdage Frame,Hexo,hexo %}

信息参数,定义徽标右侧内容背景色,指向链接

{% bdage CDN,JsDelivr,jsDelivr||abcdef,https://metroui.org.ua/index.html,本站使用JsDelivr为静态资源提供CDN加速 %}
//如果是跨顺序省略可选参数,仍然需要写个逗号,用作分割
{% bdage Source,GitHub,GitHub||,https://github.com/ %}

拓展参数,支持shields的API的全部参数内容

{% bdage Hosted,Vercel,Vercel||brightgreen,https://vercel.com/,本站采用双线部署,默认线路托管于Vercel||style=social&logoWidth=20 %}
//如果是跨顺序省略可选参数组,仍然需要写双竖线||用作分割
{% bdage Hosted,Vercel,Vercel||||style=social&logoWidth=20&logoColor=violet %}

拓展内容-使用纯css实现仿徽标样式

  • html源码
  • css样式
  • 标签预览
<div class="github-badge">
  <a style="color:#fff" href="https://hexo.io/" target="_blank" title="由 Hexo 强力驱动">
    <span class="badge-subject">
      <!-- fa图标,也支持阿里图标 -->
      <i class="fa fa-copyright"></i>
      Powered
    </span>
    <span class="badge-value bg-blue">
      Hexo
    </span>
  </a>
</div>
/*标签整体样式*/
.github-badge {
  margin-left: 5px;
  display: inline-block;
  border-radius: 4px;
  text-shadow: none;
  color: #fff;
  line-height: 15px;
  background-color: #abbac3;
  margin-bottom: 5px;
  font-size: 12px;
}
/* 超链接下划线隐藏 */
.github-badge a {
  text-decoration: none;
}
/* 标签左侧样式 */
.github-badge .badge-subject {
  background-color: #4d4d4d;
  padding: 4px 4px 4px 6px;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
/* 标签右侧样式 */
.github-badge .badge-value {
  padding: 4px 6px 4px 4px;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}
/* 标签背景色,随意自定义,渐变色也可以 */
.github-badge .bg-blue {
  background-color: #007ec6;
}

.github-badge .bg-green {
  background-color: #4dc820;
}

.github-badge .bg-orange {
  background-color: orange;
}

.github-badge .bg-gradient {
  background: linear-gradient(to right, #3ca5f6, #a86af9);
}

.github-badge .bg-violet {
  background-color: #8833d7;
}