mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-17 17:33:40 -05:00
feat: add new tags (#16)
This commit is contained in:
committed by
Nathaniel Landau
parent
17985615b3
commit
d94d9f2197
@@ -110,17 +110,18 @@ def main(
|
||||
[bold underline]Filter Notes in Scope[/]
|
||||
Limit the scope of notes to be processed with one or more filters.
|
||||
• Path filter (regex): Limit scope based on the path or filename
|
||||
• Metadata Filter: Limit scope based on a key or key/value pair
|
||||
• Tag Filter: Limit scope based on an in-text tag
|
||||
• List and Clear Filters: List all current filters and clear one or all
|
||||
• Metadata filter: Limit scope based on a key or key/value pair
|
||||
• Tag filter: Limit scope based on an in-text tag
|
||||
• List and clear filters: List all current filters and clear one or all
|
||||
• List notes in scope: List notes that will be processed.
|
||||
|
||||
[bold underline]Add Metadata[/]
|
||||
Add new metadata to your vault.
|
||||
• Add metadata to the frontmatter
|
||||
• Add to inline metadata - Set `insert_location` in the config to
|
||||
• Add new metadata to the frontmatter
|
||||
• Add new inline metadata - Set `insert_location` in the config to
|
||||
control where the new metadata is inserted. (Default: Bottom)
|
||||
• [dim]Add to inline tag (Not yet implemented)[/]
|
||||
• Add new inline tag - Set `insert_location` in the config to
|
||||
control where the new tag is inserted. (Default: Bottom)
|
||||
|
||||
[bold underline]Rename Metadata[/]
|
||||
Rename either a key and all associated values, a specific value within a key. or an in-text tag.
|
||||
|
||||
@@ -104,7 +104,19 @@ class Application:
|
||||
alerts.success(f"Added metadata to {num_changed} notes")
|
||||
|
||||
case MetadataType.TAGS:
|
||||
alerts.warning(f"Adding metadata to {area} is not supported yet")
|
||||
tag = self.questions.ask_new_tag()
|
||||
if tag is None: # pragma: no cover
|
||||
return
|
||||
|
||||
num_changed = self.vault.add_metadata(
|
||||
area=area, value=tag, location=self.vault.insert_location
|
||||
)
|
||||
|
||||
if num_changed == 0: # pragma: no cover
|
||||
alerts.warning(f"No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(f"Added metadata to {num_changed} notes")
|
||||
case _: # pragma: no cover
|
||||
return
|
||||
|
||||
|
||||
@@ -401,7 +401,7 @@ class InlineMetadata:
|
||||
"""
|
||||
return f"InlineMetadata(inline_metadata={self.dict})"
|
||||
|
||||
def add(self, key: str, value: str | list[str] = None) -> bool:
|
||||
def add(self, key: str, value: str = None) -> bool:
|
||||
"""Add a key and value to the inline metadata.
|
||||
|
||||
Args:
|
||||
@@ -411,15 +411,12 @@ class InlineMetadata:
|
||||
Returns:
|
||||
bool: True if the metadata was added
|
||||
"""
|
||||
if value is None:
|
||||
if value is None or value == "" or value == "None":
|
||||
if key not in self.dict:
|
||||
self.dict[key] = []
|
||||
return True
|
||||
return False
|
||||
|
||||
if isinstance(value, list):
|
||||
value = value[0]
|
||||
|
||||
if key not in self.dict:
|
||||
self.dict[key] = [value]
|
||||
return True
|
||||
@@ -564,6 +561,23 @@ class InlineTags:
|
||||
)
|
||||
)
|
||||
|
||||
def add(self, new_tag: str) -> bool:
|
||||
"""Add a new inline tag.
|
||||
|
||||
Args:
|
||||
new_tag (str): Tag to add.
|
||||
|
||||
Returns:
|
||||
bool: True if a tag was added.
|
||||
"""
|
||||
if new_tag in self.list:
|
||||
return False
|
||||
|
||||
new_list = self.list.copy()
|
||||
new_list.append(new_tag)
|
||||
self.list = sorted(new_list)
|
||||
return True
|
||||
|
||||
def contains(self, tag: str, is_regex: bool = False) -> bool:
|
||||
"""Check if a tag exists in the metadata.
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ class Note:
|
||||
def add_metadata(
|
||||
self,
|
||||
area: MetadataType,
|
||||
key: str,
|
||||
key: str = None,
|
||||
value: str | list[str] = None,
|
||||
location: InsertLocation = None,
|
||||
) -> bool:
|
||||
@@ -128,7 +128,7 @@ class Note:
|
||||
|
||||
Args:
|
||||
area (MetadataType): Area to add metadata to.
|
||||
key (str): Key to add.
|
||||
key (str, optional): Key to add
|
||||
location (InsertLocation, optional): Location to add inline metadata and tags.
|
||||
value (str, optional): Value to add.
|
||||
|
||||
@@ -140,7 +140,7 @@ class Note:
|
||||
return True
|
||||
|
||||
try:
|
||||
if area is MetadataType.INLINE and self.inline_metadata.add(key, value):
|
||||
if area is MetadataType.INLINE and self.inline_metadata.add(key, str(value)):
|
||||
line = f"{key}:: " if value is None else f"{key}:: {value}"
|
||||
self.insert(new_string=line, location=location)
|
||||
return True
|
||||
@@ -149,9 +149,10 @@ class Note:
|
||||
log.warning(f"Could not add metadata to {self.note_path}: {e}")
|
||||
return False
|
||||
|
||||
if area is MetadataType.TAGS:
|
||||
# TODO: implement adding to intext tags
|
||||
pass
|
||||
if area is MetadataType.TAGS and self.inline_tags.add(str(value)):
|
||||
line = f"#{value}"
|
||||
self.insert(new_string=line, location=location)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@@ -436,8 +436,12 @@ class Questions:
|
||||
question, validate=self._validate_new_key, style=self.style, qmark="INPUT |"
|
||||
).ask()
|
||||
|
||||
def ask_new_tag(self, question: str = "New tag name") -> str: # pragma: no cover
|
||||
"""Ask the user for a new inline tag."""
|
||||
def ask_new_tag(self, question: str = "Enter a new tag") -> str: # pragma: no cover
|
||||
"""Ask the user for a new tag.
|
||||
|
||||
Args:
|
||||
question (str, optional): The question to ask. Defaults to "Enter a new tag".
|
||||
"""
|
||||
return questionary.text(
|
||||
question, validate=self._validate_new_tag, style=self.style, qmark="INPUT |"
|
||||
).ask()
|
||||
|
||||
@@ -165,7 +165,7 @@ class Vault:
|
||||
def add_metadata(
|
||||
self,
|
||||
area: MetadataType,
|
||||
key: str,
|
||||
key: str = None,
|
||||
value: str | list[str] = None,
|
||||
location: InsertLocation = None,
|
||||
) -> int:
|
||||
@@ -186,7 +186,7 @@ class Vault:
|
||||
num_changed = 0
|
||||
|
||||
for _note in self.notes_in_scope:
|
||||
if _note.add_metadata(area, key, value, location):
|
||||
if _note.add_metadata(area=area, key=key, value=value, location=location):
|
||||
num_changed += 1
|
||||
|
||||
if num_changed > 0:
|
||||
|
||||
Reference in New Issue
Block a user