Skip to content

Commit 111337c

Browse files
committed
'改进'
1 parent 2250f2e commit 111337c

10 files changed

+67
-39
lines changed

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# hexo-history
2-
文章历史对比, 显示每次更新差异的一个 hexo 插件
2+
3+
文章历史对比, 显示每次更新差异的一个 hexo 插件, 关于如何开发的详细文章可见[]()
4+
5+
## 使用方法
6+
7+
- 本地执行测试, 更改 info-provider 即可
8+
9+
| 参数名 | 含义 |
10+
| ----------- | ---------------------------------- |
11+
| repo | 用户名+仓库名 |
12+
| path | 文件路径 |
13+
| anchorClass | 用于挂载和解析的文章节点 |
14+
| baseUrl | 使用 iframe 渲染时设定的 base 标签 |
15+
16+
- 正式使用, 设置 config, 名称为 hexoHistory, 参照下图操作即可
17+
18+
## 后续改进
19+
20+
- icon 引入 bug
21+
- diff 算法实现
22+
- hexo 只选择 post 进行处理

index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('hexo-fs');
22

3-
var config = hexo.config.history || {};
3+
var config = hexo.config.hexoHistory || {};
44
function buildGenerator(sourcePath, distPath) {
55
return {
66
data: () => fs.createReadStream(sourcePath),
@@ -19,9 +19,11 @@ hexo.extend.generator.register('hexo-history-file', () => {
1919
});
2020
//添加上script标签
2121
hexo.extend.filter.register('after_render:html', (htmlContent) => {
22-
const scriptToInject = `HexoHistory(${JSON.stringify(config)});`; //把配置注册为全局变量好了
22+
const scriptToInject = `<script>window.hexoHistoryConfig = JSON.parse(${JSON.stringify(
23+
config
24+
)});</script> `; //把配置注册为全局变量好了
2325
let vueInject = `<script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>`;
24-
const contentToInject = `${vueInject}<script src="/hexo-history-main.js"></script><script>${scriptToInject}</script>`;
26+
const contentToInject = `${vueInject} ${scriptToInject}<script src="/hexo-history-main.js"></script>`;
2527
let newHtmlContent = htmlContent;
2628
if (/([\n\r\s\t]*<\/body>)/i.test(htmlContent)) {
2729
const lastIndex = htmlContent.lastIndexOf('</body>');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hexo-history",
3-
"version": "1.1.2",
3+
"version": "1.1.4",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/luckyray-fan/hexo-history.git"

src/ajax.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import gitInfo from './git-provider';
1+
import gitInfo from './info-provider';
22
var { repo, sha, path, token } = gitInfo;
3-
if (window.hexoHistoryConfig) {
4-
({ repo, sha, token } = window.hexoHistoryConfig.gitInfo);
5-
path = location.pathname;
6-
}
73
import { Base64, dateContract } from './util/index.js';
84
//由提交中的信息获取内容
95
function getFetch(url) {

src/dombuild.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
const parser = require('./parser');
22
import { diff, diffPage } from './diff';
33
const ratio = 0.8;
4-
var anchorClass = 'article-container';
5-
if (window.hexoHistoryConfig) {
6-
let themeToClass = {
7-
melody: 'article-container',
8-
next: 'post-body'
9-
};
10-
anchorClass = themeToClass[window.hexoHistoryConfig.theme];
11-
}
4+
import infoProvider from './info-provider.js';
5+
var anchorClass = infoProvider.anchorClass;
126
const classes = {
137
add: 'add',
148
del: 'del',
@@ -121,10 +115,10 @@ class dombuild {
121115
iframe.setAttribute('id', 'hexo-history-iframe');
122116
document.body.appendChild(iframe);
123117
}
124-
if (!window.hexoHistoryConfig) {
118+
if (infoProvider.baseUrl) {
125119
item =
126120
item.slice(0, item.indexOf('<head>') + 6) +
127-
'<base href="https://luckyray-fan.github.io/"/>' +
121+
infoProvider.baseUrl +
128122
item.slice(item.indexOf('<head>') + 6);
129123
}
130124

src/git-provider.js

-10
This file was deleted.

src/index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
import app from './index.vue';
33
Vue.config.productionTip = false; //防止生成生产提示
44
import 'iview/dist/styles/iview.css';
5+
import infoProvider from './info-provider.js';
56

67
window.HexoHistory = (function() {
78
Vue.component('app', app);
8-
function start(config) {
9-
if (config) {
10-
let configJson = JSON.parse(config);
11-
window.hexoHistoryConfig = configJson;
12-
}
13-
14-
setTimeout(initPlugin, 1500); //和文档一块加载出来, TODO 加载动画
9+
function start() {
10+
document.addEventListener('DOMContentLoaded', function() {
11+
setTimeout(initPlugin, 1000); //和文档一块加载出来, TODO 加载动画
12+
});
1513
}
1614
// 将html格式字符串转化为dom的函数
1715
function htmlStrToDom(htmlstr, parentdata) {
@@ -27,7 +25,7 @@ window.HexoHistory = (function() {
2725
}
2826
function initPlugin() {
2927
var vue = new Vue({
30-
el: '#post',
28+
el: '.' + infoProvider.anchorClass,
3129
data: {
3230
dom: ''
3331
},

src/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default {
132132
});
133133
//存储
134134
saveInfo.skeleton = new skeletonTem().$mount().$el;
135-
saveInfo.origin = this.dom.children[0]; // element.children 是 live 的
135+
saveInfo.origin = this.dom; // element.children 是 live 的
136136
saveInfo.infoAlert = new infoAlert().$mount().$el;
137137
138138
this.domCompare = new dombuild();

src/info-provider.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//直接由配置给定即可, 注意, 这里仅用来测试本地的文件
2+
//对于 melody 的主题的话
3+
var Info = {
4+
repo: 'luckyray-fan/luckyray-fan.github.io', //用户名+仓库名
5+
sha: 'master',
6+
path: '2019/10/20/promise-await-yield/',
7+
token: '1d5107f3e8a6dd270b60bb70ffce79485b387e53', //提升每小时访问github api 的limit
8+
anchorClass: 'article-container',
9+
baseUrl: '<base href="https://luckyray-fan.github.io/"/>'
10+
};
11+
let themeToClass = {
12+
melody: 'article-container',
13+
next: 'post-body'
14+
};
15+
Info.path = location.pathname;
16+
if (window.hexoHistoryConfig) {
17+
({ repo: gitInfo.repo, token: gitInfo.token } = window.hexoHistoryConfig.gitInfo);
18+
if (window.hexoHistoryConfig.theme) {
19+
Info.anchorClass = themeToClass[window.hexoHistoryConfig.theme.toLowerCase()];
20+
}
21+
Info.baseUrl = false;
22+
}
23+
24+
// gitInfo.path = location.pathname;
25+
export default Info;

src/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,7 @@ console.dir(c);
8888
* - bug fix
8989
* - 请求没有按照顺序返回
9090
* - 兼容next主题
91+
* - **换主题的解决方案**
92+
* - 挂载点问题解决
93+
* - style icon bug 解决
9194
*/

0 commit comments

Comments
 (0)