Skip to content

Commit

Permalink
feat: handling multiple heading for different platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
LinceMathew committed Feb 6, 2024
1 parent 25f01a1 commit 7ba8cd8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
50 changes: 47 additions & 3 deletions glee.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from bs4 import BeautifulSoup
import os
import shutil
import json

from datetime import datetime as date
from handle_config import (
Expand Down Expand Up @@ -264,9 +265,10 @@ def post_to_ghost(meta, md):
)

return

inject_multi_titles(meta)
meta = add_blog_configurations(meta)
meta["html"] = to_html(md)

token = get_jwt()

headers = {"Authorization": "Ghost {}".format(token)}
Expand Down Expand Up @@ -301,19 +303,61 @@ def add_blog_configurations(meta):
theme = select_codehilite_theme(code_theme)

if side_bar_toc:
meta["codeinjection_head"] = (
meta["codeinjection_head"] += (
f"""<style>{default_style+theme}</style>""" + sidebar_toc_head
)
meta["codeinjection_foot"] = sidebar_toc_footer
else:
meta["codeinjection_head"] = f"""<style>{default_style+theme}</style>"""
meta["codeinjection_head"] += f"""<style>{default_style+theme}</style>"""

return meta

except Exception as e:
sys.exit(f"""Error: {e}""")


def inject_multi_titles(meta):
meta["codeinjection_head"] = ""
try:
title_data = meta["title"]

if isinstance(title_data, str): # Title is a string
pass
elif isinstance(title_data, dict): # Title is a dictionary
default_title = title_data.get("default")

if "default" not in title_data:
raise KeyError("Missing 'default' key in title_data")

meta["title"] = default_title

title_data_str = json.dumps(title_data)

meta[
"codeinjection_head"
] = f"""<script>
document.addEventListener("DOMContentLoaded", function() {{
const urlParams = new URLSearchParams(window.location.search);
const articleTitleElement = document.querySelector('.article-title');
const title = {title_data_str};
if (urlParams.has('src')) {{
const srcValue = urlParams.get('src');
if (title[srcValue] !== undefined) {{
articleTitleElement.textContent = title[srcValue];
document.title = title[srcValue];
}}
}}
}});
</script>"""

except KeyError:
sys.exit(f"""Error: missing default title""")

except Exception as e:
logging.error("error in title updating", e)
pass


if __name__ == "__main__":
post = frontmatter.load(args.markdown_file)
post_to_ghost(post.metadata, post.content)
51 changes: 48 additions & 3 deletions sample_post.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
---
title: 'testing sample markdown file'
title:
default: new default title
hn: title from glee to HN
reddit: title from glee to reddit

authors:
- sample@gmail.com
tags: ["draft"]
featured: false
status: draft
custom_excerpt: This is a customexcerpt
custom_excerpt: This is a custom excerpt
feature_image: ./test_images/Animhorse.gif
# sidebar_toc: false
# code_hilite_theme: vim
Expand Down Expand Up @@ -262,4 +266,45 @@ def hello_world():
</a>
</div>
</div>
</div>
</div>


<style>
.article-title {
opacity: 0;
filter: blur(3px);
animation: fadeIn 1s ease 1s forwards;
}

@keyframes fadeIn {
to {
opacity: 1;
filter: blur(0px);
}
}
</style>


<script>
document.addEventListener("DOMContentLoaded", function() {
const urlParams = new URLSearchParams(window.location.search);
const articleTitleElement = document.querySelector('.article-title');
if (urlParams.has('src')) {
const srcValue = urlParams.get('src');

switch (srcValue) {
case 'hn':
articleTitleElement.textContent = 'New Title for HN';
document.title = 'New Title for HN'
break;
case 'reddit':
articleTitleElement.textContent = 'New Title for Reddit';
document.title = 'New Title for Reddit';
break;
default:
break;
}

}
});
</script>

0 comments on commit 7ba8cd8

Please sign in to comment.