From 9ab839d95c3f2c0b651f5cedc922a21d8b4e6d10 Mon Sep 17 00:00:00 2001 From: 7zMonkey <7zmonkey@aliyun.com> Date: Sun, 16 Apr 2023 18:45:00 +0800 Subject: [PATCH] v 1.0.0 --- README.md | 13 ++++++++++++- index.js | 17 ++++++++++++++++- lib/index.js | 22 ++++------------------ lib/utils/options.js | 22 +++++++++++----------- package.json | 21 ++++++++++++++++----- 5 files changed, 59 insertions(+), 36 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 index.js mode change 100644 => 100755 lib/index.js mode change 100644 => 100755 lib/utils/options.js mode change 100644 => 100755 package.json diff --git a/README.md b/README.md old mode 100644 new mode 100755 index f4250c7..d5723dc --- a/README.md +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 1cb6f2d..33588b5 --- a/index.js +++ b/index.js @@ -1 +1,16 @@ -hexo.extend.filter.register('after_post_render', require('./lib/index')); \ No newline at end of file +(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); +})() \ No newline at end of file diff --git a/lib/index.js b/lib/index.js old mode 100644 new mode 100755 index b60ea1d..40425e2 --- a/lib/index.js +++ b/lib/index.js @@ -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; @@ -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; @@ -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 = `${match}`; return path ? link : match; }); diff --git a/lib/utils/options.js b/lib/utils/options.js old mode 100644 new mode 100755 index 81d7acc..d6414ff --- a/lib/utils/options.js +++ b/lib/utils/options.js @@ -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 } } diff --git a/package.json b/package.json old mode 100644 new mode 100755 index 8fe1635..fedb709 --- a/package.json +++ b/package.json @@ -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" }