Files
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

41 lines
849 B
TypeScript

import type { Node } from "prosemirror-model";
import { parser } from "@server/editor";
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 = new Map<string, ImageProps>();
if (!doc) {
return [];
}
doc.descendants((node: Node) => {
if (node.type.name === "image") {
if (!images.has(node.attrs.src)) {
images.set(node.attrs.src, {
src: node.attrs.src,
alt: node.attrs.alt,
});
}
return false;
}
if (!node.content.size) {
return false;
}
return true;
});
return Array.from(images.values());
}