Skip to content

Fix(slugify): transliterate non ascii - #763

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
shahzainshafique:fix/slugify-transliterate-non-ascii
Aug 21, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
shahzainshafique:fix/slugify-transliterate-non-ascii

Conversation

@shahzainshafique

@shahzainshafique shahzainshafique commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

slugify was stripping every character outside [a-z0-9-], which deleted accented and non-ASCII letters instead of transliterating them. Ürün came out as rn, so CSV/PDF exports were named ansvisor_rn_…, and it collided more than it should (Ağaç and Aa both produced aa). Given how many brands are Turkish, this hollowed names out fairly often.

This PR transliterates before stripping. An explicit map handles the letters Unicode NFD doesn't decompose on its own (Turkish ı/ş/ğ, German ß, Nordic ø, Polish ł, etc.) and pins the Turkish I/ı lowercasing case to i. An NFD pass then covers the rest of Latin-1 (é→e, ü→u, ç→c). Ürün now slugifies to urun.

It also pulls the three drifted copies of this logic in brand.ts, organization.ts, and the inline chain in onboarding/page.tsx and turns them into one shared helper at web/src/lib/slug.ts, with each call site keeping its own fallback prefix (brand-, org-). New unit tests sit next to csv.test.ts and include Işık for the Turkish lowercase gotcha. Existing slugs are intentionally not backfilled.

Related issue

Closes #760

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to Ansvisor, and thank you for your first PR! A maintainer will be by soon to review.

In the meantime, two things that make review faster:

  • A short description of why the change matters, not just what it does — most of our PRs link a Fixes #... to the issue that explains the why.
  • A quick local test note if there's anything worth eyeballing in the browser.

If you'd like to keep an eye on where Ansvisor goes next, a ⭐ on the repo helps others discover it (and Watch sends you a ping when related issues land). Either way — looking forward to your next one.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on — the transliteration itself is right, and the consolidation is what the issue asked for. I pulled the branch and ran it: the 8 new tests pass, tsc --noEmit is clean, lint has 0 errors, all three copies are gone with no leftovers, and each call site keeps its own fallback prefix. Every case from the issue's table comes out right, including Işık and İstanbul.

Three things before merge.

1. format:check fails — blocking.

CI is red on it, and it is one trailing space at the end of src/lib/slug.ts:31:

 * brand's name.·

Worth correcting the note in the PR description: this is not pre-existing. I ran yarn format:check from web/ across the whole tree and src/lib/slug.ts is the only file that fails — the four files you edited are all Prettier-clean, and server and schema passed. yarn format from web/ fixes it.

2. The character list is written twice.

.replace(/[ıİIşŞğĞßøØłŁđĐðÐþÞæÆœŒ]/g, (ch) => TRANSLITERATIONS[ch] ?? ch)

The same 22 characters live in the regex and in the map's keys. They agree today, but this issue existed because one rule was written in three places and they drifted — reintroducing that on a smaller scale is worth avoiding. Deriving the class from Object.keys(TRANSLITERATIONS), or just matching [^\u0000-\u007F] and letting ?? ch pass unmapped characters through to the NFD pass, removes the second copy.

3. Nit: test('plain ASCII names lowercase and hyphenate') asserts slugify('Öz Güven'), which is not ASCII. Either rename it or move that case in with the accented ones.

Non-blocking and out of scope, just noting it: a name written entirely in a non-Latin script still slugs to empty and falls back to the timestamp. That is unchanged from before, so nothing regressed.

slugify stripped every character outside [a-z0-9-], so accented and
non-ASCII letters were deleted rather than transliterated: "Ürün"
became "rn" and its CSV/PDF exports were named ansvisor_rn_… . For a
product with many Turkish brands this hollowed names out and made
slugs collide ("Ağaç" and "Aa" both produced "aa").

Transliterate before stripping: an explicit map covers letters Unicode
NFD does not decompose (Turkish dotless/dotted i, German ß, Nordic ø,
Polish ł, …) and the Turkish-lowercasing hazard around I/ı, then an
NFD pass handles the rest of Latin-1 (é→e, ü→u, ç→c). "Ürün" now
slugifies to "urun".

Pull the three drifted copies (brand.ts, organization.ts, and the
inline chain in onboarding) into one shared helper in lib/slug.ts, with
a unit test alongside csv.test.ts. Existing rows are intentionally not
backfilled; this only changes what new names produce.

Closes ansvisor#760
@shahzainshafique
shahzainshafique force-pushed the fix/slugify-transliterate-non-ascii branch from eb7951d to c475bec Compare August 21, 2026 12:26
@shahzainshafique

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review! Fixed all three:

  • Trailing space is gone: format:check passes now. And you're right, my note was wrong; I've corrected it, that file was the only real failure.
  • Good call on the duplication: that's exactly the trap this issue was about. The character set now lives only in the map and the regex is derived from its keys via Object.keys, so the two can't drift.
  • Renamed the ASCII test to use actual ASCII and moved Öz Güven in with the accented cases.

Squashed it down to a single commit. The non-Latin-script-to-empty fallback is unchanged as you noted.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three addressed, CI green. Deriving the pattern from the map's keys is the right call. Thanks!

@gkhngyk
gkhngyk merged commit 05f2b16 into ansvisor:main Aug 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: slugify deletes non-ASCII letters instead of transliterating them

2 participants