From 689c6d7f4ec1aeee3d0f9950a21571e04c913786 Mon Sep 17 00:00:00 2001 From: altaf-creator Date: Mon, 6 Jul 2026 11:36:40 +0800 Subject: markdown support done --- index.html | 58 ++++++++++++++++++++++++++++++++++++++-- main.py | 75 +++++++++++++++++++++++++++++++++++++--------------- markdown_renderer.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ style.css | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 230 insertions(+), 23 deletions(-) create mode 100644 markdown_renderer.py diff --git a/index.html b/index.html index 59dabb7..87881bf 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,15 @@ altaf-files + + + + + + + + +
-
+ -
+
+
+
+
+
+ + diff --git a/main.py b/main.py index 6a320fa..15a1720 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,8 @@ from bs4 import BeautifulSoup import datetime from dotenv import load_dotenv from os import getenv +import markdown_renderer as md # markdown_renderer.py +import requests print("Hello, world!") load_dotenv() @@ -60,8 +62,15 @@ def folder(path: str): return fastapi.responses.Response(status_code=403, content="Access denied.") if target_path.is_file(): -# if target_path.suffix == ".md": -# return fastapi.responses.Response(content=markdown(target_path), media_type="text/html", status_code=200) + if target_path.suffix == ".md": + return fastapi.responses.Response(content=markdown(absolute_path=target_path, url_path=path), media_type="text/html", status_code=200) + if target_path.suffix == ".nextcloudmd": + content = "" + with target_path.open(mode="r") as f: + content = f.read() + r = requests.get(content.rstrip(), stream=True) + + return fastapi.responses.Response(content=markdown(mdstr=r.text, url_path=path), media_type="text/html", status_code=200) return fastapi.responses.FileResponse(target_path) elif not target_path.is_dir(): return fastapi.responses.Response(status_code=404) @@ -82,33 +91,36 @@ def folder(path: str): for f in files: add_item_html(f, path, False, soup) - local_path_list = path.split("/") + render_breadcrumb(soup, path) - home_btn = soup.new_tag("a") - home_btn["class"] = "link" - home_btn["href"] = "/" - home_btn.string = "🏠 altaf-files" - soup.select_one("#breadcrumb-container").append(home_btn) + soup.select_one("#file-content-section").decompose() - for i in range(len(local_path_list)): - new_sep = soup.new_tag("span") - new_sep.string = "/" - soup.select_one("#breadcrumb-container").append(new_sep) + return fastapi.responses.Response(content=str(soup), media_type="text/html", status_code=200) - new_btn = soup.new_tag("a") - new_btn["class"] = "link" - new_btn["href"] = "/" + "/".join(local_path_list[0:i + 1]) - new_btn.string = local_path_list[i] - soup.select_one("#breadcrumb-container").append(new_btn) - return fastapi.responses.Response(content=str(soup), media_type="text/html", status_code=200) +def markdown(mdstr: str = None, absolute_path: Path = None, url_path: str = None): + content = "" + if absolute_path: + with absolute_path.open(mode="r") as f: + content = f.read() + else: + content = mdstr + + _, mdhtml = md.md_to_html(content) + + soup = BeautifulSoup(HTML_FOLDER_TEMPLATE, "html.parser") + + soup.select_one("#list-container-section").decompose() + md_soup = BeautifulSoup(mdhtml, "html.parser") -def markdown(absolute_path: str): - pass + soup.select_one("#file-content").append(md_soup) + render_breadcrumb(soup, url_path) -def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: BeautifulSoup): + return str(soup) + +def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: BeautifulSoup) -> None: href_path = "" if current_path == "": @@ -152,6 +164,27 @@ def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: soup.select_one("#list-container").append(new_btn) +def render_breadcrumb(soup: BeautifulSoup, path: str) -> None: + local_path_list = path.split("/") + + home_btn = soup.new_tag("a") + home_btn["class"] = "link" + home_btn["href"] = "/" + home_btn.string = "🏠 altaf-files" + soup.select_one("#breadcrumb-container").append(home_btn) + + for i in range(len(local_path_list)): + new_sep = soup.new_tag("span") + new_sep.string = "/" + soup.select_one("#breadcrumb-container").append(new_sep) + + new_btn = soup.new_tag("a") + new_btn["class"] = "link" + new_btn["href"] = "/" + "/".join(local_path_list[0:i + 1]) + new_btn.string = local_path_list[i] + soup.select_one("#breadcrumb-container").append(new_btn) + + def format_size(byte_size): for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB']: if byte_size < 1024.0: diff --git a/markdown_renderer.py b/markdown_renderer.py new file mode 100644 index 0000000..cf3a05c --- /dev/null +++ b/markdown_renderer.py @@ -0,0 +1,71 @@ +import markdown +import frontmatter +import markdown.treeprocessors +import markdown.extensions + + +class DefaultClassTreeprocessor(markdown.treeprocessors.Treeprocessor): + def run(self, root): + for element in root.iter("a"): + print(element.attrib) + if "class" not in element.attrib: + element.set("class", "link") + elif "button" not in element.attrib["class"]: + element.set("class", element.attrib["class"] + " link") + if "href" in element.attrib and element.attrib["href"] == "": + del element.attrib["href"] + for element in root.iter("img"): + element.set("class", "img") + + +class DefaultClassExtension(markdown.extensions.Extension): + def extendMarkdown(self, md): + md.treeprocessors.register(DefaultClassTreeprocessor(md), "link_class", 2) + + +def strict_identity_slugify(value, separator): + return value + + +def md_to_html(raw_content: str, text_mode: bool = False) -> str: + metadata, content = frontmatter.parse(raw_content) + html_text = markdown.markdown( + content, + output_format="html", + extensions=["attr_list", "fenced_code", "tables", DefaultClassExtension(), "md_in_html", "pymdownx.tilde", "footnotes", "codehilite", "mdx_wikilink_plus", "toc", "pymdownx.mark", "pymdownx.arithmatex"], + extension_configs={ + "toc": { + "slugify": strict_identity_slugify + }, + "pymdownx.arithmatex": { + "generic": True + } + } + ) + + section_class = "" + section_class_only = "" + + if text_mode: + section_class = ' class="text-section"' + section_class_only = ' text-section' + + html_text = html_text.replace("

