タグ一覧を生成する
#cosense
kitasenjudesign
Tag
code:tags.js
function countHashtags(data) {
const counts = new Map();
const NG_CHARS = new Set([
'%', '/', '=', ',', ';', ''', ':', '~', '+', '\\'
]);
const isColorCode = (tag) => {
return /^#0-9a-fA-F{3}$/.test(tag) || /^#0-9a-fA-F{6}$/.test(tag);
};
const hasNGChar = (tag) => {
return ...tag.some(char => NG_CHARS.has(char));
};
for (const page of data.pages || []) {
if (page.title === "tag") continue;
for (const line of page.lines || []) {
if (typeof line !== "string") continue;
const matches = line.matchAll(/#^\s#+/g);
for (const match of matches) {
let tag = match0;
const index = match.index;
// URL内の # を除外
if (index > 0 && lineindex - 1 === '/') continue;
const before = line.slice(0, index);
if (/(https?:\/\/|www\.)\S*$/.test(before)) continue;
// NG文字
if (hasNGChar(tag)) continue;
// カラーコード
if (isColorCode(tag)) continue;
// ✅ 小文字に統一
const normalizedTag = tag.toLowerCase();
counts.set(normalizedTag, (counts.get(normalizedTag) || 0) + 1);
}
}
}
return ...counts.entries()
.filter((count) => count >= 2)
.sort((a, b) => b1 - a1)
.map((tag, count) => ${tag} ${count})
.join(" ");
}
/* tagを自動検出 */
scrapbox.PageMenu.addMenu({
title: 'maketags',
image: 'https://gyazo.com/d7ca2e6648a9523bc72696ac8286e27d.png',
onClick: async () => {
const url = "https://scrapbox.io/api/page-data/export/kinetictypo.json";
try {
// JSONを取得
const response = await fetch(url);
if (!response.ok) {
throw new Error(HTTPエラー: ${response.status});
}
const data = await response.json();
// クリップボードにコピー
let text = countHashtags(data);
//console.log(text);
await navigator.clipboard.writeText(text);
alert("タグ一覧をクリップボードにコピーしました!");
} catch (error) {
console.error("取得中にエラーが発生しました:", error);
alert("エラーが発生しました");
}
}
});