feat: support converting the tags into chinese pinyin.

This commit is contained in:
Yufan Sheng 2024-11-29 12:19:39 +08:00
parent 1e322ec952
commit 6ea2fafd92
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
3 changed files with 24 additions and 4 deletions

13
package-lock.json generated
View File

@ -19,6 +19,7 @@
"luxon": "^3.5.0", "luxon": "^3.5.0",
"marked": "^15.0.2", "marked": "^15.0.2",
"pg": "^8.13.1", "pg": "^8.13.1",
"pinyin-pro": "^3.26.0",
"qrcode-svg": "^1.1.0", "qrcode-svg": "^1.1.0",
"sharp": "^0.33.5", "sharp": "^0.33.5",
"ultrahtml": "^1.5.3" "ultrahtml": "^1.5.3"
@ -3890,9 +3891,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.5.66", "version": "1.5.67",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.66.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.67.tgz",
"integrity": "sha512-pI2QF6+i+zjPbqRzJwkMvtvkdI7MjVbSh2g8dlMguDJIXEPw+kwasS1Jl+YGPEBfGVxsVgGUratAKymPdPo2vQ==", "integrity": "sha512-nz88NNBsD7kQSAGGJyp8hS6xSPtWwqNogA0mjtc2nUYeEf3nURK9qpV18TuBdDmEDgVWotS8Wkzf+V52dSQ/LQ==",
"license": "ISC" "license": "ISC"
}, },
"node_modules/emmet": { "node_modules/emmet": {
@ -7146,6 +7147,12 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/pinyin-pro": {
"version": "3.26.0",
"resolved": "https://registry.npmjs.org/pinyin-pro/-/pinyin-pro-3.26.0.tgz",
"integrity": "sha512-HcBZZb0pvm0/JkPhZHWA5Hqp2cWHXrrW/WrV+OtaYYM+kf35ffvZppIUuGmyuQ7gDr1JDJKMkbEE+GN0wfMoGg==",
"license": "MIT"
},
"node_modules/pkg-dir": { "node_modules/pkg-dir": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz",

View File

@ -52,6 +52,7 @@
"luxon": "^3.5.0", "luxon": "^3.5.0",
"marked": "^15.0.2", "marked": "^15.0.2",
"pg": "^8.13.1", "pg": "^8.13.1",
"pinyin-pro": "^3.26.0",
"qrcode-svg": "^1.1.0", "qrcode-svg": "^1.1.0",
"sharp": "^0.33.5", "sharp": "^0.33.5",
"ultrahtml": "^1.5.3" "ultrahtml": "^1.5.3"

View File

@ -1,6 +1,7 @@
import { defaultCover } from '@/content/config.ts'; import { defaultCover } from '@/content/config.ts';
import options from '@/options'; import options from '@/options';
import { getCollection, getEntry, type Render } from 'astro:content'; import { getCollection, getEntry, type Render } from 'astro:content';
import { pinyin } from 'pinyin-pro';
// Import the collections from the astro content. // Import the collections from the astro content.
const categoriesCollection = await getCollection('categories'); const categoriesCollection = await getCollection('categories');
@ -80,7 +81,18 @@ if (missingCategories.length > 0) {
// Find the missing tags from posts. // Find the missing tags from posts.
const missingTags: string[] = posts.flatMap((post) => post.tags).filter((tag) => !tags.find((t) => t.name === tag)); const missingTags: string[] = posts.flatMap((post) => post.tags).filter((tag) => !tags.find((t) => t.name === tag));
if (missingTags.length > 0) { if (missingTags.length > 0) {
throw new Error(`The bellowing tags has not been configured:\n$${missingTags.join('\n')}`); console.warn(`The bellowing tags has not been configured:\n${missingTags.join('\n')}`);
for (const missingTag of missingTags) {
const slug = pinyin(missingTag, { toneType: 'none', separator: '-', nonZh: 'consecutive', type: 'string' })
.replaceAll(' ', '-')
.toLowerCase();
tags.push({
name: missingTag,
slug: slug,
permalink: `/tags/${slug}`,
counts: posts.filter((post) => post.tags.includes(missingTag)).length,
});
}
} }
// Find the missing covers from posts. // Find the missing covers from posts.