from pathlib import Path
from watchfiles import watch, Change
from blog.loader import load_single_post
from blog.models import Post
CONTENT_DIR = Path("content/blog").resolve()
def watch_content():
print("Watching content/blog for changes...")
for changes in watch(CONTENT_DIR):
for change, path in changes:
path = Path(path)
if path.suffix != ".md":
continue
rel_path = str(path.resolve().relative_to(Path.cwd()))
if change in {Change.added, Change.modified}:
print(f"Reload: {rel_path}")
load_single_post(path)
elif change == Change.deleted:
Post.objects.filter(markdown_path=rel_path).delete()
print(f"Removed: {rel_path}")