| name | description |
|---|---|
reddit-to-markdown |
Fetch a Reddit post and its comments as structured markdown.
Auto-triggers when the user's message contains a reddit.com URL pointing to
a submission (e.g. /r/sub/comments/id/...).
Example triggers: any message containing "reddit.com/r/*/comments/*".
|
When the user's message contains a Reddit submission URL, fetch it and render as markdown so the content is available in the conversation.
Reddit serves JSON for any page when you append .json to the URL.
No API key, no auth, no browser needed.
- Extract the Reddit submission URL from the user's message.
- Normalize the URL: strip query params and trailing slashes, ensure it ends
with
.json. - Fetch with curl:
curl -s -H "User-Agent: reddit2md/1.0" "<url>.json" - Parse the JSON and render markdown using this Python snippet (pipe curl output into it):
import json, sys
data = json.load(sys.stdin)
post = data[0]['data']['children'][0]['data']
lines = []
lines.append(f"# {post['title']}")
meta = f"**{post['subreddit_name_prefixed']}** | u/{post['author']} | {post['score']} pts ({int(post.get('upvote_ratio',0)*100)}% upvoted) | {post.get('num_comments',0)} comments"
lines.append(meta)
flair = post.get('link_flair_text', '')
if flair:
lines.append(f"Flair: {flair}")
lines.append('')
if post.get('selftext'):
lines.append(post['selftext'])
lines.append('')
if post.get('url') and not post.get('is_self'):
lines.append(f"Link: {post['url']}")
lines.append('')
MAX_DEPTH = 5
def render_comments(children):
result = []
for child in children:
if child['kind'] != 't1':
continue
d = child['data']
depth = d.get('depth', 0)
if depth > MAX_DEPTH:
continue
result.append(f"[depth:{depth}] **u/{d.get('author','[deleted]')}** ({d.get('score',0)} pts)")
result.append(d.get('body', ''))
result.append('')
replies = d.get('replies', '')
if isinstance(replies, dict):
result.extend(render_comments(replies['data']['children']))
return result
lines.append('---')
lines.append('## Comments')
lines.append('')
lines.extend(render_comments(data[1]['data']['children']))
print('\n'.join(lines))- Present the markdown output to the user in the conversation.
Reddit may rate-limit requests. If you get a 429 or empty response, wait a few seconds and retry once.
For faster repeated use, save the Python snippet above to a file:
# One-time setup
cat << 'EOF' > ~/.local/bin/reddit2md
#!/usr/bin/env python3
import json, sys
data = json.load(sys.stdin)
post = data[0]['data']['children'][0]['data']
lines = []
lines.append(f"# {post['title']}")
meta = f"**{post['subreddit_name_prefixed']}** | u/{post['author']} | {post['score']} pts ({int(post.get('upvote_ratio',0)*100)}% upvoted) | {post.get('num_comments',0)} comments"
lines.append(meta)
flair = post.get('link_flair_text', '')
if flair:
lines.append(f"Flair: {flair}")
lines.append('')
if post.get('selftext'):
lines.append(post['selftext'])
lines.append('')
if post.get('url') and not post.get('is_self'):
lines.append(f"Link: {post['url']}")
lines.append('')
MAX_DEPTH = 5
def render_comments(children):
result = []
for child in children:
if child['kind'] != 't1':
continue
d = child['data']
depth = d.get('depth', 0)
if depth > MAX_DEPTH:
continue
result.append(f"[depth:{depth}] **u/{d.get('author','[deleted]')}** ({d.get('score',0)} pts)")
result.append(d.get('body', ''))
result.append('')
replies = d.get('replies', '')
if isinstance(replies, dict):
result.extend(render_comments(replies['data']['children']))
return result
lines.append('---')
lines.append('## Comments')
lines.append('')
lines.extend(render_comments(data[1]['data']['children']))
print('\n'.join(lines))
EOF
chmod +x ~/.local/bin/reddit2mdThen use: curl -s -H "User-Agent: reddit2md/1.0" "<url>.json" | reddit2md