Eliminate raw HTML injection: convert ~kg-html/captions to native sx

Add shared/sx/html_to_sx.py (HTMLParser-based HTML→sx converter) and
update lexical_to_sx.py so HTML cards, markdown cards, and captions all
produce native sx expressions instead of opaque HTML strings.

- ~kg-html now wraps native sx children (editor can identify the block)
- New ~kg-md component for markdown card blocks
- Captions are sx expressions, not escaped HTML strings
- kg_cards.sx: replace (raw! caption) with direct caption rendering
- sx-editor.js: htmlToSx() via DOMParser, serializeInline for captions,
  _childrenSx for ~kg-html/~kg-md, new kg-md edit UI
- Migration script (blog/scripts/migrate_sx_html.py) to re-convert
  stored sx_content from lexical source

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-02 19:57:27 +00:00
parent 4668c30890
commit 8ceb9aee62
7 changed files with 595 additions and 25 deletions

View File

@@ -16,6 +16,8 @@ from typing import Callable
import mistune
from shared.sx.html_to_sx import html_to_sx
# ---------------------------------------------------------------------------
# Registry
@@ -249,7 +251,7 @@ def _image(node: dict) -> str:
if alt:
parts.append(f':alt "{_esc(alt)}"')
if caption:
parts.append(f':caption "{_esc(caption)}"')
parts.append(f":caption {html_to_sx(caption)}")
if width:
parts.append(f':width "{_esc(width)}"')
if href:
@@ -273,20 +275,21 @@ def _gallery(node: dict) -> str:
if img.get("alt"):
item_parts.append(f'"alt" "{_esc(img["alt"])}"')
if img.get("caption"):
item_parts.append(f'"caption" "{_esc(img["caption"])}"')
item_parts.append(f'"caption" {html_to_sx(img["caption"])}')
row_items.append("(dict " + " ".join(item_parts) + ")")
rows.append("(list " + " ".join(row_items) + ")")
images_sx = "(list " + " ".join(rows) + ")"
caption = node.get("caption", "")
caption_attr = f' :caption "{_esc(caption)}"' if caption else ""
caption_attr = f" :caption {html_to_sx(caption)}" if caption else ""
return f"(~kg-gallery :images {images_sx}{caption_attr})"
@_converter("html")
def _html_card(node: dict) -> str:
raw = node.get("html", "")
return f'(~kg-html :html "{_esc(raw)}")'
inner = html_to_sx(raw)
return f"(~kg-html {inner})"
@_converter("embed")
@@ -295,7 +298,7 @@ def _embed(node: dict) -> str:
caption = node.get("caption", "")
parts = [f':html "{_esc(embed_html)}"']
if caption:
parts.append(f':caption "{_esc(caption)}"')
parts.append(f":caption {html_to_sx(caption)}")
return "(~kg-embed " + " ".join(parts) + ")"
@@ -325,7 +328,7 @@ def _bookmark(node: dict) -> str:
parts.append(f':thumbnail "{_esc(thumbnail)}"')
caption = node.get("caption", "")
if caption:
parts.append(f':caption "{_esc(caption)}"')
parts.append(f":caption {html_to_sx(caption)}")
return "(~kg-bookmark " + " ".join(parts) + ")"
@@ -390,7 +393,7 @@ def _video(node: dict) -> str:
parts = [f':src "{_esc(src)}"']
if caption:
parts.append(f':caption "{_esc(caption)}"')
parts.append(f":caption {html_to_sx(caption)}")
if width:
parts.append(f':width "{_esc(width)}"')
if thumbnail:
@@ -425,7 +428,7 @@ def _file(node: dict) -> str:
if size_str:
parts.append(f':filesize "{size_str}"')
if caption:
parts.append(f':caption "{_esc(caption)}"')
parts.append(f":caption {html_to_sx(caption)}")
return "(~kg-file " + " ".join(parts) + ")"
@@ -438,4 +441,5 @@ def _paywall(_node: dict) -> str:
def _markdown(node: dict) -> str:
md_text = node.get("markdown", "")
rendered = mistune.html(md_text)
return f'(~kg-html :html "{_esc(rendered)}")'
inner = html_to_sx(rendered)
return f"(~kg-md {inner})"