Hexo博客技巧:添加GitHub徽标

GitHub 徽标

1.GitHub 徽标可以直接通过 shields.io 在线生成。

2.理论上可以放在页面的任何地方。教程案例是添加在页脚。

3.工具网站包括:

具体步骤

  1. 通过 shields.io 在线生成。

    • label: 标签,徽标左侧内容
    • message: 信息,徽标右侧内容
    • color: 色值,支持支持十六进制、rgb、rgba、hsl、hsla 和 css 命名颜色。十六进制记得删除前面的 #号
  2. 输入相关信息后,点击 make badge 即可得到徽标的 URL。可以用 img 标签引用,写法简单。不过正式写法建议用 object 标签引用,写法示例如下。

    HTML
    <!-- 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=Hexo
logo自定义图标, 限制较多,不推荐?logo=data:image/png;base64,url
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>

样式示例

在这里插入图片描述

  1. [Blogroot]\_config.butterfly.ymlfooter 配置项中添加徽标,注意事先压缩一下,使他们只留一行。为了不至于太过紧凑,设置一下行内间隔属性 margin-inline

    DIFF
          footer:
            owner:
              enable: true
              since: 2016
    -       custom_text:
    +       custom_text: <p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a><a style="margin-inline:5px" target="_blank" href="https://vercel.com/ "><img src="https://img.shields.io/badge/Hosted-Vercel-brightgreen?style=flat&logo=Vercel" title="本站采用双线部署,默认线路托管于Vercel"></a><a style="margin-inline:5px" target="_blank" href="https://vercel.com/ "><img src="https://img.shields.io/badge/Hosted-Coding-0cedbe?style=flat&logo=Codio" title="本站采用双线部署,联通线路托管于Coding"></a><a style="margin-inline:5px" target="_blank" href="https://github.com/"><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&logo=GitHub" title="本站项目由Gtihub托管"></a><a style="margin-inline:5px" 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
    

插件化写法

  1. 修改[Blogroot]\themes\butterfly\layout\includes\footer.pug,添加页脚标签循环节:

    DIFF
        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)
    
  2. [Blogroot]\_config.butterfly.yml中添加相关配置项:

    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://butterfly.js.org/
          shields: https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender
          message: 主题版本Butterfly_v3.4.2
        - link: https://www.jsdelivr.com/
          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-Vercel-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 标签来引用。

  1. 新建[Blogroot]\themes\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] %}

配置参数

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

样式预览

在这里插入图片描述

示例源码

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

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

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

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

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

3.拓展参数,支持 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 实现仿徽标样式

这一段参考自 paddylin-Butterfly 脚底自定义标签

在使用徽标时,偶尔会遇到需要在内部写入动态内容的情况,这时候再使用 shields.io 提供的标签就无法满足我们的需求了。所以就要使用 HTML 语言配合 CSS 来仿写标签。

html源码

<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>

css样式

/*标签整体样式*/
.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;
}

结语

本文来自Akilar:Add Github Badge

End


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值