背景
在美化hexo next主题的时候代码块的样式依旧很突兀,遂萌发了美化代码块的想法。说干咋就干。
 效果
自己看下面的结果吧!
 实现方法
 建立代码块js
在next/scripts/目录下新建两个js文件分别命名为events.js以及codeblock.js
代码如下:
events.js代码
1 2 3 4 5 6 7
   |  var exec = require('child_process').exec;
 
  hexo.on('new', function(data){   exec('open -a MacDown ' + data.path); });
 
  | 
 
codeblock.js代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   |  var attributes = [   'autocomplete="off"',   'autocorrect="off"',   'autocapitalize="off"',   'spellcheck="false"',   'contenteditable="true"' ]
  var attributesStr = attributes.join(' ')
  hexo.extend.filter.register('after_post_render', function (data) {   while (/<figure class="highlight ([a-zA-Z]+)">.*?<\/figure>/.test(data.content)) {     data.content = data.content.replace(/<figure class="highlight ([a-zA-Z]+)">.*?<\/figure>/, function () {       var language = RegExp.$1 || 'plain'       var lastMatch = RegExp.lastMatch       lastMatch = lastMatch.replace(/<figure class="highlight /, '<figure class="iseeu highlight /')       return '<div class="highlight-wrap"' + attributesStr + 'data-rel="' + language.toUpperCase() + '">' + lastMatch + '</div>'     })   }   return data })
 
  | 
 
 设置样式文件
到source/css/_common/components/highlight/目录下建立一个macPenal.styl文件
macPenal.styl文件内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
   | // mac Panel效果代码块相关 .highlight-wrap[data-rel] {   position: relative;   overflow: hidden;   border-radius: 5px;   //box-shadow: 0 10px 30px 0px rgba(0, 0, 0, 0.4);   box-shadow:18px 18px 15px 0px rgba(0,0,0,.4)   margin: 35px 0;   ::-webkit-scrollbar {     height: 10px;   }
    ::-webkit-scrollbar-track {     -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);     border-radius: 10px;   }
    ::-webkit-scrollbar-thumb {     border-radius: 10px;     -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);   }   &::before {     color: white;     content: attr(data-rel);     height: 38px;     line-height: 38px;     //background: #21252b;     background: #108414de;     color: #fff;     font-size: 16px;     position: absolute;     top: 0;     left: 0;     width: 100%;     //font-family: 'Source Sans Pro', sans-serif;     font-weight: bold;     padding: 0px 80px;     text-indent: 15px;     float: left;   }   &::after {     content: ' ';     position: absolute;     -webkit-border-radius: 50%;     border-radius: 50%;     background: #fc625d;     width: 12px;     height: 12px;     top: 0;     left: 20px;     margin-top: 13px;     -webkit-box-shadow: 20px 0px #fdbc40, 40px 0px #35cd4b;     box-shadow: 20px 0px #fdbc40, 40px 0px #35cd4b;     z-index: 3;   } }
   | 
 
再到highlight.styl文件中引入新建的macPenal.styl
1 2 3
   |   @require "theme"   @require "diff" + @require "macPanel"
   | 
 
到此,不出意外,代码块样式应该已经出来了。
如果样式出来了,但代码未显示出来,那说明代码是被遮盖了,调一下样式就能出来了。
 样式调试
经过上述一系列操作,macPenal效果成功出现但样式有些不对(不好看)。
修改/nextlayout/_third-party/copy-code.swig
内容为
1 2 3 4 5 6 7 8 9
   | .highlight-wrap .copy-btn {   transition: opacity .3s ease-in-out;   opacity: 0;   padding: 2px 6px;   position: absolute;   right: 5px; - top: 8px; + top: -31px; }
  | 
 
修改next/source/css/_common/component/highlight下的highlight.styl内容为
1 2 3 4 5 6 7 8 9 10
   | $code-block {   overflow: auto; + margin: 36px 0px 0px; - margin: 36px 0px;   padding: 0;   font-size $code-font-size;   color: $highlight-foreground;   background: $highlight-background;   line-height: $line-height-code-block; }
  | 
 
最后到主题的config.yml文件下修改highlight_theme为normal
highlight_theme: normal
本主题为:hexo next 6.4.2 mist 已换为Matery