Skip to content

Commit

Permalink
fix(Insight): improve URL encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeaturner committed Dec 11, 2023
1 parent c7167c0 commit f2124f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/src/components/kb/KBPageEditMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const KBPageEditMode = ({
if (!res.data.page || !res.data.page.slug) {
throw new Error("Error creating page");
}
window.location.assign(`/kb/${res.data.page.slug}`); // Redirect to new page
window.location.assign(`/insight/${res.data.page.slug}`); // Redirect to new page
} catch (err) {
handleGlobalError(err);
} finally {
Expand Down
15 changes: 10 additions & 5 deletions server/api/kb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,17 @@ function _sanitizeBodyContent(content: string) {
}

function _generatePageSlug(title: string, userInput?: string) {
if (userInput) {
const spacesReplaced = userInput.replace(/\s/g, "-");
return encodeURIComponent(spacesReplaced.toLowerCase());
let val = '';
if (userInput && userInput.length > 0) {
val = userInput;
} else {
val = title
}
const spacesReplaced = title.replace(/\s/g, "-");
return encodeURIComponent(spacesReplaced.toLowerCase());
const trimmed = val.trim(); // remove leading and trailing whitespace
const noQuotations = trimmed.replace(/['"]+/g, ""); // remove quotations
const noSpecialChars = noQuotations.replace(/[!@#$%^&*]/g, ""); // remove special characters
const spacesReplaced = noSpecialChars.replace(/\s/g, "-"); // replace spaces with hyphens
return encodeURIComponent(spacesReplaced.toLowerCase()); // encode the slug
}

export default {
Expand Down

0 comments on commit f2124f9

Please sign in to comment.