Skip to content

Commit d32f5c3

Browse files
authored
perf: Early return if there is no marimo code block in file (#52)
* WIP * re-use parsing and simplify
1 parent f0b5df1 commit d32f5c3

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

mkdocs_marimo/plugin.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def is_inside_four_backticks(markdown: str, start_pos: int) -> bool:
2727

2828

2929
def find_marimo_code_fences(markdown: str) -> list[re.Match[str]]:
30-
matches: list[Any] = []
31-
for match in CODE_FENCE_REGEX.finditer(markdown):
32-
if not is_inside_four_backticks(markdown, match.start()):
33-
matches.append(match)
34-
return matches
30+
return [
31+
match
32+
for match in CODE_FENCE_REGEX.finditer(markdown)
33+
if not is_inside_four_backticks(markdown, match.start())
34+
]
3535

3636

3737
def collect_marimo_code(markdown: str) -> tuple[list[str], list[re.Match[str]]]:
@@ -107,11 +107,14 @@ def on_page_markdown(
107107
if page.abs_url is None:
108108
return markdown
109109

110+
code_blocks, matches = collect_marimo_code(markdown)
111+
if not matches:
112+
return markdown
113+
110114
generator = marimo.MarimoIslandGenerator()
111115
replacements: list[str] = []
112116
self.replacements[page.abs_url] = replacements
113117
outputs: list[Any] = []
114-
code_blocks, matches = collect_marimo_code(markdown)
115118

116119
for code in code_blocks:
117120
outputs.append(generator.add_code(code))
@@ -207,8 +210,8 @@ def on_post_page(self, output: str, /, *, page: Page, config: MkDocsConfig) -> s
207210
# Add the extra header to the output
208211
output = output.replace("</head>", f"{header}\n</head>")
209212

210-
replacesments: list[str] = self.replacements.get(page.abs_url, [])
211-
for idx, replacement in enumerate(replacesments):
213+
replacements: list[str] = self.replacements.get(page.abs_url, [])
214+
for idx, replacement in enumerate(replacements):
212215
output = output.replace(f"<marimo-internal-island idx='{idx}'/>", replacement, 1)
213216
return output
214217

0 commit comments

Comments
 (0)