Skip to content

Commit

Permalink
code: use location.hash for archive page
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Lord committed Nov 9, 2023
1 parent 3d91eba commit 6f59e45
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@

一切博文相关的 commit 名必须满足以下规范:

```
```plain
修改形式:标题名(包含空格)(可以适当省略部分文字):具体描述(可选)
```

如,以下 commit 名均符合规范

```
```plain
新增博文:VS 简明使用教程
更新博文:浅谈 Python 3.10 新特性:联合类型
```
Expand Down
102 changes: 53 additions & 49 deletions _includes/scripts/archieve.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
(function() {
(function () {
var SOURCES = window.TEXT_VARIABLES.sources;
function queryString() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var i = 0, queryObj = {}, pair;
var i = 0,
queryObj = {},
pair;
var queryStr = window.location.search.substring(1);
var queryArr = queryStr.split('&');
var queryArr = queryStr.split("&");
for (i = 0; i < queryArr.length; i++) {
pair = queryArr[i].split('=');
pair = queryArr[i].split("=");
// If first entry with this name
if (typeof queryObj[pair[0]] === 'undefined') {
if (typeof queryObj[pair[0]] === "undefined") {
queryObj[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof queryObj[pair[0]] === 'string') {
} else if (typeof queryObj[pair[0]] === "string") {
queryObj[pair[0]] = [queryObj[pair[0]], pair[1]];
// If third or later entry with this name
} else {
Expand All @@ -22,42 +24,41 @@
return queryObj;
}

var setUrlQuery = (function() {
var baseUrl = window.location.href.split('?')[0];
return function(query) {
if (typeof query === 'string') {
window.history.replaceState(null, '', baseUrl + query);
} else {
window.history.replaceState(null, '', baseUrl);
}
};
})();
var setUrlHash = function (hash) {
var url = window.location.href.split("#")[0] + "#";
if (typeof hash === "string" && hash !== "") {
url = url + hash;
}
window.location.replace(url);
// This prevents history entry creation, which makes the back button work as expected.
};

window.Lazyload.js(SOURCES.jquery, function() {
var $tags = $('.js-tags');
var $articleTags = $tags.find('button');
var $tagShowAll = $tags.find('.tag-button--all');
var $result = $('.js-result');
var $sections = $result.find('section');
window.Lazyload.js(SOURCES.jquery, function () {
var $tags = $(".js-tags");
var $articleTags = $tags.find("button");
var $tagShowAll = $tags.find(".tag-button--all");
var $result = $(".js-result");
var $sections = $result.find("section");
var sectionArticles = [];
var $lastFocusButton = null;
var sectionTopArticleIndex = [];
var hasInit = false;

$sections.each(function() {
sectionArticles.push($(this).find('.item'));
$sections.each(function () {
sectionArticles.push($(this).find(".item"));
});

function init() {
var i, index = 0;
var i,
index = 0;
for (i = 0; i < $sections.length; i++) {
sectionTopArticleIndex.push(index);
index += $sections.eq(i).find('.item').length;
index += $sections.eq(i).find(".item").length;
}
sectionTopArticleIndex.push(index);
}

function searchButtonsByTag(_tag/*raw tag*/) {
function searchButtonsByTag(_tag /*raw tag*/) {
if (!_tag) {
return $tagShowAll;
}
Expand All @@ -69,67 +70,70 @@
}
function buttonFocus(target) {
if (target) {
target.addClass('focus');
$lastFocusButton && !$lastFocusButton.is(target) && $lastFocusButton.removeClass('focus');
target.addClass("focus");
$lastFocusButton &&
!$lastFocusButton.is(target) &&
$lastFocusButton.removeClass("focus");
$lastFocusButton = target;
}
}

function tagSelect (tag/*raw tag*/, target) {
var result = {}, $articles;
function tagSelect(tag /*raw tag*/, target) {
var result = {},
$articles;
var i, j, k, _tag;

for (i = 0; i < sectionArticles.length; i++) {
$articles = sectionArticles[i];
for (j = 0; j < $articles.length; j++) {
if (tag === '' || tag === undefined) {
if (tag === "" || tag === undefined) {
result[i] || (result[i] = {});
result[i][j] = true;
} else {
var tags = $articles.eq(j).data('tags').split(',');
var tags = $articles.eq(j).data("tags").split(",");
for (k = 0; k < tags.length; k++) {
if (tags[k] === tag) {
result[i] || (result[i] = {});
result[i][j] = true; break;
result[i][j] = true;
break;
}
}
}
}
}

for (i = 0; i < sectionArticles.length; i++) {
result[i] && $sections.eq(i).removeClass('d-none');
result[i] || $sections.eq(i).addClass('d-none');
result[i] && $sections.eq(i).removeClass("d-none");
result[i] || $sections.eq(i).addClass("d-none");
for (j = 0; j < sectionArticles[i].length; j++) {
if (result[i] && result[i][j]) {
sectionArticles[i].eq(j).removeClass('d-none');
sectionArticles[i].eq(j).removeClass("d-none");
} else {
sectionArticles[i].eq(j).addClass('d-none');
sectionArticles[i].eq(j).addClass("d-none");
}
}
}

hasInit || ($result.removeClass('d-none'), hasInit = true);

hasInit || ($result.removeClass("d-none"), (hasInit = true));

if (target) {
buttonFocus(target);
_tag = target.attr('data-encode');
if (_tag === '' || typeof _tag !== 'string') {
setUrlQuery();
_tag = target.attr("data-encode");
if (_tag === "" || typeof _tag !== "string") {
setUrlHash("");
} else {
setUrlQuery('?tag=' + _tag);
setUrlHash(_tag);
}
} else {
buttonFocus(searchButtonsByTag(tag));
}
}

var query = queryString(), _tag = query.tag;
init(); tagSelect(_tag);
$tags.on('click', 'button', function() {
tagSelect($(this).data('encode'), $(this));
var _tag = window.location.hash.slice(1);
init();
tagSelect(_tag);
$tags.on("click", "button", function () {
tagSelect($(this).data("encode"), $(this));
});

});
})();
1 change: 0 additions & 1 deletion robots.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
User-agent: *
Disallow: /cgi-bin/trace
Disallow: /archive?tag=*
Disallow: /IndexNow-*

0 comments on commit 6f59e45

Please sign in to comment.