feature: make md parser service more generic

This commit is contained in:
thomasabishop 2024-11-11 14:45:46 +00:00
parent b023531dc8
commit 13cc17eed4

View file

@ -7,28 +7,27 @@ import frontmatter
class ParseMarkdownService:
"""Extract tags, internal links and body text from Markdown entries"""
def __init__(self, file):
self.file = file
def __init__(self):
pass
def __get_internal_links(self):
def __get_internal_links(self, file):
link_rgx = r"\[.*?\]\(([^)]+\.md)\)"
with open(self.file, "r") as f:
with open(file, "r") as f:
internal_links = []
lines = f.readlines()
for line in lines:
internal_link = re.findall(link_rgx, line)
if internal_link:
# internal_links.append(internal_link)
internal_links.append(
[os.path.basename(link) for link in internal_link]
)
return [item for row in internal_links for item in row]
def parse(self):
with open(self.file) as f:
def parse(self, markdown_file):
with open(markdown_file) as f:
metadata, content = frontmatter.parse(f.read())
return {
"tags": metadata.get("tags", []),
"body": content or "",
"links": self.__get_internal_links() or [],
"links": self.__get_internal_links(markdown_file) or [],
}