From 6ea2fafd9238c29ab3868cf5a9d746907105af39 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Fri, 29 Nov 2024 12:19:39 +0800 Subject: [PATCH] feat: support converting the tags into chinese pinyin. --- package-lock.json | 13 ++++++++++--- package.json | 1 + src/helpers/schema.ts | 14 +++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index ae088e4..5227e4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "luxon": "^3.5.0", "marked": "^15.0.2", "pg": "^8.13.1", + "pinyin-pro": "^3.26.0", "qrcode-svg": "^1.1.0", "sharp": "^0.33.5", "ultrahtml": "^1.5.3" @@ -3890,9 +3891,9 @@ "license": "MIT" }, "node_modules/electron-to-chromium": { - "version": "1.5.66", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.66.tgz", - "integrity": "sha512-pI2QF6+i+zjPbqRzJwkMvtvkdI7MjVbSh2g8dlMguDJIXEPw+kwasS1Jl+YGPEBfGVxsVgGUratAKymPdPo2vQ==", + "version": "1.5.67", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.67.tgz", + "integrity": "sha512-nz88NNBsD7kQSAGGJyp8hS6xSPtWwqNogA0mjtc2nUYeEf3nURK9qpV18TuBdDmEDgVWotS8Wkzf+V52dSQ/LQ==", "license": "ISC" }, "node_modules/emmet": { @@ -7146,6 +7147,12 @@ "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": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz", diff --git a/package.json b/package.json index e160138..469fc9d 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "luxon": "^3.5.0", "marked": "^15.0.2", "pg": "^8.13.1", + "pinyin-pro": "^3.26.0", "qrcode-svg": "^1.1.0", "sharp": "^0.33.5", "ultrahtml": "^1.5.3" diff --git a/src/helpers/schema.ts b/src/helpers/schema.ts index 45ff1ee..4fb8168 100644 --- a/src/helpers/schema.ts +++ b/src/helpers/schema.ts @@ -1,6 +1,7 @@ import { defaultCover } from '@/content/config.ts'; import options from '@/options'; import { getCollection, getEntry, type Render } from 'astro:content'; +import { pinyin } from 'pinyin-pro'; // Import the collections from the astro content. const categoriesCollection = await getCollection('categories'); @@ -80,7 +81,18 @@ if (missingCategories.length > 0) { // Find the missing tags from posts. const missingTags: string[] = posts.flatMap((post) => post.tags).filter((tag) => !tags.find((t) => t.name === tag)); 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.