fix(notion-writer): fence-aware image/translate, NtnUploadError boundary, dead-code removal

FIX 1: _content_has_local_images and extract_local_images now skip lines inside
  ``` fences so a local-image ref inside a code block is not treated as real.
FIX 2: md_translate.translate() tracks fence state; callout/columns/mention
  transforms are suppressed for lines inside ``` ... ``` blocks (pass verbatim).
FIX 3: main() catches NtnUploadError and prints a clean error + e.stderr before
  sys.exit(1); body moved to _main_body(parser, args).
FIX 4: ntn_files.upload() raises NtnUploadError on empty stdout instead of
  IndexError, giving a clean message combined with FIX 3.
FIX 5: Removed unused write_to_page() (both page paths are inlined).
FIX 6: create_page_markdown() omits the 'markdown' key when value is falsy,
  avoiding empty-markdown sends on DB rows created without --file/--stdin.
FIX 7: CLAUDE.md documents --allow-deleting-content scope (markdown engine only).
TDD: Added test_fence_passthrough_no_transform (md_translate, suite now 8) and
  test_local_image_inside_fence_ignored (engine_routing, suite now 6).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-27 10:33:09 +09:00
parent cf7e649284
commit 759bb20911
7 changed files with 90 additions and 22 deletions

View File

@@ -18,6 +18,7 @@ from notion_client.errors import APIResponseError
import _notion_compat as compat
import ntn_files
from ntn_files import NtnUploadError
import md_translate
MARKDOWN_NOTION_VERSION = "2026-03-11"
@@ -533,7 +534,13 @@ def create_image_block(alt: str, target: str) -> Dict[str, Any]:
def _content_has_local_images(content: str) -> bool:
in_fence = False
for line in content.split('\n'):
if line.strip().startswith('```'):
in_fence = not in_fence
continue # fence delimiter lines are never image lines
if in_fence:
continue
m = IMAGE_RE.match(line)
if m and not m.group(2).startswith(('http://', 'https://')):
return True
@@ -542,9 +549,18 @@ def _content_has_local_images(content: str) -> bool:
def extract_local_images(content: str):
"""Remove standalone LOCAL image lines; keep remote ones inline.
Returns (content_without_local_images, [(alt, target), ...])."""
Returns (content_without_local_images, [(alt, target), ...]).
Lines inside fenced code blocks are passed through unchanged."""
kept, imgs = [], []
in_fence = False
for line in content.split('\n'):
if line.strip().startswith('```'):
in_fence = not in_fence
kept.append(line)
continue
if in_fence:
kept.append(line)
continue
m = IMAGE_RE.match(line)
if m and not m.group(2).startswith(('http://', 'https://')):
imgs.append((m.group(1), m.group(2)))
@@ -792,21 +808,6 @@ def update_page_properties(notion: Client, page_id: str, properties: Dict) -> bo
return False
def write_to_page(notion: Client, page_id: str, markdown_content: str, mode: str = 'append') -> bool:
"""Write markdown content to a Notion page."""
blocks = markdown_to_notion_blocks(markdown_content)
if not blocks:
print("No content to write")
return False
if mode == 'replace':
if not clear_page_content(notion, page_id):
return False
return append_to_page(notion, page_id, blocks)
def main():
parser = argparse.ArgumentParser(
description='Push markdown content to Notion pages or databases',
@@ -853,6 +854,17 @@ Examples:
args = parser.parse_args()
try:
_main_body(parser, args)
except NtnUploadError as e:
print(f"Error: {e}")
if e.stderr:
print(e.stderr)
sys.exit(1)
def _main_body(parser, args):
"""Body of main() after argparse; separated so NtnUploadError can be caught cleanly."""
if not NOTION_TOKEN:
print("Error: NOTION_API_KEY not set in environment.")
print("Preferred: fetch from 1Password at runtime —")