diff --git a/pcweb/markdown_api.py b/pcweb/markdown_api.py new file mode 100644 index 000000000..31bf6b935 --- /dev/null +++ b/pcweb/markdown_api.py @@ -0,0 +1,22 @@ +"""Write per-doc raw Markdown files under ``.web/public`` for ``.md`` URLs.""" + +from pathlib import Path + +from reflex.constants import Dirs + +from pcweb.pages.docs import doc_markdown_sources + +PUBLIC_DIR = Path.cwd() / Dirs.WEB / Dirs.PUBLIC + + +def generate_markdown_files() -> None: + for route, source_path in doc_markdown_sources.items(): + resolved = Path(source_path) + if not resolved.is_absolute(): + resolved = Path.cwd() / resolved + if not resolved.is_file(): + continue + + dest = PUBLIC_DIR / (route.strip("/") + ".md") + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_text(resolved.read_text(encoding="utf-8"), encoding="utf-8") diff --git a/pcweb/pages/docs/__init__.py b/pcweb/pages/docs/__init__.py index 2b0b9f979..76a326b0f 100644 --- a/pcweb/pages/docs/__init__.py +++ b/pcweb/pages/docs/__init__.py @@ -132,6 +132,8 @@ def get_components_from_metadata(current_doc): recipes_list = defaultdict(list) docs_ns = SimpleNamespace() +doc_markdown_sources: dict[str, str] = {} + def exec_blocks(doc, href): """Execute the exec and demo blocks in the document.""" @@ -283,6 +285,21 @@ def comp(_actual=actual_path, _virtual=virtual_doc): return make_docpage(resolved.route, resolved.display_title, virtual_doc, comp) +for fd in flexdown_docs: + if fd.endswith("-style.md") or fd.endswith("-ll.md"): + continue + route = doc_route_from_path(fd) + if not _check_whitelisted_path(route): + continue + doc_markdown_sources[route] = doc_path_mapping.get(fd, fd) +for virtual_doc, actual_path in docgen_docs.items(): + if virtual_doc.endswith("-style.md") or virtual_doc.endswith("-ll.md"): + continue + route = doc_route_from_path(virtual_doc) + if not _check_whitelisted_path(route): + continue + doc_markdown_sources[route] = actual_path + doc_routes = [ library, custom_components, diff --git a/pcweb/pcweb.py b/pcweb/pcweb.py index c272e6fac..15ed39c82 100644 --- a/pcweb/pcweb.py +++ b/pcweb/pcweb.py @@ -8,6 +8,7 @@ from pcweb import styles from pcweb.constants import REFLEX_ASSETS_CDN +from pcweb.markdown_api import generate_markdown_files from pcweb.meta.meta import favicons_links, to_cdn_image_url from pcweb.pages import page404, routes from pcweb.pages.docs import exec_blocks, outblocks @@ -22,6 +23,8 @@ for doc, href in outblocks: exec_blocks(doc, href) +generate_markdown_files() + # Create the app. app = rxe.App( style=styles.BASE_STYLE,