Skip to content

Commit fb63d96

Browse files
authored
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
1 parent c20d747 commit fb63d96

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"remark-toc": "^9.0.0",
2525
"satori": "^0.18.3",
2626
"sharp": "^0.34.5",
27+
"slugify": "^1.6.6",
2728
"tailwindcss": "^4.1.18"
2829
},
2930
"devDependencies": {

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Card.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const { title, description, ...props } = data;
2222
"focus-visible:no-underline focus-visible:underline-offset-0",
2323
]}
2424
>
25-
<Heading style={{ viewTransitionName: slugifyStr(title) }}>
25+
<Heading
26+
style={{ viewTransitionName: slugifyStr(title.replaceAll(".", "-")) }}
27+
>
2628
{title}
2729
</Heading>
2830
</a>

src/layouts/PostDetails.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const nextPost =
9191
data-pagefind-body
9292
>
9393
<h1
94-
transition:name={slugifyStr(title)}
94+
transition:name={slugifyStr(title.replaceAll(".", "-"))}
9595
class="inline-block text-2xl font-bold text-accent sm:text-3xl"
9696
>
9797
{title}

src/utils/slugify.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import kebabcase from "lodash.kebabcase";
2+
import slugify from "slugify";
23

3-
export const slugifyStr = (str: string) => kebabcase(str);
4+
/**
5+
* Check if string contains non-Latin characters
6+
*/
7+
const hasNonLatin = (str: string): boolean => /[^\x00-\x7F]/.test(str);
8+
9+
/**
10+
* Slugify a string using a hybrid approach:
11+
* - For Latin-only strings: use slugify (eg: "E2E Testing" -> "e2e-testing", "TypeScript 5.0" -> "typescript-5.0")
12+
* - For strings with non-Latin characters: use lodash.kebabcase (preserves non-Latin chars)
13+
*/
14+
export const slugifyStr = (str: string): string => {
15+
if (hasNonLatin(str)) {
16+
// Preserve non-Latin characters (e.g., Burmese, Chinese, etc.)
17+
return kebabcase(str);
18+
}
19+
// Handle Latin strings with better number/acronym handling
20+
return slugify(str, { lower: true });
21+
};
422

523
export const slugifyAll = (arr: string[]) => arr.map(str => slugifyStr(str));

0 commit comments

Comments
 (0)