Skip to content

note

This module contains the classes that represent Obsidian notes. It provides classes for reading, writing, and retrieving properties of Obsidian notes.

Note(path: str, content: Optional[str] = None)

Represents a note in Obsidian vault.

Parameters:

  • path (str) –

    The path of the note.

  • content (str, default: None ) –

    The content of the note. If not provided, it will be read from the note file.

Source code in pyobsidian/note.py
360
361
362
363
364
def __init__(self: Self, path: str, content: Optional[str] = None):
    if content is None:
        content = read_file(path)
    self.__path = path
    self.__properties = NoteProperties(path, content)

path: str property

Retrieves the normalized path of the note.

Returns:

  • str

    The normalized path of the note.

properties: NoteProperties property

Retrieves the properties of the note.

Returns:

read() -> str

Reads the content of a note from the file.

Returns:

  • str

    The content of the note file

Source code in pyobsidian/note.py
399
400
401
402
403
404
405
406
407
def read(self: Self) -> str:
    """Reads the content of a note from the file.

    Returns
    -------
    str
        The content of the note file
    """
    return read_file(self.path)

write() -> str

Writes the content of a note to the file.

Returns:

  • str

    The path of the written note

Source code in pyobsidian/note.py
409
410
411
412
413
414
415
416
417
418
419
def write(self: Self) -> str:
    """Writes the content of a note to the file.

    Returns
    -------
    str
        The path of the written note
    """
    with open(self.path, 'w', encoding='utf8') as file:
        file.write(self.properties.content)
    return self.path

NoteProperties(path: str, content: str)

A class used to retrieve properties of Obsidian notes.

Parameters:

  • path (str) –

    The path of the note file

  • content (str) –

    The content of the note file

Source code in pyobsidian/note.py
26
27
28
def __init__(self: Self, path: str, content: str):
    self.__path = path
    self.__content = content

content: str property writable

The content of the note file

Returns:

  • str

    The content of the note file

creation_time: datetime property

Retrieves the creation time of the note file.

Notes

It uses the os.path.getctime function to retrieve the creation time of the file.

Returns:

  • datetime

    The creation time of the note file.

filename: str property

Returns the filename of the current object's path.

This property takes the path of the current object and splits it using the operating system's path separator. It then extracts the last element of the resulting list, which represents the filename. The filename is further split using the dot separator to remove the file extension. The function then returns the extracted filename as a string.

Returns:

  • str

    Note filename.

folder: list[str] property

Retrieves the folders path of the current note as a list of strings. All combinations of the folder path are returned. For example, if the folder path is "C:/Users/user/vault", it returns ['C:', 'C:/Users', 'C:/Users/user', 'C:/Users/user/vault']

Returns:

  • list[str]

    A list containing the folders path of the current note.

last_access_time: datetime property

Retrieves the last access time of the file.

Notes

It uses the os.path.getatime function to retrieve the last access time of the file.

Returns:

  • datetime

    The last access time of the file.

last_modification_time: datetime property

Retrieves the last modification time of the file.

Notes

It uses the os.path.getmtime function to retrieve the last modification time of the file.

Returns:

  • datetime

    The last modification time of the file.

path: str property

The path of the note file

Returns:

  • str

    The path of the note file

related_note: dict[str, Optional[list[str]] | Optional[dict[str, list[str]]]] property

Retrieves related notes from both YAML content and inline content.

This property returns a dictionary containing the related notes extracted from both the YAML content and inline content of the note. The dictionary has two keys: - 'yaml': The related notes extracted from the YAML content of the note. It is a dictionary where the keys are the YAML keys and the values are lists of related note names. - 'inline': The related notes extracted from the inline content of the note. It is a list of related note names.

Returns:

  • dict[str, Union[list[str], dict[str, list[str]]]]

    A dictionary containing the related notes extracted from both the YAML content and inline content of the note.

tag: dict[str, Optional[list[str]]] property

Retrieves the tags from the note.

This property returns a dictionary containing the tags extracted from the note. The dictionary has two keys:

  • 'yaml': The tags extracted from the YAML content of the note. It is a list of strings or None if no tags are found.
  • 'inline': The tags extracted from the inline content of the note. It is a list of strings or None if no tags are found.

Returns:

  • dict[str, Optional[list[str]]]

    A dictionary containing the tags extracted from the note.

yaml_content: Optional[dict] property

Retrieves the YAML content from the note. The YAML is identified by '---'. All text between these two delimiters is considered part of the YAML content. If more than one '---' separator is found, only the first pair is considered.

Notes

See Obsidian properties for more details.

Returns:

  • Optional[dict]

    The YAML content of the note, or None if no YAML content is found.

Retrieves all the related notes that are inline in the content of the note.

This function uses regular expressions to find all occurrences of related notes in the content of the note. The related notes are identified by the pattern [[note|alias]], where 'note' is the name of the related note and 'alias' is an optional alias for the note. The function removes the alias from the related note and returns a list of the related notes without aliases.

Returns:

  • Optional[list[str]]

    A list of related notes found in the content, without aliases, or None if no related notes are found.

