mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-18 09:53:40 -05:00
feat: add new tags (#16)
This commit is contained in:
committed by
Nathaniel Landau
parent
17985615b3
commit
d94d9f2197
@@ -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