Skip to content

Commit 7ebfef3

Browse files
fix: placeholder index.md should be removed after (not during) running "ap dev"
1 parent 60aeca7 commit 7ebfef3

4 files changed

Lines changed: 42 additions & 38 deletions

File tree

src/afterpython/builders/index_md.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
from afterpython.const import CONTENT_TYPES, PLACEHOLDER_INDEX_MARKER
55

66

7-
def _legacy_placeholder_content(content_type: str) -> str:
8-
return f"""---
9-
title: ← {content_type.capitalize()}
10-
---
11-
12-
This is a placeholder index page. The actual {content_type} landing page is rendered by SvelteKit.
13-
"""
14-
15-
16-
def _legacy_welcome_content(content_type: str) -> str:
17-
return f"""# Welcome to AfterPython
7+
def _placeholder_content(content_type: str) -> str:
8+
"""Current placeholder body. Marker is an HTML comment so it embeds invisibly;
9+
_is_afterpython_placeholder_index keys off it to tell our files apart from
10+
user-authored ones."""
11+
return f"""{PLACEHOLDER_INDEX_MARKER}
12+
# Welcome to AfterPython
1813
1914
Welcome to your project's {content_type}! This is a starter page to help you get started.
2015
@@ -35,6 +30,19 @@ def _legacy_welcome_content(content_type: str) -> str:
3530
"""
3631

3732

33+
def _legacy_placeholder_content(content_type: str) -> str:
34+
"""Frozen fingerprint of the arrow-stub placeholder written between commits
35+
49f30c1 and 3a560e2 (no marker). Kept solely so projects built in that window
36+
are still recognized as ours and cleaned up rather than blocking with the
37+
'reserved' error. Do not edit — that would break recognition."""
38+
return f"""---
39+
title: ← {content_type.capitalize()}
40+
---
41+
42+
This is a placeholder index page. The actual {content_type} landing page is rendered by SvelteKit.
43+
"""
44+
45+
3846
def _is_afterpython_placeholder_index(index_md, content_type: str) -> bool:
3947
"""Return True only for non-doc index.md files generated by AfterPython."""
4048
content = index_md.read_text(encoding="utf-8", errors="ignore")
@@ -43,7 +51,6 @@ def _is_afterpython_placeholder_index(index_md, content_type: str) -> bool:
4351
return (
4452
PLACEHOLDER_INDEX_MARKER in content
4553
or normalized_content == _legacy_placeholder_content(content_type).strip()
46-
or normalized_content == _legacy_welcome_content(content_type).strip()
4754
)
4855

4956

@@ -76,25 +83,15 @@ def create_placeholder_index_md_files():
7683
)
7784
continue
7885

79-
content_path = ap.paths.afterpython_path / content_type
80-
myst_yml_path = content_path / "myst.yml"
81-
82-
# Prepend index.md to TOC in myst.yml
83-
if myst_yml_path.exists():
84-
# Create placeholder index.md file
85-
_write_index_file(content_type)
86-
myst_data = read_yaml(myst_yml_path) or {}
87-
toc = myst_data.get("project", {}).get("toc", [])
86+
_write_index_file(content_type)
8887

89-
# Check if index.md is already first in TOC
90-
if not toc or toc[0].get("file") != "index.md":
91-
# Prepend index.md to TOC in myst.yml
92-
new_toc = [{"file": "index.md"}, *toc]
93-
if "project" not in myst_data:
94-
myst_data["project"] = {}
95-
myst_data["project"]["toc"] = new_toc
96-
write_yaml(myst_yml_path, myst_data)
97-
click.echo(f"Added index.md to TOC in {myst_yml_path}")
88+
# Prepend index.md to TOC in myst.yml (skip if already first).
89+
myst_data = read_yaml(myst_yml_path) or {}
90+
toc = myst_data.get("project", {}).get("toc", [])
91+
if not toc or toc[0].get("file") != "index.md":
92+
myst_data.setdefault("project", {})["toc"] = [{"file": "index.md"}, *toc]
93+
write_yaml(myst_yml_path, myst_data)
94+
click.echo(f"Added index.md to TOC in {myst_yml_path}")
9895

9996

10097
def delete_placeholder_index_md_files():

src/afterpython/cli/commands/build.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ def _move_files(
171171
print(f"Moved: {source} to {destination}")
172172

173173
build_content_json()
174-
delete_placeholder_index_md_files()
174+
# In dev mode the MyST `start` server is watching these files. Deleting
175+
# them here would make MyST re-index without the placeholder and revert
176+
# to using the first content file as the section index — exactly what
177+
# the placeholder was created to prevent. `ap dev` cleans them up in
178+
# its `finally` block after MyST has been shut down.
179+
if not dev_build:
180+
delete_placeholder_index_md_files()
175181

176182
website_static = ap.paths.website_path / "static"
177183
website_static.mkdir(parents=True, exist_ok=True)

src/afterpython/cli/commands/dev.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import click
1212
from click.exceptions import Exit
1313

14+
from afterpython.builders import delete_placeholder_index_md_files
1415
from afterpython.cli.commands.build import postbuild, prebuild
1516
from afterpython.const import CONTENT_TYPES
1617
from afterpython.utils import find_available_port, find_node_env
@@ -218,3 +219,6 @@ def cleanup_processes():
218219
pass
219220
finally:
220221
cleanup_processes()
222+
# Now that MyST watchers are gone, clean up the placeholders
223+
# postbuild() intentionally left behind for dev mode.
224+
delete_placeholder_index_md_files()

src/afterpython/tools/myst.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,11 @@ def _write_index_file(content_type: tContentType):
143143
f"and update the reference in afterpython/{content_type}/myst.yml."
144144
)
145145

146-
index_content = f"""---
147-
title: ← {content_type.capitalize()}
148-
---
146+
# Lazy import to avoid the circular dep with builders/index_md.py, which
147+
# already lazy-imports _write_index_file from this module.
148+
from afterpython.builders.index_md import _placeholder_content
149149

150-
{PLACEHOLDER_INDEX_MARKER}
151-
152-
This is a placeholder index page. The actual {content_type} landing page is rendered by SvelteKit.
153-
"""
150+
index_content = _placeholder_content(content_type)
154151
index_file.write_text(index_content)
155152
return index_file
156153

0 commit comments

Comments
 (0)