# 第一步 · notion_tools.py 追加 · 复制整块粘贴到终端

<aside>
📋

**妈妈操作：** 复制下面整个代码框 → 粘贴到终端 → 回车

这个命令用的是 `>>` 双箭头，只在文件末尾追加新代码，不会动现有代码。

</aside>

```bash
cat >> ~/chenxing-aircraft/backend/notion_tools.py << 'ENDOFFILE'

# === F1-卡1 新增 · write_to_notion · 2026-05-24 ===
async def create_subpage(parent_page_id, title, content="", icon="📝"):
    """在指定页面下新建子页面，返回新页面ID"""
    if not NOTION_API_KEY:
        return "[Error] NOTION_API_KEY未配置"
    pid = parent_page_id.replace("-", "")
    fid = f"{pid[:8]}-{pid[8:12]}-{pid[12:16]}-{pid[16:20]}-{pid[20:]}"
    children = []
    if content:
        lines = content.split("\n")
        for line in lines:
            stripped = line.strip()
            if not stripped:
                continue
            if stripped.startswith("### "):
                children.append({"object": "block", "type": "heading_3",
                    "heading_3": {"rich_text": [{"text": {"content": stripped[4:]}}]}})
            elif stripped.startswith("## "):
                children.append({"object": "block", "type": "heading_2",
                    "heading_2": {"rich_text": [{"text": {"content": stripped[3:]}}]}})
            elif stripped.startswith("# "):
                children.append({"object": "block", "type": "heading_1",
                    "heading_1": {"rich_text": [{"text": {"content": stripped[2:]}}]}})
            elif stripped.startswith("- "):
                children.append({"object": "block", "type": "bulleted_list_item",
                    "bulleted_list_item": {"rich_text": [{"text": {"content": stripped[2:]}}]}})
            elif stripped.startswith("> "):
                children.append({"object": "block", "type": "quote",
                    "quote": {"rich_text": [{"text": {"content": stripped[2:]}}]}})
            elif stripped == "---":
                children.append({"object": "block", "type": "divider", "divider": {}})
            elif stripped.startswith("```"):
                continue
            else:
                children.append({"object": "block", "type": "paragraph",
                    "paragraph": {"rich_text": [{"text": {"content": stripped}}]}})
        children = children[:100]
    payload = {
        "parent": {"page_id": fid},
        "properties": {"title": [{"text": {"content": title}}]},
        "children": children
    }
    if icon:
        payload["icon"] = {"type": "emoji", "emoji": icon}
    async with httpx.AsyncClient(timeout=30) as c:
        r = await c.post(f"{BASE}/pages", headers=_h(), json=payload)
        if r.status_code in (200, 201):
            new_id = r.json().get("id", "").replace("-", "")
            return f"✅ 子页面创建成功！\n标题：{title}\n页面ID：{new_id}\n父页面：{pid}"
        else:
            return f"[Error] 创建失败 HTTP {r.status_code} - {r.text[:300]}"
ENDOFFILE
echo "✅ notion_tools.py 追加完成"
```

<aside>
✅

终端显示 `✅ notion_tools.py 追加完成` 就成功了，继续第二步。

</aside>