下面将介绍对Hugo主题进行简单的定制,所选的主题是我所使用tranquilpeak,若你使用的是其他主题也可以参考,应该大同小异。

目录

基本配置

baseURL = "https://JamesHZQ.github.io/"
languageCode = "en-us"
defaultContentLanguage = "zh-cn"
title = "James He::blog"
theme = "hugo-tranquilpeak-theme"
# disqusShortname = "hugo-tranquilpeak-theme"
# googleAnalytics = "UA-123-45"
paginate = 7          //每页显示文章数
canonifyurls = true

侧边栏配置

[author]
  name = "James He"
  # '<br />'用户换行
  bio = "不忘初心<br />hezhiqiang.a@gmail.com"
  job = "程序猿"
  location = "广州"
  # Your Gravatar email. Overwrite `author.picture` everywhere in the blog
  # 在Gravatar官网设置你的头像,并将注册的邮箱填在下面
  gravatarEmail = "hezhiqiang.a@gmail.com"
  # Your profile picture
  # Overwritten by your gravatar image if `author.gravatarEmail` is filled
  picture = "https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png"
  # Your Twitter username without the @. E.g : thibaudlepretre
  # twitter = "thibaudlepretre"
  # Your google plus profile id. E.g : +ThibaudLepretre or 114625208755123718311
  # googlePlus = "+ThibaudLepretre"

设置自己的github、stackoverflow

[[menu.links]]
  weight = 1
  identifier = "github"
  name = "GitHub"
  pre = "<i class=\"sidebar-button-icon fa fa-lg fa-github\"></i>"
  url = "https://github.com/JamesHZQ"
[[menu.links]]
  weight = 2
  identifier = "stackoverflow"
  name = "Stack Overflow"
  pre = "<i class=\"sidebar-button-icon fa fa-lg fa-stack-overflow\"></i>"
  url = "https://stackoverflow.com/users/11174813/james-he"

可以去这里找到自己想要的图标

生成markdown目录

  [markup]
    [markup.goldmark]
      [markup.goldmark.renderer]
        unsafe = true
  # 可以在目录中使用特殊字符    
  preserveTaxonomyNames = true

较新版本的Hugo使用tranquilpeak主题,如果不添加上面的配置无法通过<!--toc-->快速生成目录,参考这里这里。另外要注意一级标题并不会出现在目录里。

配置代码高亮主题

先去这里找到自己喜欢的代码高亮主题,然后去这里找到相应的css代码,然后修改themes\hugo-tranquilpeak-theme\src\scss\themes\ _hljs-custom.scss,按照文件中的提示进行相应的配置,可以参考下面的的配置,我所选用的代码高亮的主题的night owl。

// Highlight.js custom theme
// Follow guidelines
// 1. Uncomment the block just below - remove `/*` and `*/`
// 2. Fill `background`, `color`, `border-right-color` properties
// with properties of `.hljs` CSS class of your theme
// 3. Put your theme where it's indicated just below
// 4. Remove `.hljs {...}` rules
// 5. Remove `hljs-` of all CSS class name. E.g : `.hljs-comment` becomes `.comment`
// 6. Comment line 61 (`'themes/tranquilpeak',`) of `source/_css/tranquilpeak.scss` file
// 7. Run `grunt buildProd` to build the theme and see the result

// Default code block
pre > code {
    background: #011627;
    color: map-get($highlight-colors, night-rider);
}
pre > code.codeblock {
    overflow-x: auto;
}

// Default inline code
code {
    //行内代码背景色
    background-color: map-get($highlight, 'background');
}

// Tabbed code block tab
// If your theme is dark then use a lighter background
// to see clearly which tab is active
.codeblock--tabbed figcaption .tab.active {
    background: map-get($colors, 'primary');
    color: white;
}

// highlight code block
figure.highlight,

.codeblock {
    background: #011627;
    color: #d6deeb;

    figcaption {
        background: #011627;
        color: #999999;
    }

    .gutter {
        background: #011627;
        border-right-color: #e6e6e6;

        .line {
            color: #aaaaaa;
        }
    }
    // PUT YOUR THEME HERE
    /*color: fg_yellow*/
    .keyword {
        color: #c792ea;
        font-style: italic;
    }

    .built_in {
        color: #addb67;
        font-style: italic;
    }

    .type {
        color: #82aaff;
    }

    .literal {
        color: #ff5874;
    }

    .number {
        color: #F78C6C;
    }

    .regexp {
        color: #5ca7e4;
    }

    .string {
        color: #ecc48d;
    }

    .subst {
        color: #d3423e;
    }

    .symbol {
        color: #82aaff;
    }

    .class {
        color: #ffcb8b;
    }

    .function {
        color: #82AAFF;
    }

    .title {
        color: #DCDCAA;
        font-style: italic;
    }

    .params {
        color: #7fdbca;
    }
    /* Meta */
    .comment {
        color: #637777;
        font-style: italic;
    }

    .doctag {
        color: #7fdbca;
    }

    .meta {
        color: #82aaff;
    }

    .meta-keyword {
        color: #82aaff;
    }

    .meta-string {
        color: #ecc48d;
    }
    /* Tags, attributes, config */
    .section {
        color: #82b1ff;
    }

    .tag,

    .name,

    .builtin-name {
        color: #7fdbca;
    }

    .attr {
        color: #7fdbca;
    }

    .attribute {
        color: #80cbc4;
    }

    .variable {
        color: #addb67;
    }
    /* Markup */
    .bullet {
        color: #d9f5dd;
    }

    .code {
        color: #80CBC4;
    }

    .emphasis {
        color: #c792ea;
        font-style: italic;
    }

    .strong {
        color: #addb67;
        font-weight: bold;
    }

    .formula {
        color: #c792ea;
    }

    .link {
        color: #ff869a;
    }

    .quote {
        color: #697098;
        font-style: italic;
    }
    /* CSS */
    .selector-tag {
        color: #ff6363;
    }

    .selector-id {
        color: #fad430;
    }

    .selector-class {
        color: #addb67;
        font-style: italic;
    }

    .selector-attr,

    .selector-pseudo {
        color: #c792ea;
        font-style: italic;
    }
    /* Templates */
    .template-tag {
        color: #c792ea;
    }

    .template-variable {
        color: #addb67;
    }
    /* diff */
    .addition {
        color: #addb67ff;
        font-style: italic;
    }

    .deletion {
        color: #EF535090;
        font-style: italic;
    }
}

记得去修改src\scss\tranquilpeak.scss第66行,把 themes/hljs-tranquilpeak注释掉

然后在themes\hugo-tranquilpeak-theme\src\scss\themes\目录下执行下列编译命令

npm install
npm install grunt-cli -g
grunt buildProd
//如果出错执行下面的命令
npm run start
grunt buildProd

主要参考

https://3mile.github.io/

https://studygolang.com/articles/20975

https://www.jianshu.com/p/4669fb3bf35a

https://gohugo.io/getting-started/