fix: Double recursive loops can cause server lockup on deeply nested docs (#5222)

This commit is contained in:
Tom Moor
2023-04-18 19:38:35 -04:00
committed by GitHub
parent bcffd81c9c
commit 1642eb610d
7 changed files with 61 additions and 27 deletions

View File

@@ -2,25 +2,24 @@ import { Node } from "prosemirror-model";
import { parser } from "@server/editor";
export default function parseImages(text: string): string[] {
const value = parser.parse(text);
const doc = parser.parse(text);
const images: string[] = [];
function findImages(node: Node) {
doc.descendants((node: Node) => {
if (node.type.name === "image") {
if (!images.includes(node.attrs.src)) {
images.push(node.attrs.src);
}
return;
return false;
}
if (!node.content.size) {
return;
return false;
}
node.content.descendants(findImages);
}
return true;
});
findImages(value);
return images;
}