From 412081f1f94c7709e72dc64a1bfd5c4f5a164deb Mon Sep 17 00:00:00 2001 From: redatman Date: Sat, 3 Aug 2024 22:41:00 +0800 Subject: [PATCH] feat: Add body property to Note class Adds a new `body` property to the `Note` class, allowing users to easily access the note's body content. The property extracts the body from the note's content, splitting it at the first newline. This improves the usability and clarity of the `Note` class. --- lib/models.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/models.py b/lib/models.py index f88be87..35b55d1 100644 --- a/lib/models.py +++ b/lib/models.py @@ -228,7 +228,8 @@ def _title(self): content = self._content except Exception: return CONFIG.SIMPLENOTE_DEFAULT_NOTE_TITLE - return self.get_title(content) + title, _ = self.get_title_body(content) + return title @property def title(self): @@ -236,16 +237,26 @@ def title(self): content = self.d.content except Exception: return CONFIG.SIMPLENOTE_DEFAULT_NOTE_TITLE - return self.get_title(content) + title, _ = self.get_title_body(content) + return title + + @property + def body(self) -> str: + _, body = self.get_title_body(self.d.content) + return body @staticmethod - def get_title(content: str) -> str: - index = content.find("\n") - if index > -1: - title = content[:index] + def get_title_body(content: str) -> tuple[str, str]: + if not isinstance(content, str): + return CONFIG.SIMPLENOTE_DEFAULT_NOTE_TITLE, "empty body" + _content = content.split("\n", 1) + title, body = CONFIG.SIMPLENOTE_DEFAULT_NOTE_TITLE, "empty body" + if len(_content) == 1: + title = _content[0] or title else: - title = content or CONFIG.SIMPLENOTE_DEFAULT_NOTE_TITLE - return title + title = _content[0] or title + body = _content[1] or body + return title, body @property def _filename(self) -> str: