Skip to content

Commit

Permalink
v 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
7zMonkey committed Apr 16, 2023
1 parent b353368 commit 9ab839d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
13 changes: 12 additions & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
| self | 是否关联自己 | false |Boolean: true \| false|
| mode | 关键模式 | "all" |String: single \| all|
| class_name | class名称 | "auto-association" | String |
| log | 显示日志 | true | Boolean: true \| false |
| is_clear_cache | 清除缓存 | true | Boolean: true \| false |

默认的配置项内容如下:
```yml
auto_associations:
enable: true
key: association
url: /
priority: 10
self: false
mode: all
class_name: auto-association
is_clear_cache: true
```
17 changes: 16 additions & 1 deletion index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
hexo.extend.filter.register('after_post_render', require('./lib/index'));
(function () {
const setOptions = require('./lib/utils/options')

hexo.config.auto_associations = setOptions(hexo.config.auto_associations);
let { auto_associations } = hexo.config;

const { enable, is_clear_cache, priority } = auto_associations;


if (!enable) {
console.log("[hexo-auto-association]: Not enabled, skip.")
return
}

hexo.extend.filter.register('after_post_render', require('./lib/index'), priority);
})()
22 changes: 4 additions & 18 deletions lib/index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
// 载入配置文件
const initFunc = require('./utils/options')
module.exports = function (data) {
const { config, locals } = this;
// 如果不存在配置信息则创建配置信息
if (!config.auto_associations) {
console.log("[hexo-auto-association]: Installing...")
initFunc.call(this);
}
const { enable, is_clear_cache } = config.auto_associations;
if (!enable) {
console.log("[hexo-auto-association]: Not enabled, skip.")
return
}
// 清除缓存
const { is_clear_cache } = config.auto_associations;

if (is_clear_cache) {
console.log("[hexo-auto-association]: Clearing cache...")
locals.invalidate();
}

const posts = locals.get('posts').data;
data.content = replaceHtmlText(data, posts, config.auto_associations);
return data;
Expand All @@ -25,8 +15,7 @@ module.exports = function (data) {
function replaceHtmlText(data, posts, config) {
const { key, url, self, mode, class_name, log } = config;
const associations = getAssociations(posts, key);
if (associations.length !== 0) console.log("[hexo-auto-association]: Associations: ", associations.join())
else console.log("[hexo-auto-association]: No associations, skip.")
if (associations.length === 0) console.log("[hexo-auto-association]: No associations, skip.")
const regex = new RegExp(`(${associations.join('|')})`, 'g');
const htmlString = data.content;
const title = data.title;
Expand All @@ -38,9 +27,6 @@ function replaceHtmlText(data, posts, config) {
const path = findPath(posts, key, match);
if (!path) return match;
existedOnce.add(match);
if (log) {
console.log(`[hexo-auto-association]:${title} - ${match}`);
}
const link = `<a class="${class_name}" href="${url}${path}">${match}</a>`;
return path ? link : match;
});
Expand Down
22 changes: 11 additions & 11 deletions lib/utils/options.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ association 关联词 通常是 string 或者 []
`
// 导出模块使用comm
module.exports = function initFunc() {
this.config.auto_associations = {
enable: true,
key: "association",
url: '/',
priority: 10,
self: false,
mode: "all", // single | all
class_name: "auto-association",
log: true, // true | false
is_clear_cache: true // true | false
module.exports = function initFunc(config) {
return {
enable: config.enable ?? true,
key: config.key ?? "association",
url: config.url ?? '/',
priority: config.priority ?? 10,
self: config.self ?? false,
mode: config.mode ?? "all", // single | all
class_name: config.class_name ?? "auto-association",
log: config.log ?? true, // true | false
is_clear_cache: config.is_clear_cache ?? true // true | false
}
}
21 changes: 16 additions & 5 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
{
"name": "hexo-auto-association",
"version": "0.0.1",
"description": "",
"version": "1.0.0",
"description": "A tool for internally associating the static pages generated by hexo. In the after_post_render stage, it will judge the specified content under the front-matter of the article (the default is the association option) for association.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
"homepage": "https://github.com/7zMonkey/hexo-auto-association",
"bugs": {
"url": "https://github.com/7zMonkey/hexo-auto-association/issues",
},
"keywords": ["hexo", "plugin", "auto", "association"],
"author": {
"name": "7zMonkey",
"url" : "https://www.7zmonkey.tech/"
},
"repository": {
"type": "git",
"url": "https://github.com/7zMonkey/hexo-auto-association"
},
"license": "MIT"
}

0 comments on commit 9ab839d

Please sign in to comment.