feature: make md parser service more generic
This commit is contained in:
parent
b023531dc8
commit
13cc17eed4
1 changed files with 7 additions and 8 deletions
|
@ -7,28 +7,27 @@ import frontmatter
|
||||||
class ParseMarkdownService:
|
class ParseMarkdownService:
|
||||||
"""Extract tags, internal links and body text from Markdown entries"""
|
"""Extract tags, internal links and body text from Markdown entries"""
|
||||||
|
|
||||||
def __init__(self, file):
|
def __init__(self):
|
||||||
self.file = file
|
pass
|
||||||
|
|
||||||
def __get_internal_links(self):
|
def __get_internal_links(self, file):
|
||||||
link_rgx = r"\[.*?\]\(([^)]+\.md)\)"
|
link_rgx = r"\[.*?\]\(([^)]+\.md)\)"
|
||||||
with open(self.file, "r") as f:
|
with open(file, "r") as f:
|
||||||
internal_links = []
|
internal_links = []
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
internal_link = re.findall(link_rgx, line)
|
internal_link = re.findall(link_rgx, line)
|
||||||
if internal_link:
|
if internal_link:
|
||||||
# internal_links.append(internal_link)
|
|
||||||
internal_links.append(
|
internal_links.append(
|
||||||
[os.path.basename(link) for link in internal_link]
|
[os.path.basename(link) for link in internal_link]
|
||||||
)
|
)
|
||||||
return [item for row in internal_links for item in row]
|
return [item for row in internal_links for item in row]
|
||||||
|
|
||||||
def parse(self):
|
def parse(self, markdown_file):
|
||||||
with open(self.file) as f:
|
with open(markdown_file) as f:
|
||||||
metadata, content = frontmatter.parse(f.read())
|
metadata, content = frontmatter.parse(f.read())
|
||||||
return {
|
return {
|
||||||
"tags": metadata.get("tags", []),
|
"tags": metadata.get("tags", []),
|
||||||
"body": content or "",
|
"body": content or "",
|
||||||
"links": self.__get_internal_links() or [],
|
"links": self.__get_internal_links(markdown_file) or [],
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue