Skip to content

Commit

Permalink
Test markdown changes
Browse files Browse the repository at this point in the history
  • Loading branch information
liraymond04 committed Dec 9, 2024
1 parent 1e48046 commit 0fec307
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: webnovel
title: Stand by me Pleiades Part 4
repo_url: liraymond04/re0-translations
file_path: Stand by me Pleiades Part 4.md
file_path: posts/side_stories/Stand by me Pleiades Part 4/Stand by me Pleiades Part 4.md
supabase_page_format: true
---
<div style="text-align:center">
Expand Down
4 changes: 3 additions & 1 deletion scripts/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
SUPABASE_PAGE_FORMAT = True
REPO_URL = "liraymond04/re0-translations"

cur_dir = os.getcwd()

def generate_yaml_frontmatter(md_file_path):
title = os.path.splitext(os.path.basename(md_file_path))[0]

file_path = os.path.relpath(md_file_path, start=os.getcwd())
file_path = os.path.relpath(md_file_path, start=cur_dir)

frontmatter = f"""---
layout: {LAYOUT}
Expand Down
28 changes: 17 additions & 11 deletions scripts/process_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def extract_yaml_frontmatter(text):
else:
raise ValueError("No YAML frontmatter found")

def remove_yaml_frontmatter(text):
pattern = r"^---\n.*?\n---\n"
content_without_frontmatter = re.sub(pattern, "", text, flags=re.DOTALL)
return content_without_frontmatter

def get_added_files_from_env():
try:
added_files_env = os.getenv("ADDED_MARKDOWN_FILES")
Expand Down Expand Up @@ -68,21 +73,22 @@ def get_changed_files_from_env():
logger.error(f"Error parsing changed markdown files: {e}")
sys.exit(1)

def check(response, metadata, string):
return response[string] == metadata[string]
def check(response, metadata, field):
return response[field] == metadata[field]

def update_post_field_in_db(metadata, field):
file = metadata["file_path"] or ""
try:
_ = (
supabase.table("posts")
.update({field: metadata[field]})
.eq("id", metadata["id"])
.eq("repo_url", metadata["repo_url"])
.eq("file_path", metadata["file_path"])
.execute()
)
logger.info(f"Successfully updated post for {file}")
logger.info(f"Successfully updated {field} in post for {file}:\n{metadata[field]}")
except Exception as e:
logger.error(f"Error updating post in database for {file}: {e}")
logger.error(f"Error updating {field} in post for {file}: {e}")
sys.exit(1)

def create_new_post(file, metadata):
Expand Down Expand Up @@ -127,13 +133,15 @@ def check_if_differences_exist(file, metadata):
.eq("repo_url", metadata["repo_url"])
.eq("file_path", metadata["file_path"])
.single()
.execute()
)

fields = ["title", "content", "tags", "keywords", "updated_at", "created_at"]

for field in fields:
if not check(response, metadata, field):
update_post_field_in_db(metadata, field)
if field in metadata:
if not check(response.data, metadata, field):
update_post_field_in_db(metadata, field)
except Exception as e:
logger.error(f"Error checking differences for {file}: {e}")
sys.exit(1)
Expand All @@ -144,7 +152,7 @@ def read_markdown_metadata(file_path):
content = f.read()

ret = extract_yaml_frontmatter(content)
ret["content"] = content
ret["content"] = remove_yaml_frontmatter(content)
return ret
except FileNotFoundError:
logger.error(f"Markdown file not found: {file_path}")
Expand All @@ -164,7 +172,6 @@ def main():
added_files = get_added_files_from_env()
if not added_files:
logger.info("No markdown files were added.")
return

for file in added_files:
if file.endswith('.md'):
Expand All @@ -178,7 +185,6 @@ def main():
changed_files = get_changed_files_from_env()
if not changed_files:
logger.info("No markdown files were changed.")
return

for file in changed_files:
if file.endswith('.md'):
Expand All @@ -192,14 +198,14 @@ def main():
deleted_files = get_deleted_files_from_env()
if not deleted_files:
logger.info("No markdown files were deleted.")
return

for file in deleted_files:
if file.endswith('.md'):
logger.info(f"Processing {file}")
metadata = read_markdown_metadata(file)
# print("Markdown files were deleted.")
# print(metadata)
delete_post(file, metadata)

except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
Expand Down

0 comments on commit 0fec307

Please sign in to comment.