Source code in pyobsidian/note.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def _get_related_notes_inline(self: Self) -> Optional[list[str]]:
    """Retrieves all the related notes that are inline in the content of the note.

    This function uses regular expressions to find all occurrences of related notes in the content of the note.
    The related notes are identified by the pattern [[note|alias]], where 'note' is the name of the related note and 'alias' is an optional alias for the note.
    The function removes the alias from the related note and returns a list of the related notes without aliases.

    Returns
    -------
    Optional[list[str]]
        A list of related notes found in the content, without aliases, or None if no related notes are found.
    """
    re_compile = re.compile(r'---(.*?)---', re.DOTALL)
    raw_content = re.sub(re_compile, '', self.content)
    regex = re.compile(r'\[\[(.*?)\]\]')
    related_notes = regex.findall(raw_content)
    if len(related_notes) == 0:
        return None
    related_notes_without_alias = [
        note.split('|')[0] 
        for note in related_notes
    ]
    return related_notes_without_alias

Retrieves related notes from YAML content.

This function searches for related notes in the YAML content of the note. It uses regular expressions to find all occurrences of related notes in the YAML content. The related notes are identified by the pattern [[note|alias]], where 'note' is the name of the related note and 'alias' is an optional alias for the note. The function removes the alias from the related note and returns a dictionary of the related notes, grouped by the YAML key.

Returns:

  • Optional[dict[str, list[str]]]

    A dictionary of related notes found in the YAML content, grouped by the YAML key, or None if no related notes are found.

Source code in pyobsidian/note.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def _get_related_notes_yaml(self: Self) -> Optional[ dict[str, list[str]] ]:
    """Retrieves related notes from YAML content.

    This function searches for related notes in the YAML content of the note.
    It uses regular expressions to find all occurrences of related notes in the YAML content.
    The related notes are identified by the pattern [[note|alias]], where 'note' is the name of the related note and 'alias' is an optional alias for the note.
    The function removes the alias from the related note and returns a dictionary of the related notes, grouped by the YAML key.

    Returns
    -------
    Optional[dict[str, list[str]]]
        A dictionary of related notes found in the YAML content, grouped by the YAML key, or None if no related notes are found.
    """
    yaml_content = self.yaml_content
    if yaml_content is None:
        return None

    def rec_search(values, compile: re.Pattern = re.compile(r'\[\[(.*?)\]\]')) -> list[str]:
        results = []
        if isinstance(values, str):
            related_notes = re.findall(compile, values)
            related_notes_without_alias = [
                note.split('|')[0] 
                for note in related_notes
            ]
            results.extend(related_notes_without_alias)
        elif isinstance(values, list):
            for value in values:
                results.extend(rec_search(value))
        return results

    related_notes = {}
    for key, values in yaml_content.items():
        related_notes[key] = rec_search(values)

    valid_related_notes = {
        key: values
        for key, values in related_notes.items()
        if len(values) > 0
    }
    if len(valid_related_notes) == 0:
        return None
    return valid_related_notes

_get_tags_inline() -> Optional[list[str]]

Retrieves all the inline tags from the content of the note.

This function uses regular expressions to find all occurrences of tags in the content of the note. The tags are identified by the pattern '#tag', where 'tag' is any non-whitespace sequence of characters.

Returns:

  • Optional[list[str]]

    A list of tags found in the content, or None if no tags are found.

Source code in pyobsidian/note.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def _get_tags_inline(self: Self) -> Optional[list[str]]:
    """Retrieves all the inline tags from the content of the note.

    This function uses regular expressions to find all occurrences of tags in the content of the note.
    The tags are identified by the pattern '#tag', where 'tag' is any non-whitespace sequence of characters.

    Returns
    -------
    Optional[list[str]]
        A list of tags found in the content, or None if no tags are found.
    """
    tags = re.compile(r'#([^\s]+)')
    tags_content = tags.findall(self.content)
    if len(tags_content) == 0:
        return None
    return tags_content

_get_tags_yaml() -> Optional[list[str]]

Retrieves the tags from the YAML content of the note.

Returns:

  • Optional[list[str]]

    The list of tags if they exist in the YAML content, otherwise None.

Source code in pyobsidian/note.py
52
53
54
55
56
57
58
59
60
61
62
63
64
def _get_tags_yaml(self: Self) -> Optional[list[str]]:
    """Retrieves the tags from the YAML content of the note.

    Returns
    -------
    Optional[list[str]]
        The list of tags if they exist in the YAML content, 
        otherwise None.
    """
    yaml_tags = self._get_yaml_field('tags')
    if isinstance(yaml_tags, str):
        yaml_tags = [yaml_tags]
    return yaml_tags

_get_yaml_field(field: str) -> Optional[str | list[str]]

Retrieves the value of a specified field from the YAML content of the note.

Parameters:

  • field (str) –

    The name of the field to retrieve the value of.

Returns:

  • Optional[str | list[str]]

    The value of the specified field if it exists in the YAML content, otherwise None.

Source code in pyobsidian/note.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def _get_yaml_field(self: Self, field: str) -> Optional[str | list[str]]:
    """Retrieves the value of a specified field from the YAML content of the note.

    Parameters
    ----------
    field : str
        The name of the field to retrieve the value of.

    Returns
    -------
    Optional[str | list[str]]
        The value of the specified field if it exists in the YAML content, 
        otherwise None.
    """
    if self.yaml_content is None:
        return None
    field_value = self.yaml_content.get(field)
    return field_value