fix: Correctly escape RegExp in import tasks

closes #3460
This commit is contained in:
Tom Moor
2022-04-27 23:58:01 -07:00
parent 5f6b6e2879
commit c7e4f491eb
3 changed files with 14 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import { escapeRegExp } from "lodash";
import * as React from "react";
import replace from "string-replace-to-array";
import styled from "styled-components";
@@ -23,7 +24,7 @@ function Highlight({
regex = highlight;
} else {
regex = new RegExp(
(highlight || "").replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&"),
escapeRegExp(highlight || ""),
caseSensitive ? "g" : "gi"
);
}

View File

@@ -1,4 +1,5 @@
import JSZip from "jszip";
import { escapeRegExp } from "lodash";
import mime from "mime-types";
import { v4 as uuidv4 } from "uuid";
import documentImporter from "@server/commands/documentImporter";
@@ -160,9 +161,15 @@ export default class ImportMarkdownZipTask extends ImportTask {
const reference = `<<${attachment.id}>>`;
document.text = document.text
.replace(new RegExp(attachment.path, "g"), reference)
.replace(new RegExp(normalizedAttachmentPath, "g"), reference)
.replace(new RegExp(`/${normalizedAttachmentPath}`, "g"), reference);
.replace(new RegExp(escapeRegExp(attachment.path), "g"), reference)
.replace(
new RegExp(escapeRegExp(normalizedAttachmentPath), "g"),
reference
)
.replace(
new RegExp(escapeRegExp(`/${normalizedAttachmentPath}`), "g"),
reference
);
}
}

View File

@@ -1,6 +1,6 @@
import path from "path";
import JSZip from "jszip";
import { compact } from "lodash";
import { compact, escapeRegExp } from "lodash";
import mime from "mime-types";
import { v4 as uuidv4 } from "uuid";
import documentImporter from "@server/commands/documentImporter";
@@ -148,7 +148,7 @@ export default class ImportNotionTask extends ImportTask {
);
} else {
text = text.replace(
new RegExp(image.src, "g"),
new RegExp(escapeRegExp(image.src), "g"),
`<<${attachment.id}>>`
);
}