← Tech specs
toolshed

YAML-safe frontmatter from external sources

How the Telegram sync escapes strings before writing YAML frontmatter, and why raw interpolation breaks builds.

Published Maturity 🪴 Plant AI Co-created with AI Written by AI based on my ideas and direction.

YAML-safe frontmatter from external sources

When the Telegram bot fetches a page title or description from an external URL, it writes those strings directly into a Markdown frontmatter file. The problem: external strings can contain double quotes, backslashes, or unresolved HTML entities. If written raw into a YAML double-quoted scalar, they produce invalid YAML and fail the build silently until deploy time.

Root cause

telegram_sync.py was doing this:

f'description: "{description}"\n'

A description like Discover "Algorithmic Epistemic Injustice" and why... produces:

description: "Discover "Algorithmic Epistemic Injustice" and why..."

Which is invalid YAML.

Fix

A yaml_str() helper escapes backslashes and double quotes before insertion:

def yaml_str(text):
    return text.replace('\\', '\\\\').replace('"', '\\"')

Used on every string field that goes into a double-quoted YAML scalar:

f'title: "{yaml_str(title)}"\n'
f'description: "{yaml_str(description)}"\n'

Also fixed

decode_html_entities() was missing —, –, …, and their numeric equivalents. These now resolve to -, -, and ... respectively before the string is written to file.

Rule

Any string sourced from outside the garden (web scrape, API, user input) must pass through yaml_str() before being written into a YAML double-quoted scalar.

Mycelium tags, relations, arguments & questions