---
name: mainstreet-publish
description: Publish markdown files to MainStreet for free — no account needed. Use when the user wants to share a markdown file publicly, collect comments on a document, update a published page, or retrieve feedback from MainStreet.
argument-hint: "[filename or action: publish/update/comments/delete/list]"
allowed-tools: Bash(curl *), Read, Write, Glob, Grep
---

# Publish to MainStreet

Publish, update, or manage markdown files on MainStreet — free, no account needed.
Readers can view and comment at the published URL. Comments sync back to your workspace.

## Determining the action

Parse the user's request to determine the action:

- **"publish"** or **"share"** + a file → Publish
- **"update"** + a file/publication → Update
- **"comments"** or **"feedback"** → Get Comments
- **"delete"** or **"remove"** → Delete
- **"list"** or **"publications"** → List

If `$ARGUMENTS` contains a filename, use that. Otherwise look at the user's recent context for markdown files.

## Publish

1. Identify the markdown file (from `$ARGUMENTS` or conversation context)
2. Read the file content
3. Extract the title from the first `# Heading`, or use the filename
4. Create a temp JSON file and POST to the API:
   ```bash
   cat > /tmp/ms-publish.json << 'EOF'
   {"title": "<title>", "content": "<escaped content>", "accessLevel": "comment", "sourceFile": "<filename>"}
   EOF
   curl -s -X POST https://mainstreet.ink/api/free/publish \
     -H "Content-Type: application/json" \
     -d @/tmp/ms-publish.json
   ```
5. Parse the JSON response to get `publishId`, `slug`, `secret`, and `url`
6. Create `.mainstreet/` directory if it doesn't exist
7. Write the config to `.mainstreet/<filename-stem>.yaml`:
   ```yaml
   publishId: <publishId>
   slug: <slug>
   secret: <secret>
   url: <url>
   sourceFile: <original filename>
   publishedAt: <ISO timestamp>
   updatedAt: <ISO timestamp>
   ```
8. Ensure `.mainstreet/` is in `.gitignore` (it contains secrets!)
9. Tell the user: **"Published! Live at \<url\>. Anyone with the link can view and comment."**

## Update

1. Find the `.mainstreet/<name>.yaml` config
2. Read the current markdown content
3. PUT to the API with the secret:
   ```bash
   curl -s -X PUT https://mainstreet.ink/api/free/<slug> \
     -H "Content-Type: application/json" \
     -H "X-MainStreet-Secret: <secret>" \
     -d @/tmp/ms-update.json
   ```
4. Update `updatedAt` in the config
5. Tell the user: **"Updated! Changes are live at \<url\>"**

## Get Comments

1. Find the `.mainstreet/<name>.yaml` config
2. GET comments with the secret:
   ```bash
   curl -s https://mainstreet.ink/api/free/<slug>/comments \
     -H "X-MainStreet-Secret: <secret>"
   ```
3. Display comments to the user in a readable format
4. Save to `.mainstreet/comments/<name>.md`:
   ```markdown
   # Comments for <title>
   Last synced: <timestamp>

   ## Author — Date
   > "selected text if any"

   Comment content here.

   ---
   ```
5. Tell the user how many comments were found and where they're saved

## Delete

1. Find the `.mainstreet/<name>.yaml` config
2. DELETE with the secret:
   ```bash
   curl -s -X DELETE https://mainstreet.ink/api/free/<slug> \
     -H "X-MainStreet-Secret: <secret>"
   ```
3. Remove the config and comments files
4. Tell the user: **"Deleted. The page is no longer accessible."**

## List Publications

1. Glob for `*.yaml` files in `.mainstreet/` (excluding `config.yaml`)
2. Read each and display: **Filename | URL | Published | Updated**

## Important

- `.mainstreet/` contains management secrets — **always ensure it's in `.gitignore`**
- If a publication already exists for a file, ask if the user wants to update instead
- Content limit: 500KB per file
- Properly escape markdown content in JSON (handle quotes, newlines, backslashes)
- Base URL: `https://mainstreet.ink`
