Skip to content

Commit 836ea28

Browse files
committed
完成中文路径拼音转换
1 parent 117f851 commit 836ea28

File tree

6 files changed

+71
-17
lines changed

6 files changed

+71
-17
lines changed

astro.config.mjs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
// @ts-check
2-
import { defineConfig } from 'astro/config';
2+
import { defineConfig, envField } from "astro/config";
33

4-
import tailwindcss from '@tailwindcss/vite';
4+
import tailwindcss from "@tailwindcss/vite";
55

66
// https://astro.build/config
77
export default defineConfig({
8-
site: 'https://linglambda.github.io',
9-
base: '/',
10-
8+
site: "https://linglambda.github.io",
9+
base: "/",
10+
1111
//悬停预加载
1212
prefetch: true,
1313

1414
vite: {
1515
plugins: [tailwindcss()],
1616
},
17-
});
17+
env: {
18+
schema: {
19+
PATH_MODULE: envField.enum({
20+
values:['no_change','pinyin','pinyin_with_tones'],
21+
default: 'pinyin',
22+
optional: false,
23+
context: "server",
24+
access: "secret",
25+
}),
26+
},
27+
},
28+
});

post/用javafx和robot类实现截图小工具.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 用javafx和robot类实现截图小工具(未完待续)
2+
title: 用javafx和robot类实现截图小工具
33
date: 2024-6-3 22:31
44
description: 用javafx和robot类实现截图小工具
55
category: 编程

src/components/TitileCard.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
---
2+
import { titleToPinyin } from "../utils/utils";
3+
24
const { title, date, wordNum } = Astro.props;
35
---
46

5-
<a class="post-card" href={`/blog/${title}/`} data-astro-prefetch>
7+
<a class="post-card" href={`/blog/${titleToPinyin(title)}/`} data-astro-prefetch>
68
<div class="card-top">
79
<div class="card-title">{title}</div>
810
<div class="word-num">{wordNum}字</div>

src/content.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { defineCollection, z } from "astro:content";
33

44
// 2. 导入加载器
5-
import { glob, file } from "astro/loaders";
5+
import { glob } from "astro/loaders";
66

77
// 3. 定义你的集合
88
const blog = defineCollection({

src/pages/blog/[page].astro

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,42 @@
22
import { render } from "astro:content";
33
import BaseLayout from "../../layouts/BaseLayout.astro";
44
import Prose from "../../components/Prose.astro";
5-
import { getCollectionOrderByDate, getWordCount } from "../../utils/utils";
5+
import {
6+
getCollectionOrderByDate,
7+
getWordCount,
8+
titleToPinyin,
9+
} from "../../utils/utils";
610
711
export const getStaticPaths = async () => {
8-
const blogs = await getCollectionOrderByDate("blog",({data})=>{
12+
const blogs = await getCollectionOrderByDate("blog", ({ data }) => {
913
return data.published === true;
1014
});
1115
return blogs.map((item) => ({
1216
params: {
13-
page: item.data.title,
17+
page: titleToPinyin(item.data.title),
1418
},
1519
props: {
1620
title: item.data.title,
1721
description: item.data.description,
1822
date: item.data.date,
19-
tags:item.data.tags,
23+
tags: item.data.tags,
2024
fontNum: getWordCount(item.body ? item.body : ""),
2125
Content: item,
2226
},
2327
}));
2428
};
2529
26-
const { title, description, date, fontNum, Content: rawContent, tags} = Astro.props;
30+
const {
31+
title,
32+
description,
33+
date,
34+
fontNum,
35+
Content: rawContent,
36+
tags,
37+
} = Astro.props;
2738
const { Content } = await render(rawContent);
2839
const MIN_READ_COUNT = 350;
2940
const readTimeMin = Math.ceil(fontNum / MIN_READ_COUNT);
30-
console.log(fontNum);
3141
---
3242

3343
<BaseLayout description={description} keywords={tags}>

src/utils/utils.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
getCollection,
33
type AnyEntryMap,
44
type CollectionEntry,
5-
type RenderedContent,
65
} from "astro:content";
76

87
/**
@@ -21,11 +20,43 @@ export const getCollectionOrderByDate = async <C extends keyof AnyEntryMap>(
2120
collection: C,
2221
filter?: (entry: CollectionEntry<C>) => unknown
2322
): Promise<CollectionEntry<C>[]> => {
24-
const cs:CollectionEntry<C>[] = filter
23+
const cs: CollectionEntry<C>[] = filter
2524
? await getCollection(collection, filter)
2625
: await getCollection(collection);
2726
cs.sort((a, b) => {
2827
return new Date(b.data.date).getTime() - new Date(a.data.date).getTime();
2928
});
3029
return cs;
3130
};
31+
32+
import { pinyin } from "pinyin-pro";
33+
const module = import.meta.env.PATH_MODULE; //根据配置修改路径生成规则
34+
export let titleToPinyin = (title: string) =>
35+
title.replaceAll(/\s+/g, "-").replaceAll(/-+/g, "-");
36+
switch (module) {
37+
case "no_change": {
38+
break;
39+
}
40+
case "pinyin": {
41+
titleToPinyin = (title: string) =>
42+
pinyin(title.replaceAll(/\s+/g, "-"), {
43+
toneType: "none",
44+
type: "array",
45+
nonZh: "consecutive",
46+
})
47+
.join("-")
48+
.replaceAll(/-+/g, "-");
49+
break;
50+
}
51+
case "pinyin_with_tones": {
52+
titleToPinyin = (title: string) =>
53+
pinyin(title.replaceAll(/\s+/g, "-"), {
54+
toneType: "num",
55+
type: "array",
56+
nonZh: "consecutive",
57+
})
58+
.join("-")
59+
.replaceAll(/-+/g, "-");
60+
break;
61+
}
62+
}

0 commit comments

Comments
 (0)