technical-screen-2025-10-22/modules/markdown_utils.py

91 lines
3.9 KiB
Python

print('🟣 MARKDOWN_UTILS.PY: Starting import...', flush=True)
#!/usr/bin/env python3
import re
import requests
import json
def send_to_api_and_get_haste_link(markdown_content, document_title):
"""Send FULL structured markdown to API and get both raw markdown and HTML URLs"""
try:
print("Sending to API for URLs...", flush=True)
# Send the FULL structured markdown - NO STRIPPING, NO CLEANING
# Only remove local image references since they won't work online
online_markdown = re.sub(r'!\[Slide (\d+)\]\(slides/[^\)]+\)', r'**[Slide \1 Image]**', markdown_content)
# First, send to haste.nixc.us for raw markdown
raw_haste_url = None
try:
print(" 📝 Creating raw markdown URL...", flush=True)
raw_response = requests.post(
"https://haste.nixc.us/documents",
data=online_markdown.encode('utf-8'),
headers={"Content-Type": "text/plain"},
timeout=30
)
if raw_response.status_code == 200:
response_data = raw_response.json()
raw_token = response_data.get('key', '')
raw_haste_url = f"https://haste.nixc.us/{raw_token}"
print(f" ✅ Raw markdown URL created", flush=True)
else:
print(f" ⚠️ Raw markdown upload failed with status {raw_response.status_code}", flush=True)
except Exception as e:
print(f" ⚠️ Failed to create raw markdown URL: {e}", flush=True)
# Then, send to md.colinknapp.com for HTML version (with fallback endpoints)
html_url = None
try:
print(" 🎨 Creating HTML version URL...", flush=True)
api_data = {
"title": f"Pitch Deck Analysis: {document_title}",
"content": online_markdown
}
endpoints = [
"https://md.colinknapp.com/haste",
"https://md.colinknapp.com/api/haste",
"https://md.colinknapp.com/md/haste"
]
last_status = None
last_body = None
for ep in endpoints:
try:
response = requests.post(
ep,
headers={"Content-Type": "application/json"},
json=api_data,
timeout=30
)
last_status = response.status_code
last_body = response.text[:200]
if response.status_code == 200:
result = response.json()
candidate = result.get('url') or result.get('html_url') or result.get('link')
if candidate:
html_url = candidate
print(f" ✅ HTML version URL created via {ep}", flush=True)
break
else:
print(f" ⚠️ {ep} responded 200 without url field", flush=True)
else:
print(f" ⚠️ {ep} failed with status {response.status_code}", flush=True)
except Exception as inner_e:
print(f" ⚠️ {ep} request error: {inner_e}", flush=True)
if html_url is None and last_status is not None:
print(f" ⚠️ HTML API request failed. Last status: {last_status}", flush=True)
if last_body:
print(f" Response: {last_body}", flush=True)
except Exception as e:
print(f" ⚠️ Failed to create HTML URL: {e}", flush=True)
return raw_haste_url, html_url
except Exception as e:
print(f"⚠️ Failed to send to API: {e}", flush=True)
return None, None
print('🟣 MARKDOWN_UTILS.PY: Import complete!', flush=True)