Skip to content

Commit

Permalink
feat: hash path for comments & view count
Browse files Browse the repository at this point in the history
  • Loading branch information
Candinya committed Aug 12, 2024
1 parent 44f1a2f commit 804bf88
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ comments:
_shared: ""
index: ""
post: ""
path_hasher: "md5"

# 访问数量统计
visit_count:
Expand All @@ -199,6 +200,7 @@ visit_count:
_shared: ""
index: ""
post: ""
path_hasher: "md5"

################## 额外的注入代码 ##################
additional_injections:
Expand Down
4 changes: 2 additions & 2 deletions layout/_index_style/card.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@
<% if (theme.visit_count.enable_at.includes("index")) { %>
<a>
<i class="fa fa-eye"></i>
<%- __('post_meta.read_count', (theme.visit_count.template._shared + theme.visit_count.template.index).replaceAll("$PATH", url_for(post.path))) %>
<%- __('post_meta.read_count', process_template_path(theme.visit_count.template._shared + theme.visit_count.template.index, url_for(post.path), "visit_count")) %>
</a>
<% } %>
<% if (theme.comments.count.enable_at.includes("index") && post.comments) { %>
<a>
<i class="fa fa-commenting-o"></i>
<%- __('post_meta.comments_count', (theme.comments.count.template._shared + theme.comments.count.template.index).replaceAll("$PATH", url_for(post.path))) %>
<%- __('post_meta.comments_count', process_template_path(theme.comments.count.template._shared + theme.comments.count.template.index, url_for(post.path), "comments")) %>
</a>
<% } %>
</span>
Expand Down
4 changes: 2 additions & 2 deletions layout/_index_style/half.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
<% if (theme.visit_count.enable_at.includes("index")) { %>
<li>
<i class="fa fa-eye"></i>
<%- __('post_meta.read_count', (theme.visit_count.template._shared + theme.visit_count.template.index).replaceAll("$PATH", url_for(post.path))) %>
<%- __('post_meta.read_count', process_template_path(theme.visit_count.template._shared + theme.visit_count.template.index, url_for(post.path), "visit_count")) %>
</li>
<% } %>
<% if (theme.comments.count.enable_at.includes("index") && post.comments) { %>
<li>
<i class="fa fa-commenting-o"></i>
<%- __('post_meta.comments_count', (theme.comments.count.template._shared + theme.comments.count.template.index).replaceAll("$PATH", url_for(post.path))) %>
<%- __('post_meta.comments_count', process_template_path(theme.comments.count.template._shared + theme.comments.count.template.index, url_for(post.path), "comments")) %>
</li>
<% } %>
</ul>
Expand Down
4 changes: 2 additions & 2 deletions layout/_index_style/status.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
<% if (theme.visit_count.enable_at.includes("index")) { %>
<a>
<i class="fa fa-eye"></i>
<%- __('post_meta.read_count', (theme.visit_count.template._shared + theme.visit_count.template.index).replaceAll("$PATH", url_for(post.path))) %>
<%- __('post_meta.read_count', process_template_path(theme.visit_count.template._shared + theme.visit_count.template.index, url_for(post.path), "visit_count")) %>
</a>
<% } %>
<% if (theme.comments.count.enable_at.includes("index") && post.comments) { %>
<a>
<i class="fa fa-commenting-o"></i>
<%- __('post_meta.comments_count', (theme.comments.count.template._shared + theme.comments.count.template.index).replaceAll("$PATH", url_for(post.path))) %>
<%- __('post_meta.comments_count', process_template_path(theme.comments.count.template._shared + theme.comments.count.template.index, url_for(post.path), "comments")) %>
</a>
<% } %>
</span>
Expand Down
2 changes: 1 addition & 1 deletion layout/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

<% if (theme.comments.core.enable_at.includes('index')) { %>
<div class="comment-index">
<%- (theme.comments.core.template._shared + theme.comments.core.template.index).replaceAll("$PATH", url_for(page.path)) %>
<%- process_template_path(theme.comments.core.template._shared + theme.comments.core.template.index, url_for(page.path), "comments") %>
</div>
<% } %>
2 changes: 1 addition & 1 deletion layout/page.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</div>
<% if (page.comments && theme.comments.core.enable_at.includes('page')) { %>
<div class="comment-page">
<%- (theme.comments.core.template._shared + theme.comments.core.template.page).replaceAll("$PATH", url_for(page.path)) %>
<%- process_template_path(theme.comments.core.template._shared + theme.comments.core.template.page, url_for(page.path), "comments") %>
</div>
<% } %>
</article>
6 changes: 3 additions & 3 deletions layout/post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
<% if (theme.visit_count.enable_at.includes("post")) { %>
<li>
<i class="fa fa-eye"></i>
<%- __('post_meta.read_count', (theme.visit_count.template._shared + theme.visit_count.template.post).replaceAll("$PATH", url_for(page.path))) %>
<%- __('post_meta.read_count', process_template_path(theme.visit_count.template._shared + theme.visit_count.template.post, url_for(page.path), "visit_count")) %>
</li>
<% } %>
<% if (page.comments && theme.comments.count.enable_at.includes("post")) { %>
<li>
<i class="fa fa-commenting-o"></i>
<%- __('post_meta.comments_count', (theme.comments.count.template._shared + theme.comments.count.template.post).replaceAll("$PATH", url_for(page.path))) %>
<%- __('post_meta.comments_count', process_template_path(theme.comments.count.template._shared + theme.comments.count.template.post, url_for(page.path), "comments")) %>
</li>
<% } %>
</ul>
Expand Down Expand Up @@ -100,7 +100,7 @@
<% } %>
<% if (page.comments && theme.comments.core.enable_at.includes('post')) { %>
<div class="comment-post">
<%- (theme.comments.core.template._shared + theme.comments.core.template.post).replaceAll("$PATH", url_for(page.path)) %>
<%- process_template_path(theme.comments.core.template._shared + theme.comments.core.template.post, url_for(page.path), "comments") %>
</div>
<% } %>
</article>
10 changes: 10 additions & 0 deletions scripts/process-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { createHash } = require("crypto");

hexo.extend.helper.register("process_template_path", (template, path, type) => {
const hasher = hexo.theme.config[type].path_hasher;
const hash = createHash(hasher);
hash.update(path);
const digest = hash.digest("hex");

return template.replaceAll("$PATH_HASH", digest).replaceAll("$PATH", path);
});

0 comments on commit 804bf88

Please sign in to comment.