Skip to content

Commit 4c25056

Browse files
committed
Consistently slugify company URLs
1 parent b950b54 commit 4c25056

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

components/ComparisonTable.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,9 @@ function ComparisonTable<T extends object>({
219219
return
220220
}
221221

222-
const companyRoute = getCompanyURL(lowerCaseName, wikiId)
223-
window.location.href = companyRoute
222+
window.location.assign(getCompanyURL(lowerCaseName, wikiId))
224223
} else {
225-
const municipalityrRoute = `/kommun/${lowerCaseName}`
226-
router.push(municipalityrRoute)
224+
router.push(`/kommun/${lowerCaseName}`)
227225
}
228226
}
229227

package-lock.json

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"react-mailchimp-subscribe": "^2.1.3",
3535
"react-markdown": "^9.0.1",
3636
"rehype-external-links": "^3.0.0",
37+
"slugify": "^1.6.6",
3738
"styled-components": "^6.1.8",
3839
"vite": "^5.2.11"
3940
},

utils/shared.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { slugifyURL } from './slugifyURL'
2+
13
export const normalizeString = (input: string) => input.replace('ä', 'a').replace('ö', 'o').replace('å', 'a').toLowerCase()
24

35
export const toTitleCase = (str: string) => str.replace(
@@ -7,8 +9,8 @@ export const toTitleCase = (str: string) => str.replace(
79

810
export const ONE_WEEK_MS = 60 * 60 * 24 * 7
911

10-
export const getCompanyURL = (companyName: string, wikiId: string) => {
11-
const baseURL = process.env.NODE_ENV === 'production' ? 'https://beta.klimatkollen.se' : 'http://localhost:4321'
12-
const dashName = companyName.toLowerCase().replaceAll(' ', '-')
13-
return `${baseURL}/foretag/${dashName}-${wikiId}`
14-
}
12+
const baseURL = process.env.NODE_ENV === 'development' ? 'http://localhost:4321' : 'https://beta.klimatkollen.se'
13+
14+
export const getCompanyURL = (companyName: string, wikiId: string) => (
15+
`${baseURL}/foretag/${slugifyURL(companyName)}-${wikiId}`
16+
)

utils/slugifyURL.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import slugify from 'slugify'
2+
3+
export const slugifyURL = (url: string, locale = 'sv') => (
4+
slugify(url, {
5+
replacement: '-', // replace spaces with replacement character
6+
remove: undefined, // remove characters that match regex
7+
lower: true, // convert to lower case
8+
strict: true, // strip special characters except replacement
9+
locale, // locale identifier used to create locale-aware slugs
10+
trim: true, // trim leading and trailing replacement chars
11+
})
12+
)

0 commit comments

Comments
 (0)