+-+-+-

", f'
') + html_text = html_text.replace("

+-+-+-", f'

') + html_text = html_text.replace("+-+-+-

", f'
') + html_text = html_text.replace("-+-+-+

", "
") + html_text = html_text.replace("

-+-+-+", "

") + html_text = html_text.replace("

+=+=+=

", f'
') + html_text = html_text.replace("

+=+=+=", f'

') + html_text = html_text.replace("+=+=+=

", f'
') + html_text = html_text.replace("

{{{

", '
') + html_text = html_text.replace("

{{{", '

') + html_text = html_text.replace("{{{

", '
') + html_text = html_text.replace("

}}}

", '
') + html_text = html_text.replace("}}}

", '
') + html_text = html_text.replace("

}}}", '

') + html_text = html_text.replace("

{&{

", '
') + html_text = html_text.replace("

{&{", '

') + html_text = html_text.replace("{&{

", '
') + + return (metadata, html_text) diff --git a/style.css b/style.css index 166ac5c..4fa6de6 100644 --- a/style.css +++ b/style.css @@ -84,6 +84,55 @@ section { justify-content: center; } +ul { + line-height: 1.5rem; +} + +p { + line-height: 1.5rem; +} + +h1 { + font-family: "JetBrains Mono"; + font-size: 2.5rem; + font-weight: 600; +} + +h2 { + font-family: "JetBrains Mono"; + font-size: 2.1rem; + font-weight: 600; + margin-bottom: 1rem; +} + +h3 { + font-family: "JetBrains Mono"; + font-size: 1.8rem; + font-weight: 500; + margin-bottom: 1rem; +} + +h4 { + font-family: "JetBrains Mono"; + font-size: 1.5rem; + font-weight: 400; + margin-bottom: 1rem; +} + +h5 { + font-family: "JetBrains Mono"; + font-size: 1.2rem; + font-weight: 700; + margin-bottom: 1rem; +} + +h6 { + font-family: "JetBrains Mono"; + font-size: 1rem; + font-weight: 700; + margin-bottom: 1rem; +} + .div-sizing { max-width: 1200px; width: 100%; -- cgit v1.2.3