fix: Handle base64 and remote images when creating a file (#5740)
This commit is contained in:
@@ -1,17 +1,29 @@
|
||||
import { Node } from "prosemirror-model";
|
||||
import { parser } from "@server/editor";
|
||||
|
||||
export default function parseImages(text: string): string[] {
|
||||
type ImageProps = { src: string; alt: string };
|
||||
|
||||
/**
|
||||
* Parses a string of markdown and returns a list of images.
|
||||
*
|
||||
* @param text The markdown to parse
|
||||
* @returns A unique list of images
|
||||
*/
|
||||
export default function parseImages(text: string): ImageProps[] {
|
||||
const doc = parser.parse(text);
|
||||
const images: string[] = [];
|
||||
const images = new Map<string, ImageProps>();
|
||||
|
||||
if (!doc) {
|
||||
return images;
|
||||
return [];
|
||||
}
|
||||
|
||||
doc.descendants((node: Node) => {
|
||||
if (node.type.name === "image") {
|
||||
if (!images.includes(node.attrs.src)) {
|
||||
images.push(node.attrs.src);
|
||||
if (!images.has(node.attrs.src)) {
|
||||
images.set(node.attrs.src, {
|
||||
src: node.attrs.src,
|
||||
alt: node.attrs.alt,
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -24,5 +36,5 @@ export default function parseImages(text: string): string[] {
|
||||
return true;
|
||||
});
|
||||
|
||||
return images;
|
||||
return Array.from(images.values());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user