Skip to content

Commit de0e074

Browse files
authored
Merge pull request #1655 from future-architect/feature
同じ条件のタグを***で表現
2 parents be58248 + fe38b55 commit de0e074

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

scripts/tags.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,37 @@ function customTagCloudHelper(options) {
5151
return '';
5252
}
5353

54-
// 全てのタグの完全な情報をMapに保存し、信頼できるデータソースとする
54+
// 「***」を付与するタグを事前に計算する
55+
// 1. タグを「紐づく記事IDの集合」でグループ化するためのMap
56+
const postSetToTagsMap = new Map();
57+
58+
site.tags.forEach(tag => {
59+
// 記事が2未満のタグは今回の条件に関係ないので除外
60+
if (tag.length < 2) {
61+
return;
62+
}
63+
// 記事のIDをソートして、一意のキーを作成 (例: "id1,id2,id3")
64+
const postIds = tag.posts.map(post => post._id).sort();
65+
const key = postIds.join(',');
66+
67+
if (!postSetToTagsMap.has(key)) {
68+
postSetToTagsMap.set(key, []);
69+
}
70+
postSetToTagsMap.get(key).push(tag.name);
71+
});
72+
73+
// 2. 条件に合う「完全に一致する」タグの集合(Set)を作成
74+
const matchedTagSet = new Set();
75+
postSetToTagsMap.forEach((tagGroup, postSetKey) => {
76+
// 記事リストが完全に一致するタグが2つ以上あるグループのみが対象
77+
if (tagGroup.length >= 2) {
78+
tagGroup.forEach(tagName => {
79+
matchedTagSet.add(tagName);
80+
});
81+
}
82+
});
83+
84+
// 全てのタグの完全な情報をMapに保存(「**」の判定で使用)
5585
const tagDataMap = new Map();
5686
site.tags.forEach(t => {
5787
tagDataMap.set(t.name, t);
@@ -80,13 +110,15 @@ function customTagCloudHelper(options) {
80110
let tagName = tag.name;
81111
let suffix = '';
82112

83-
// suffix(* または **)を決定するロジック
84-
if (tag.length === 1) {
85-
// 記事数が1つのタグは「*」を付ける
113+
// 3. メインループでsuffixを付与
114+
// 「***」の条件を最優先でチェック
115+
if (matchedTagSet.has(tag.name)) {
116+
suffix = '***';
117+
}
118+
// それ以外のタグで、記事数が1つの場合は「*」または「**」の判定
119+
else if (tag.length === 1) {
86120
suffix = '*';
87121
const singlePost = tag.posts.first();
88-
89-
// その記事に紐づく他のタグが、1つでも他の記事に紐づかないタグの場合、「**」を付与する
90122
const otherTags = singlePost.tags.filter(t => t.name !== tag.name);
91123
if (otherTags.length > 0 && otherTags.some(otherTag => {
92124
const fullTagInfo = tagDataMap.get(otherTag.name);
@@ -97,7 +129,6 @@ function customTagCloudHelper(options) {
97129
}
98130

99131
tagName += suffix;
100-
101132
tagName = tagName.replace(/ /g, '-');
102133
const tagLink = hexo.url_for(tag.path);
103134

0 commit comments

Comments
 (0)