现在的CMS系统都设计都好强悍呀!
昨天刷到蜗牛大佬Star了一个叫 [pocketbase](https://pocketbase.io/) 的内容管理系统,Go写的,结构简单,API、权限什么都一应俱全,相当哇撒了。
因为官方没有自带slug生成的功能,所以我跟着官方文档写了个转换脚本,提供的event也相当多,很有意思。
目前唯一的疑惑是为啥不提供markdown格式的fields呀?
#CMS #Github #博客
```javascript
onRecordBeforeUpdateRequest((e) => {
const pinyin = require(`pinyin`);
const title = e.record.get("title");
const titleToSlug = (title) => {
const pinyinTitle = pinyin(title, {
style: pinyin.STYLE_NORMAL, // 普通风格
heteronym: false, // 不使用多音字
})
.flat()
.join("-"); // 将拼音数组转换为字符串并用短横线连接
return pinyinTitle
.trim() // 去除首尾空格
.replace(/[\s]+/g, "-") // 将空格替换为短横线
.replace(/[^\w\-]+/g, "") // 删除非字母数字和非短横线字符
.replace(/--+/g, "-") // 替换多个短横线为一个短横线
.replace(/^-+|-+$/g, ""); // 去除首尾的短横线
};
// 示例用法
const slug = titleToSlug(title);
e.record.set("slug", slug);
}, "posts");
```