mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
add title/folder to EntitySchema
This commit is contained in:
@@ -14,6 +14,7 @@ The file format has two distinct types of content:
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from collections import OrderedDict
|
||||
|
||||
import frontmatter
|
||||
from frontmatter import Post
|
||||
@@ -99,21 +100,16 @@ class MarkdownProcessor:
|
||||
if current_checksum != expected_checksum:
|
||||
raise DirtyFileError(f"File {path} has been modified")
|
||||
|
||||
# Convert frontmatter to dict, dropping None values
|
||||
# Convert frontmatter to dict
|
||||
frontmatter_dict = OrderedDict()
|
||||
frontmatter_dict["title"] = markdown.frontmatter.title
|
||||
frontmatter_dict["type"] = markdown.frontmatter.type
|
||||
frontmatter_dict["permalink"] = markdown.frontmatter.permalink
|
||||
|
||||
metadata = markdown.frontmatter.metadata or {}
|
||||
frontmatter_dict = {
|
||||
"type": markdown.frontmatter.type,
|
||||
"permalink": markdown.frontmatter.permalink,
|
||||
"created": markdown.frontmatter.created.isoformat()
|
||||
if markdown.created
|
||||
else None,
|
||||
"modified": markdown.frontmatter.modified.isoformat()
|
||||
if markdown.modified
|
||||
else None,
|
||||
**metadata,
|
||||
}
|
||||
frontmatter_dict = {k: v for k, v in frontmatter_dict.items() if v is not None}
|
||||
|
||||
for k,v in metadata.items():
|
||||
frontmatter_dict[k] = v
|
||||
|
||||
# Start with user content (or minimal title for new files)
|
||||
content = markdown.content or f"# {markdown.frontmatter.title}\n"
|
||||
|
||||
@@ -131,7 +127,7 @@ class MarkdownProcessor:
|
||||
|
||||
# Create Post object for frontmatter
|
||||
post = Post(content, **frontmatter_dict)
|
||||
final_content = frontmatter.dumps(post)
|
||||
final_content = frontmatter.dumps(post, sort_keys=False)
|
||||
|
||||
logger.debug(f"writing file {path} with content:\n{final_content}")
|
||||
|
||||
|
||||
@@ -29,11 +29,9 @@ def entity_model_to_markdown(entity: Entity, content: Optional[str] = None) -> E
|
||||
:rtype: EntityMarkdown
|
||||
"""
|
||||
metadata = entity.entity_metadata or {}
|
||||
metadata["permalink"] = entity.permalink
|
||||
metadata["type"] = entity.entity_type or "note"
|
||||
metadata["title"] = entity.title
|
||||
metadata["created"] = entity.created_at
|
||||
metadata["modified"] = entity.updated_at
|
||||
metadata["permalink"] = entity.permalink
|
||||
|
||||
# convert model to markdown
|
||||
entity_observations = [
|
||||
@@ -80,6 +78,8 @@ def entity_model_to_markdown(entity: Entity, content: Optional[str] = None) -> E
|
||||
content=content,
|
||||
observations=observations,
|
||||
relations=relations,
|
||||
created = entity.created_at,
|
||||
modified = entity.updated_at,
|
||||
)
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ def entity_model_from_markdown(file_path: Path, markdown: EntityMarkdown, entity
|
||||
permalink = markdown.frontmatter.permalink or generate_permalink(file_path)
|
||||
model = entity or Entity()
|
||||
|
||||
model.title=markdown.frontmatter.title or file_path.stem
|
||||
model.title=markdown.frontmatter.title
|
||||
model.entity_type=markdown.frontmatter.type
|
||||
model.permalink=permalink
|
||||
model.file_path=str(file_path)
|
||||
@@ -128,14 +128,17 @@ async def schema_to_markdown(schema):
|
||||
:param schema: the schema to convert
|
||||
:return: Post
|
||||
"""
|
||||
# Add metadata to dict
|
||||
frontmatter_dict = schema.entity_metadata or {}
|
||||
|
||||
# set permalink and type
|
||||
frontmatter_dict["permalink"] = schema.permalink
|
||||
frontmatter_dict["type"] = schema.entity_type
|
||||
|
||||
# Create Post object
|
||||
content = schema.content or ""
|
||||
post = Post(content, **frontmatter_dict)
|
||||
frontmatter_metadata = schema.entity_metadata or {}
|
||||
|
||||
# remove from map so we can define ordering in frontmatter
|
||||
if "type" in frontmatter_metadata:
|
||||
del frontmatter_metadata["type"]
|
||||
if "title" in frontmatter_metadata:
|
||||
del frontmatter_metadata["title"]
|
||||
if "permalink" in frontmatter_metadata:
|
||||
del frontmatter_metadata["permalink"]
|
||||
|
||||
post = Post(content, title=schema.title, type=schema.entity_type, permalink=schema.permalink, **frontmatter_metadata)
|
||||
return post
|
||||
|
||||
Reference in New Issue
Block a user