29 lines
747 B
Python
29 lines
747 B
Python
import frontmatter
|
|
import mistune
|
|
from pathlib import Path
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
mistune_render = mistune.create_markdown(
|
|
renderer=mistune.HTMLRenderer(escape=False),
|
|
plugins=["strikethrough", "footnotes", "table", "task_lists", "abbr", "math"],
|
|
)
|
|
|
|
|
|
def render_markdown(path: Path):
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
obj = frontmatter.load(f)
|
|
content = mistune_render(obj.content)
|
|
return content, obj.metadata, obj
|
|
|
|
|
|
def read_raw_markdown(path: Path) -> str:
|
|
with open(path, "r") as f:
|
|
obj = frontmatter.load(f)
|
|
return obj.content
|
|
|
|
|
|
def rendered_markdown_to_plain_text(html):
|
|
text = "\n\n".join(BeautifulSoup(html, features="html.parser").stripped_strings)
|
|
return text
|