Skip to content

Commit

Permalink
feat: Add body property to Note class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RedAtman committed Aug 3, 2024
1 parent 6838b0d commit 412081f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,35 @@ 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):
try:
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:
Expand Down

0 comments on commit 412081f

Please sign in to comment.