def update_item_frontmatter(dataobj_id, new_frontmatter): """ Given an object id, this method overwrites the front matter of the post with `new_frontmatter`. --- date: Str id: Str path: Str tags: List[Str] title: Str type: note/bookmark --- """ from archivy.models import DataObj filename = get_by_id(dataobj_id) dataobj = frontmatter.load(filename) for key in list(new_frontmatter): dataobj[key] = new_frontmatter[key] md = frontmatter.dumps(dataobj) with open(filename, "w", encoding="utf-8") as f: f.write(md) converted_dataobj = DataObj.from_md(md) converted_dataobj.fullpath = str( filename.relative_to(current_app.config["USER_DIR"]) ) converted_dataobj.index() load_hooks().on_edit(converted_dataobj)
def update_item_md(dataobj_id, new_content): """ Given an object id, this method overwrites the inner content of the post with `new_content`. This means that it won't change the frontmatter (eg tags, id, title) but it can change the file content. For example: If we have a dataobj like this: ```md --- id: 1 title: Note --- # This is random ``` Calling `update_item(1, "# This is specific")` will turn it into: ```md --- id: 1 # unchanged title: Note --- # This is specific ``` """ from archivy.models import DataObj filename = get_by_id(dataobj_id) dataobj = frontmatter.load(filename) dataobj.content = new_content md = frontmatter.dumps(dataobj) with open(filename, "w", encoding="utf-8") as f: f.write(md) converted_dataobj = DataObj.from_md(md) converted_dataobj.fullpath = str( filename.relative_to(current_app.config["USER_DIR"]) ) converted_dataobj.index() load_hooks().on_edit(converted_dataobj)
def index(): data_dir = Path(app.config["USER_DIR"]) / "data" if not app.config["SEARCH_CONF"]["enabled"]: click.echo("Search must be enabled for this command.") return for filename in data_dir.rglob("*.md"): cur_file = open(filename) dataobj = DataObj.from_md(cur_file.read()) cur_file.close() if dataobj.index(): click.echo(f"Indexed {dataobj.title}...") else: click.echo(f"Failed to index {dataobj.title}")