-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: preserve Unicode in routable content slugs #2505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7619326
fix: keep routable content addressable across languages
ascorbic 2d5bb80
fix: keep invalid legacy slugs out of sitemaps
ascorbic 46e357f
fix: close routable publish bypasses
ascorbic 8232139
fix: support slugless non-routable seeds
ascorbic be17c3f
fix: protect published routable slugs on update
ascorbic 009ca11
fix: normalize unusable slugs in seed exports
ascorbic 3ae8359
fix: reject slugless routable schedules
ascorbic b292c3c
chore: clarify slug generation contract
ascorbic 041ca5c
chore: remove editor implementation narrative
ascorbic 329c503
test: keep revisionless fixture routable
ascorbic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "emdash": minor | ||
| "@emdash-cms/admin": minor | ||
| --- | ||
|
|
||
| Adds native Unicode slugs and requires published entries in routable collections to have a slug. Collections used only for internal or referenced content can set `routable: false` before publishing slugless entries. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| const SEPARATOR_PATTERN = /[\s_]+/gu; | ||
| const UNSAFE_CHARACTER_PATTERN = /[^\p{Letter}\p{Number}\p{Mark}-]+/gu; | ||
| const MULTIPLE_HYPHENS_PATTERN = /-+/g; | ||
| const EDGE_HYPHENS_PATTERN = /^-+|-+$/g; | ||
| const TRAILING_HYPHENS_PATTERN = /-+$/g; | ||
| const USABLE_CHARACTER_PATTERN = /[\p{Letter}\p{Number}]/u; | ||
| const GRAPHEME_SEGMENTER = new Intl.Segmenter("en", { granularity: "grapheme" }); | ||
|
|
||
| function fallbackSlug(value: string): string { | ||
| let hash = 2_166_136_261; | ||
| for (let index = 0; index < value.length; index++) { | ||
| hash ^= value.charCodeAt(index); | ||
| hash = Math.imul(hash, 16_777_619); | ||
| } | ||
| return `untitled-${(hash >>> 0).toString(36).padStart(7, "0")}`; | ||
| } | ||
|
|
||
| function truncateByGrapheme(value: string, maxLength: number): string { | ||
| if (maxLength <= 0) return ""; | ||
| if (!Number.isFinite(maxLength)) return value; | ||
|
|
||
| let result = ""; | ||
| let length = 0; | ||
| for (const { segment } of GRAPHEME_SEGMENTER.segment(value)) { | ||
| if (length >= Math.floor(maxLength)) break; | ||
| result += segment; | ||
| length++; | ||
| } | ||
| return result.replace(TRAILING_HYPHENS_PATTERN, ""); | ||
| } | ||
|
|
||
| /** | ||
| * Convert text to a browser-safe Unicode URL slug. | ||
| * | ||
| * Text is NFKC-normalized and lowercased; whitespace and underscores become | ||
| * hyphens while Unicode letters, numbers, and combining marks are preserved. | ||
| * The length limit counts grapheme clusters. Inputs without usable characters | ||
| * receive a stable `untitled-*` fallback. | ||
| */ | ||
| export function slugify(text: string, maxLength = 80): string { | ||
| const normalized = text.normalize("NFKC").toLowerCase(); | ||
| const slug = normalized | ||
| .replace(SEPARATOR_PATTERN, "-") | ||
| .replace(UNSAFE_CHARACTER_PATTERN, "") | ||
| .replace(MULTIPLE_HYPHENS_PATTERN, "-") | ||
| .replace(EDGE_HYPHENS_PATTERN, ""); | ||
| const value = USABLE_CHARACTER_PATTERN.test(slug) ? slug : fallbackSlug(normalized); | ||
| return truncateByGrapheme(value, maxLength); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.