From e1c3b7fcabac2a2e5381778170dedb1dfaa951e4 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 7 Nov 2025 00:53:06 +0100 Subject: [PATCH 1/2] WIP --- mkdocs_marimo/plugin.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mkdocs_marimo/plugin.py b/mkdocs_marimo/plugin.py index 54ad6b0..b87bb26 100644 --- a/mkdocs_marimo/plugin.py +++ b/mkdocs_marimo/plugin.py @@ -27,11 +27,11 @@ def is_inside_four_backticks(markdown: str, start_pos: int) -> bool: def find_marimo_code_fences(markdown: str) -> list[re.Match[str]]: - matches: list[Any] = [] - for match in CODE_FENCE_REGEX.finditer(markdown): - if not is_inside_four_backticks(markdown, match.start()): - matches.append(match) - return matches + return [ + match + for match in CODE_FENCE_REGEX.finditer(markdown) + if not is_inside_four_backticks(markdown, match.start()) + ] def collect_marimo_code(markdown: str) -> tuple[list[str], list[re.Match[str]]]: @@ -99,6 +99,12 @@ def on_page_markdown( ): return self.handle_marimo_file(page) + # Early return if there's no marimo code to process + if ("!marimo_file" not in markdown) and ( + "```python" not in markdown or "{marimo" not in markdown + ): + return markdown + log.info("[marimo] on_page_markdown " + str(page.abs_url)) # Process !marimo_file directives @@ -207,8 +213,8 @@ def on_post_page(self, output: str, /, *, page: Page, config: MkDocsConfig) -> s # Add the extra header to the output output = output.replace("", f"{header}\n") - replacesments: list[str] = self.replacements.get(page.abs_url, []) - for idx, replacement in enumerate(replacesments): + replacements: list[str] = self.replacements.get(page.abs_url, []) + for idx, replacement in enumerate(replacements): output = output.replace(f"", replacement, 1) return output From a98a6d12aadaef609d6b74e6b869fc8f2a596a7f Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 7 Nov 2025 09:10:42 +0100 Subject: [PATCH 2/2] re-use parsing and simplify --- mkdocs_marimo/plugin.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/mkdocs_marimo/plugin.py b/mkdocs_marimo/plugin.py index b87bb26..a77977a 100644 --- a/mkdocs_marimo/plugin.py +++ b/mkdocs_marimo/plugin.py @@ -99,12 +99,6 @@ def on_page_markdown( ): return self.handle_marimo_file(page) - # Early return if there's no marimo code to process - if ("!marimo_file" not in markdown) and ( - "```python" not in markdown or "{marimo" not in markdown - ): - return markdown - log.info("[marimo] on_page_markdown " + str(page.abs_url)) # Process !marimo_file directives @@ -113,11 +107,14 @@ def on_page_markdown( if page.abs_url is None: return markdown + code_blocks, matches = collect_marimo_code(markdown) + if not matches: + return markdown + generator = marimo.MarimoIslandGenerator() replacements: list[str] = [] self.replacements[page.abs_url] = replacements outputs: list[Any] = [] - code_blocks, matches = collect_marimo_code(markdown) for code in code_blocks: outputs.append(generator.add_code(code))