NEW 我們Astro v5.1的新blog上線啦 »

· Joseph · Marketing  · 5 min read

Upgrade hexo and hexo-theme-materialize

Upgrade hexo and hexo-theme-materialize

終於在年節時間把這個部落格升級到Hexo 6.2了!2019年建立這個部落格那時候還是用Hexo 4,前年曾經升級到Hexo 5,並且嘗試把hexo-theme-materialize一起升級到v4,不料這次theme升級幅度有點大,hexo-theme-materialize v4他們把Stylus都給改成scss,還改成了webpack,升級沒這麼順利,所以當時就只有先把Hexo從4升級到5.2。這次九天連假,前兩天就把動畫追完,後面只好把之前欠的補一補了。

事前準備

首先我們來看看Hexo版本之間的差異

Hexo versionMinimum (Node.js version)Less than (Node.js version)
6.2+12.13.0latest
6.0+12.13.018.5.0
5.0+10.13.012.0.0
4.1 - 4.28.1010.0.0
4.08.68.10.0

升級到6.2至少要NodeJs 12.13,還好都是跑在docker裡面,這邊只要改寫Dockerfile即可

FROM node:18-alpine

COPY hexo /blog
WORKDIR /blog

RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories

RUN apk add --update --no-cache git
RUN npm config set unsafe-perm true \
        && npm install hexo-cli -g \
  && npm install

EXPOSE 4000

ENTRYPOINT ["hexo", "server"]

開始升級!

基礎架設搞定以後呢,就可以開始把所有的package.json都更新上去:

{
  "name": "hexo-site",
  "version": "2.0.0",
  "private": true,
  "hexo": {
    "version": "6.3.0"
  },
  "dependencies": {
    "hexo": "^6.2.0",
    "hexo-asset-link": "^2.2.1",
    "hexo-deployer-git": "^3.0.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-seo-friendly-sitemap": "^0.2.1",
    "hexo-generator-tag": "^2.0.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-markdown-it": "^6.1.0",
    "hexo-renderer-marked": "^6.0.0",
    "hexo-renderer-stylus": "^2.1.0",
    "hexo-server": "^3.0.0",
    "hexo-tag-cloud": "^2.1.2",
    "markdown-it-abbr": "^1.0.4",
    "markdown-it-checkbox": "^1.1.0",
    "markdown-it-container": "^3.0.0",
    "markdown-it-deflist": "^2.1.0",
    "markdown-it-emoji": "^2.0.2",
    "markdown-it-footnote": "^3.0.3",
    "markdown-it-imsize": "^2.0.1",
    "markdown-it-ins": "^3.0.1",
    "markdown-it-mark": "^3.0.1",
    "markdown-it-regexp": "^0.4.0",
    "markdown-it-sub": "^1.0.0",
    "markdown-it-sup": "^1.0.0",
    "markdown-it-task-checkbox": "^1.0.6"
  }
}

我們比對一下可以發現都是滿大版號的跳動: package.json

然後 _config.yml裡有個針對external_link的修正,原先他是吃true|false,現在要改成下面這樣

external_link:
  enable: true

除此之外,這次我還安裝了hexo-renderer-marked,讓他去處理post assets路徑的問題,更詳細的討論可以看這裡

Once enabled, an asset image will be automatically resolved to its corresponding post’s path. For example, “image.jpg” is located at “/2020/01/02/foo/image.jpg”, meaning it is an asset image of “/2020/01/02/foo/“ post, ![](image.jpg) will be rendered as <img src="/2020/01/02/foo/image.jpg">.

為此,對於_config.yml要增加

post_asset_folder: true #這可能之前有,有的話就去修改它 
marked:
  prependRoot: true
  postAsset: true

到此為止,Hexo 6.2算是升級完成了。緊接著是對於theme的升級步驟。

Theme: hexo-theme-materialize

大部分人都用 NexT theme,但我跟他還是沒那麼對眼,所以我一直都是用hexo-theme-materialize,我們就直接clone他到theme folder裡。

git clone https://github.com/carlos-algms/hexo-theme-materialize themes/materialize

但我的部落格有sidebar跟categories,這些都要把他們加進去,就先來處理categories吧。先在themes/materialize/layout底下,增加一個categories.ejs

Add categories

themes/materialize/layout/categories.ejs

