Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/lib/db/FSNoteDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
isSubPathname,
keys,
} from './utils'
import { generateId, getHexatrigesimalString } from '../string'
import { escapeRegExp, generateId, getHexatrigesimalString } from '../string'
import {
prepareDirectory,
readFileAsString,
Expand Down Expand Up @@ -258,7 +258,9 @@ class FSNoteDb implements NoteDb {
await unlinkFile(attachmentPathname)
}

async createNote(noteProps: Partial<NoteDocEditibleProps | NoteDocImportableProps>) {
async createNote(
noteProps: Partial<NoteDocEditibleProps | NoteDocImportableProps>
) {
const now = getNow()
const noteDoc: NoteDoc = {
_id: generateNoteId(),
Expand Down Expand Up @@ -493,7 +495,10 @@ class FSNoteDb implements NoteDb {
const allFoldersToRename = this.getAllFolderUnderPathname(pathname).sort()

const replacePathname = (folderPathname: string) => {
return folderPathname.replace(new RegExp(`^${pathname}`), newPathname)
return folderPathname.replace(
new RegExp(`^${escapeRegExp(pathname)}`, 'g'),
newPathname
)
}
await Promise.all(
allFoldersToRename.map(async (folderPathname) => {
Expand Down Expand Up @@ -564,7 +569,7 @@ class FSNoteDb implements NoteDb {

getAllFolderUnderPathname(pathname: string) {
const allFolderPathnames = keys(this.data!.folderMap)
const pathnameRegexp = new RegExp(`^${pathname}/`)
const pathnameRegexp = new RegExp(`^${escapeRegExp(pathname)}/`, 'g')
const subFolderPathnames = allFolderPathnames.filter((pathname) => {
return pathnameRegexp.test(pathname)
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function generateId(): string {
export const generateRandomHex = () => randomBytes(32).toString('hex')

export function escapeRegExp(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&')
}

export function filenamify(value: string) {
Expand Down