Skip to content

Commit c738711

Browse files
committed
Handle missing attachment
1 parent e194461 commit c738711

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

confluence_markdown_exporter/confluence.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ def export_attachments(self, export_path: StrPath) -> None:
384384
def get_attachment_by_file_id(self, file_id: str) -> Attachment:
385385
return next(attachment for attachment in self.attachments if attachment.file_id == file_id)
386386

387-
def get_attachment_by_title(self, title: str) -> Attachment:
388-
return next(attachment for attachment in self.attachments if attachment.title == title)
387+
def get_attachments_by_title(self, title: str) -> list[Attachment]:
388+
return [attachment for attachment in self.attachments if attachment.title == title]
389389

390390
@classmethod
391391
def from_json(cls, data: JsonResponse) -> "Page":
@@ -701,13 +701,17 @@ def convert_drawio(self, el: BeautifulSoup, text: str, parent_tags: list[str]) -
701701
if match := re.search(r"\|diagramName=(.+?)\|", str(el)):
702702
drawio_name = match.group(1)
703703
preview_name = f"{drawio_name}.png"
704-
drawio_attachment = self.page.get_attachment_by_title(drawio_name)
705-
preview_attachment = self.page.get_attachment_by_title(preview_name)
704+
drawio_attachments = self.page.get_attachments_by_title(drawio_name)
705+
preview_attachments = self.page.get_attachments_by_title(preview_name)
706+
707+
if not drawio_attachments or not preview_attachments:
708+
return f"\n<!-- Drawio diagram `{drawio_name}` not found -->\n\n"
709+
706710
drawio_relpath = os.path.relpath(
707-
drawio_attachment.export_path.filepath, self.page.export_path.dirpath
711+
drawio_attachments[0].export_path.filepath, self.page.export_path.dirpath
708712
)
709713
preview_relpath = os.path.relpath(
710-
preview_attachment.export_path.filepath, self.page.export_path.dirpath
714+
preview_attachments[0].export_path.filepath, self.page.export_path.dirpath
711715
)
712716

713717
drawio_image_embedding = f"![{drawio_name}]({preview_relpath.replace(' ', '%20')})"

0 commit comments

Comments
 (0)