<div class="container">
  <div class="row">
    <div class="col s12 m9">
      <header class="article-header">
      <h1 class="article-title" itemprop="name">
        <%= page.title %>
      </h1>
      </header>

      <% if (site.categories.length){ %>
      <div class="category-all-page article-type-post show">
        <%- list_categories(site.categories, {
          show_count: true,
          class: 'category-list-item',
          style: 'list',
          depth: 2,
          separator: ''
        }) %>
      </div>
      <% } %>
      <% if ( is_tag() ) { %>
        <h2 class="header color-featured">Tag: <%- page.tag %></h2>
      <% } %>
    </div>
    <div class="col s12 m3">
      <% if (theme.sidebar && theme.sidebar !== 'bottom'){ %>
        <%- partial('_partial/sidebar', null, {cache: !config.relative_link}) %>
      <% } %>
    </div>
  </div>
</div>

然後還缺少CSS:

themes/materialize/src/styles/_partial/categories.scss

.category-all-page {
  position: relative;

  h2 {
    margin: 20px 0;
  }
  .category-all-title {
    text-align: center;
  }
  .category-all {
    margin-top: 20px;
  }
  .category-list {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .category-list-item-list-item {
    margin: 10px 15px;
  }
  .category-list-item-list-count {
    color: $color-grey;
    &:before {
      display: inline;
      content: " (";
      }
    &:after {
      display: inline;
      content: ") ";
    }
  }
  .category-list-item {
    margin: 10px 10px;
  }
  .category-list-count {
    color: $color-grey;
    &:before {
      display: inline;
      content: " ("
    }
    &:after {
      display: inline;
      content: ") "
    }
  }
  
  .category-list-child {
    padding-left: 10px;
  }
}

最後把這個partial加回去 themes/materialize/src/styles/style.css就好了

// add it
@import '_partial/categories';
Add sidebar

然後我的sidebar呢?在每個想要有sidebar的layout裡,找到正確位置新增:

<div class="col s12 m3">
  <% if (theme.sidebar && theme.sidebar !== 'bottom'){ %>
    <%- partial('_partial/sidebar', null, {cache: !config.relative_link}) %>
  <% } %>
</div>

然後將sidebar.ejs partial layout加回來, themes/materialize/layout/_partial/sidebar.ejs

<aside id="sidebar"<% if (theme.sidebar === 'bottom'){ %> class="outer"<% } %>>
  <% theme.widgets.forEach(function(widget){ %>
    <%- partial('_widget/' + widget) %>
  <% }) %>
</aside>

剩下這些partial widget,可以參考其他theme的設計,這邊檔案太多就不每個都show出來了。 widget

到此終於升級完整個hexo + theme,用了一個比較冷門的theme還真的是一個挑戰。 不過升級完的另一個挑戰,是年假還有好多天,要開始來補文章了…。

Related Posts

View All Posts »
Google Ads搜尋關鍵字廣告設定

Google Ads搜尋關鍵字廣告設定

Google Ads搜尋關鍵字廣告設定 當我們有需要被解決的需求,像牙齒痛,想知道哪裡有診所可以治療? 要買電視,想知道電視的商品資訊及評價?多數人會使用Google搜尋引擎搜尋找解答,用戶搜尋意圖明確,因此在Google搜尋引擎可以接觸到精準的潛在顧客,已是電商品牌行銷布局必要的媒體工具之一。 想投放Google搜…

Google Ads account setting

Google Ads account setting

數位科技蓬勃時代,多數人大部分時間都在使用手機及社群媒體分享生活圈維持情感連繫,品牌電商想要快速累積知名度及訂單,在社群或搜尋引擎投放廣告是最快達成成效的行銷方式,那要選擇哪個廣告平台呢?如果你的廣告預算有限,可以先從受眾精準的**Google ads搜尋廣告**開始 !

設定Google Analytics 4 / Google Tag Manager追蹤user id

設定Google Analytics 4 / Google Tag Manager追蹤user id

這篇文章我決定用中文寫,主要原因是中文的資料太少,而且也很多不是Google Analytics 4 + Google Tag Manager (GA4+GTM) 的設定,所以我覺得用中文記錄一下,好讓大家要搜尋的可以搜得到。 在以前GA追蹤埋追蹤碼時,我們要紀錄的經過規劃後會視情況放在, , , 或複雜一點的cus…