Skip to content

Commit

Permalink
Fix/whitespace and new line chars (#1351)
Browse files Browse the repository at this point in the history
* chore: add util method to trim and replace unicode line breaks

* fix: create and update endpoints

* chore: replace any typing

---------

Co-authored-by: seaerchin <[email protected]>
  • Loading branch information
alexanderleegs and seaerchin authored Jun 4, 2024
1 parent 436ada2 commit ed77606
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 30 deletions.
19 changes: 15 additions & 4 deletions src/routes/v2/authenticatedSites/collectionPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {
attachRollbackRouteHandlerWrapper,
} = require("@middleware/routeHandler")

const { recursiveTrimAndReplaceLineBreaks } = require("@utils/yaml-utils")

const {
CreatePageRequestSchema,
UpdatePageRequestSchema,
Expand All @@ -32,17 +34,20 @@ class CollectionPagesRouter {
if (error) throw new BadRequestError(error.message)
const {
content: { frontMatter, pageBody },
newFileName,
newFileName: unformattedNewFileName,
} = req.body
let createResp
const newFileName = recursiveTrimAndReplaceLineBreaks(
unformattedNewFileName
)
if (subcollectionName) {
createResp = await this.subcollectionPageService.create(
userWithSiteSessionData,
{
fileName: newFileName,
collectionName,
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
subcollectionName,
}
)
Expand Down Expand Up @@ -98,10 +103,16 @@ class CollectionPagesRouter {
const { error } = UpdatePageRequestSchema.validate(req.body)
if (error) throw new BadRequestError(error.message)
const {
content: { frontMatter, pageBody },
content: { frontMatter: unformattedFrontMatter, pageBody },
sha,
newFileName,
newFileName: unformattedNewFileName,
} = req.body
const frontMatter = recursiveTrimAndReplaceLineBreaks(
unformattedFrontMatter
)
const newFileName = recursiveTrimAndReplaceLineBreaks(
unformattedNewFileName
)
let updateResp
if (subcollectionName) {
if (newFileName) {
Expand Down
12 changes: 8 additions & 4 deletions src/routes/v2/authenticatedSites/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {
attachRollbackRouteHandlerWrapper,
} = require("@middleware/routeHandler")

const { recursiveTrimAndReplaceLineBreaks } = require("@utils/yaml-utils")

const {
CreateDirectoryRequestSchema,
RenameDirectoryRequestSchema,
Expand Down Expand Up @@ -72,7 +74,9 @@ class CollectionsRouter {
userWithSiteSessionData,
{
collectionName,
subcollectionName: newDirectoryName,
subcollectionName: recursiveTrimAndReplaceLineBreaks(
newDirectoryName
),
objArray: items,
}
)
Expand All @@ -81,7 +85,7 @@ class CollectionsRouter {
createResp = await this.collectionDirectoryService.createDirectory(
userWithSiteSessionData,
{
collectionName: newDirectoryName,
collectionName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
objArray: items,
}
)
Expand All @@ -105,7 +109,7 @@ class CollectionsRouter {
{
collectionName,
subcollectionName,
newDirectoryName,
newDirectoryName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
}
)
} else {
Expand All @@ -114,7 +118,7 @@ class CollectionsRouter {
githubSessionData,
{
collectionName,
newDirectoryName,
newDirectoryName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
}
)
}
Expand Down
8 changes: 7 additions & 1 deletion src/routes/v2/authenticatedSites/contactUs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {
attachRollbackRouteHandlerWrapper,
} = require("@middleware/routeHandler")

const { recursiveTrimAndReplaceLineBreaks } = require("@utils/yaml-utils")

const { UpdateContactUsSchema } = require("@validators/RequestSchema")

class ContactUsRouter {
Expand Down Expand Up @@ -42,7 +44,11 @@ class ContactUsRouter {

const updatedContactUsPage = await this.contactUsPageService.update(
userWithSiteSessionData,
{ content: pageBody, frontMatter, sha }
{
content: pageBody,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
sha,
}
)

res.status(200).json(updatedContactUsPage)
Expand Down
4 changes: 3 additions & 1 deletion src/routes/v2/authenticatedSites/homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {
attachRollbackRouteHandlerWrapper,
} = require("@middleware/routeHandler")

const { recursiveTrimAndReplaceLineBreaks } = require("@utils/yaml-utils")

const { UpdateHomepageSchema } = require("@validators/RequestSchema")

class HomepageRouter {
Expand Down Expand Up @@ -44,7 +46,7 @@ class HomepageRouter {
userWithSiteSessionData,
{
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
sha,
}
)
Expand Down
9 changes: 5 additions & 4 deletions src/routes/v2/authenticatedSites/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
RequestHandler,
} from "@root/types"
import { nameAnonymousMethods } from "@root/utils/apm-utils"
import { recursiveTrimAndReplaceLineBreaks } from "@root/utils/yaml-utils"
import {
CreateMediaDirectoryRequestSchema,
CreateMediaFileRequestSchema,
Expand Down Expand Up @@ -154,7 +155,7 @@ export class MediaRouter {
userWithSiteSessionData,
githubSessionData,
{
directoryName: newDirectoryName,
directoryName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
objArray: items,
}
)
Expand Down Expand Up @@ -185,7 +186,7 @@ export class MediaRouter {
githubSessionData,
{
directoryName,
newDirectoryName,
newDirectoryName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
}
)

Expand Down Expand Up @@ -270,7 +271,7 @@ export class MediaRouter {
const createResp = await this.mediaFileService.create(
userWithSiteSessionData,
{
fileName: newFileName,
fileName: recursiveTrimAndReplaceLineBreaks(newFileName),
directoryName,
content,
}
Expand Down Expand Up @@ -325,7 +326,7 @@ export class MediaRouter {
githubSessionData,
{
oldFileName: fileName,
newFileName,
newFileName: recursiveTrimAndReplaceLineBreaks(newFileName),
directoryName,
sha,
}
Expand Down
8 changes: 6 additions & 2 deletions src/routes/v2/authenticatedSites/resourceCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const {
MoveResourceDirectoryPagesRequestSchema,
} = require("@validators/RequestSchema")

const { recursiveTrimAndReplaceLineBreaks } = require("@root/utils/yaml-utils")

class ResourceCategoriesRouter {
constructor({ resourceDirectoryService }) {
this.resourceDirectoryService = resourceDirectoryService
Expand Down Expand Up @@ -46,7 +48,9 @@ class ResourceCategoriesRouter {
userWithSiteSessionData,
{
resourceRoomName,
resourceCategoryName: newDirectoryName,
resourceCategoryName: recursiveTrimAndReplaceLineBreaks(
newDirectoryName
),
}
)

Expand All @@ -68,7 +72,7 @@ class ResourceCategoriesRouter {
{
resourceRoomName,
resourceCategoryName,
newDirectoryName,
newDirectoryName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
}
)

Expand Down
10 changes: 6 additions & 4 deletions src/routes/v2/authenticatedSites/resourcePages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const {
DeleteResourcePageRequestSchema,
} = require("@validators/RequestSchema")

const { recursiveTrimAndReplaceLineBreaks } = require("@root/utils/yaml-utils")

class ResourcePagesRouter {
constructor({ resourcePageService }) {
this.resourcePageService = resourcePageService
Expand All @@ -36,11 +38,11 @@ class ResourcePagesRouter {
const createResp = await this.resourcePageService.create(
userWithSiteSessionData,
{
fileName: newFileName,
fileName: recursiveTrimAndReplaceLineBreaks(newFileName),
resourceRoomName,
resourceCategoryName,
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
}
)

Expand Down Expand Up @@ -84,11 +86,11 @@ class ResourcePagesRouter {
userWithSiteSessionData,
{
oldFileName: pageName,
newFileName,
newFileName: recursiveTrimAndReplaceLineBreaks(newFileName),
resourceRoomName,
resourceCategoryName,
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
sha,
}
)
Expand Down
6 changes: 4 additions & 2 deletions src/routes/v2/authenticatedSites/resourceRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const {
RenameResourceDirectoryRequestSchema,
} = require("@validators/RequestSchema")

const { recursiveTrimAndReplaceLineBreaks } = require("@root/utils/yaml-utils")

class ResourceRoomRouter {
constructor({ resourceRoomDirectoryService }) {
this.resourceRoomDirectoryService = resourceRoomDirectoryService
Expand Down Expand Up @@ -57,7 +59,7 @@ class ResourceRoomRouter {
const createResp = await this.resourceRoomDirectoryService.createResourceRoomDirectory(
userWithSiteSessionData,
{
resourceRoomName: newDirectoryName,
resourceRoomName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
}
)

Expand All @@ -78,7 +80,7 @@ class ResourceRoomRouter {
githubSessionData,
{
resourceRoomName,
newDirectoryName,
newDirectoryName: recursiveTrimAndReplaceLineBreaks(newDirectoryName),
}
)

Expand Down
5 changes: 4 additions & 1 deletion src/routes/v2/authenticatedSites/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
UpdateRepoPasswordRequestSchema,
} = require("@validators/RequestSchema")

const { recursiveTrimAndReplaceLineBreaks } = require("@root/utils/yaml-utils")
const { isPasswordValid } = require("@root/validators/validators")
const { SettingsService } = require("@services/configServices/SettingsService")

Expand Down Expand Up @@ -68,7 +69,9 @@ class SettingsRouter {
configContent: updatedConfigContent,
footerContent: updatedFooterContent,
navigationContent: updatedNavigationContent,
} = SettingsService.retrieveSettingsFields(settings)
} = SettingsService.retrieveSettingsFields(
recursiveTrimAndReplaceLineBreaks(settings)
)

await this.settingsService.updateSettingsFiles(userWithSiteSessionData, {
config,
Expand Down
12 changes: 7 additions & 5 deletions src/routes/v2/authenticatedSites/unlinkedPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const {
MoveDirectoryPagesRequestSchema,
} = require("@validators/RequestSchema")

const { recursiveTrimAndReplaceLineBreaks } = require("@root/utils/yaml-utils")

class UnlinkedPagesRouter {
constructor({ unlinkedPageService, unlinkedPagesDirectoryService }) {
this.unlinkedPageService = unlinkedPageService
Expand Down Expand Up @@ -46,9 +48,9 @@ class UnlinkedPagesRouter {
const createResp = await this.unlinkedPageService.create(
userWithSiteSessionData,
{
fileName: newFileName,
fileName: recursiveTrimAndReplaceLineBreaks(newFileName),
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
}
)

Expand Down Expand Up @@ -88,9 +90,9 @@ class UnlinkedPagesRouter {
userWithSiteSessionData,
{
oldFileName: pageName,
newFileName,
newFileName: recursiveTrimAndReplaceLineBreaks(newFileName),
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
sha,
}
)
Expand All @@ -100,7 +102,7 @@ class UnlinkedPagesRouter {
{
fileName: pageName,
content: pageBody,
frontMatter,
frontMatter: recursiveTrimAndReplaceLineBreaks(frontMatter),
sha,
}
)
Expand Down
39 changes: 37 additions & 2 deletions src/utils/yaml-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ import yaml from "yaml"

import { sanitizer } from "@services/utilServices/Sanitizer"

const LINE_BREAK_REGEX = "[\r\n\x0B\x0C\u0085\u2028\u2029]"
const GLOBAL_LINE_BREAK_SEARCH = new RegExp(LINE_BREAK_REGEX, "gi")

type RecursiveInput =
| string
| RecursiveInput[]
| { [key: string]: RecursiveInput }
| undefined
| null

export const recursiveTrimAndReplaceLineBreaks = (
obj: RecursiveInput
): RecursiveInput => {
if (typeof obj === "string") {
return obj.replaceAll(GLOBAL_LINE_BREAK_SEARCH, " ").trim()
}
if (Array.isArray(obj)) {
return obj.map((item) => recursiveTrimAndReplaceLineBreaks(item))
}
if (typeof obj === "object" && obj !== null) {
const newObj: RecursiveInput = {}
Object.keys(obj).forEach((key) => {
newObj[key] = recursiveTrimAndReplaceLineBreaks(obj[key])
})
return newObj
}
return obj
}

// Note: `yaml.parse()` and `yaml.stringify()` should not be used anywhere
// else in the codebase.
export const sanitizedYamlParse = (
Expand All @@ -17,7 +46,13 @@ export const sanitizedYamlParse = (
: value
)

export const sanitizedYamlStringify = (prestringifiedContent: object): string =>
yaml.stringify(prestringifiedContent, (key, value) =>
export const sanitizedYamlStringify = (
prestringifiedContent: RecursiveInput
): string => {
const formattedPrestringifiedContent = recursiveTrimAndReplaceLineBreaks(
prestringifiedContent
)
return yaml.stringify(formattedPrestringifiedContent, (key, value) =>
typeof value === "string" && !!value ? sanitizer.sanitize(value) : value
)
}

0 comments on commit ed77606

Please sign in to comment.