Loading...
Loading...
Provides Hexo blog theme development guidance covering templates, variables, helpers, and i18n. Use when creating or modifying Hexo themes, working with EJS/Nunjucks templates, using Hexo variables or helper functions, or developing Hexo plugins and extensions.
npx skill4agent add hsiangfeng/hexo-skills hexo-theme-development_config.yml# 網站設定
title: My Blog
author: Author Name
language: zh-tw
# URL 設定
url: https://example.com
permalink: :year/:month/:day/:title/
# 目錄設定
source_dir: source
public_dir: public
# 主題設定
theme: my-themehexo new post "標題" # 新建文章
hexo new page "about" # 新建頁面
hexo generate # 產生靜態檔案
hexo server # 啟動本地伺服器
hexo clean # 清除快取---
title: 文章標題
date: 2024-01-15
tags: [Tag1, Tag2]
categories: [Category]
---source/_data/site.data# source/_data/menu.yml
home: /
archives: /archives.
├── _config.yml # 主題配置檔
├── languages/ # 國際化語言檔案
├── layout/ # 模板檔案
├── scripts/ # JavaScript 擴展腳本
└── source/ # 靜態資源 (CSS, JS, 圖片)| 目錄/檔案 | 說明 |
|---|---|
| 主題配置,修改後自動生效無需重啟 |
| 存放 YAML/JSON 語言檔案 |
| 模板檔案,支援 EJS、Nunjucks、Pug |
| 初始化時自動載入的 JS 腳本 |
| 靜態資源, |
| 模板 | 說明 | 備援 |
|---|---|---|
| 首頁(必需) | - |
| 文章頁面 | |
| 獨立分頁 | |
| 歸檔頁面 | |
| 分類歸檔 | |
| 標籤歸檔 | |
body<!-- layout.ejs -->
<!DOCTYPE html>
<html>
<head><title><%= page.title %></title></head>
<body><%- body %></body>
</html>---
layout: false # 禁用佈局
---partial()<%- partial('partial/header') %>
<%- partial('partial/sidebar', {active: 'home'}) %><%- fragment_cache('header', function(){ return '<header>...</header>'; }) %>
<%- partial('partial/header', {}, {cache: true}) %>| 變數 | 說明 |
|---|---|
| 網站資訊 |
| 當前頁面資訊 |
| 網站配置 |
| 主題配置 |
| 當前頁面路徑 |
| 當前頁面完整網址 |
url_for()relative_url()full_url_for()css()js()link_to()image_tag()is_home()is_post()is_archive()trim()strip_html()truncate()date()time()relative_date()list_categories()list_tags()paginator()_config.ymllanguage: zh-tw
# 或多語言
language:
- zh-tw
- enlanguages/# languages/zh-tw.yml
Home: 首頁
Archive: 歸檔
Search: 搜尋<%= __('Home') %>
<%= _p('posts', 5) %> <!-- 複數形式 -->| API | 說明 | 用途 |
|---|---|---|
| Filter | 過濾器 | 修改處理中的資料 |
| Helper | 輔助函數 | 在模板中插入程式碼片段 |
| Generator | 產生器 | 根據檔案建立路由 |
| Tag | 標籤 | 在文章中插入自訂內容 |
| Renderer | 渲染器 | 轉換內容格式 |
| Injector | 注入器 | 在 HTML 特定位置插入程式碼 |
| Console | 控制台 | 建立自訂指令 |
// scripts/my-plugin.js
// 自訂輔助函數
hexo.extend.helper.register('greeting', function(name) {
return `Hello, ${name}!`;
});
// 文章渲染後處理
hexo.extend.filter.register('after_post_render', function(data) {
// 修改文章內容
return data;
});
// 自訂標籤
hexo.extend.tag.register('note', function(args, content) {
return `<div class="note">${content}</div>`;
}, { ends: true });| 類型 | 工具 | 用途 |
|---|---|---|
| 單元測試 | Mocha + Chai | 測試輔助函數、標籤 |
| 配置驗證 | js-yaml | 驗證 YAML 格式 |
| JS Linting | ESLint | JavaScript 品質 |
| CSS Linting | Stylelint | 樣式表品質 |
| 功能測試 | hexo-theme-unit-test | 主題完整性 |
| CI/CD | GitHub Actions | 自動化測試 |
{
"scripts": {
"test": "mocha test/index.js",
"lint": "eslint scripts/ source/js/"
},
"devDependencies": {
"chai": "^5.0.0",
"eslint": "^9.0.0",
"mocha": "^10.0.0"
}
}// test/helpers/my-helper.js
const Hexo = require('hexo');
describe('my-helper', () => {
const hexo = new Hexo(__dirname, { silent: true });
before(() => hexo.init());
const helper = hexo.extend.helper.get('my_helper').bind(hexo);
it('should return formatted text', () => {
helper('hello').should.equal('<span>hello</span>');
});
});themes.yml