File size: 949 Bytes
fb05e78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import hashlib
import re
from pathlib import Path
from urllib.parse import urlparse

def _slug_from_url(url: str) -> str:
    """
    URLから安全なファイル名を生成。例: https://ja.empatheme.org/potion/108/ → potion_108.md
    """
    p = urlparse(url)
    parts = [part for part in p.path.strip("/").split("/") if part]
    if len(parts) >= 2:
        base = f"{parts[-2]}_{parts[-1]}"
    elif parts:
        base = parts[-1]
    else:
        base = "index"
    # サニタイズ: ファイル名に使えない文字をアンダースコアに置換
    base = re.sub(r'[^\w\-_.]', '_', base)
    return f"{base}.md"

def ensure_dir(path: Path) -> None:
    path.mkdir(parents=True, exist_ok=True)

def save_markdown(md_text: str, out_dir: Path, url: str) -> Path:
    ensure_dir(out_dir)
    filename = _slug_from_url(url)
    out_path = out_dir / filename
    out_path.write_text(md_text, encoding="utf-8")
    return out_path