mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
fix: slugify supports better acronyms and preserve non-latin char (#606)
* fix: slugify supports better acronyms and preserve non-latin char Update slugify function to handle better acronyms and preserve non-latin characters. - uses slugify (simov) for Latin-only strings to fix acronym issue (eg: e2e-testing) - uses lodash.kebabcase for strings with non-latin characters to preserve them * fix: remove dots in slugified titles for view transitions Fixes #584
This commit is contained in:
+19
-1
@@ -1,5 +1,23 @@
|
||||
import kebabcase from "lodash.kebabcase";
|
||||
import slugify from "slugify";
|
||||
|
||||
export const slugifyStr = (str: string) => kebabcase(str);
|
||||
/**
|
||||
* Check if string contains non-Latin characters
|
||||
*/
|
||||
const hasNonLatin = (str: string): boolean => /[^\x00-\x7F]/.test(str);
|
||||
|
||||
/**
|
||||
* Slugify a string using a hybrid approach:
|
||||
* - For Latin-only strings: use slugify (eg: "E2E Testing" -> "e2e-testing", "TypeScript 5.0" -> "typescript-5.0")
|
||||
* - For strings with non-Latin characters: use lodash.kebabcase (preserves non-Latin chars)
|
||||
*/
|
||||
export const slugifyStr = (str: string): string => {
|
||||
if (hasNonLatin(str)) {
|
||||
// Preserve non-Latin characters (e.g., Burmese, Chinese, etc.)
|
||||
return kebabcase(str);
|
||||
}
|
||||
// Handle Latin strings with better number/acronym handling
|
||||
return slugify(str, { lower: true });
|
||||
};
|
||||
|
||||
export const slugifyAll = (arr: string[]) => arr.map(str => slugifyStr(str));
|
||||
|
||||
Reference in New Issue
Block a user