npm package: @resonia/sanity-plugin-trenddraft
Description: TrendDraft AI content generation for Sanity Studio
Author: Resonia Inc
Latest version: 0.2.0

# @resonia/sanity-plugin-trenddraft

> AI content generation for Sanity Studio powered by TrendDraft

**Status:** ✅ Published to npm (v0.1.0)

Generate AI-powered content directly in Sanity Studio with your unique voice and style. TrendDraft learns your writing patterns and incorporates your opinions into generated content.

## Features

- **AI Content Generation** - Generate content from titles/topics directly in text fields
- **Style Profiles** - Use your TrendDraft style profiles for consistent voice
- **Opinion Alignment** - AI incorporates your recorded opinions and perspectives
- **Content Goals** - Specify goals (Educate, Convert, Engage, etc.)
- **Field-Level Generation** - Works on text, string, and rich text fields

## Installation

```bash
npm install @resonia/sanity-plugin-trenddraft
# or
pnpm add @resonia/sanity-plugin-trenddraft
# or
yarn add @resonia/sanity-plugin-trenddraft
```

## Configuration

Add it as a plugin in your `sanity.config.ts`:

```typescript
import { defineConfig } from 'sanity';
import { trenddraft } from '@resonia/sanity-plugin-trenddraft';

export default defineConfig({
  // ... other config
  plugins: [
    trenddraft({
      apiKey: process.env.SANITY_STUDIO_TRENDDRAFT_API_KEY!,
      defaultStyleProfile: 'company-voice', // optional
      enableOpinionAlignment: true, // optional, default: true
      fields: ['body', 'excerpt', 'metaDescription'], // optional
      defaultWordCount: 800, // optional, default: 800
    }),
  ],
});
```

### Environment Variables

Create a `.env` file in your Sanity studio:

```bash
SANITY_STUDIO_TRENDDRAFT_API_KEY=your_api_key_here
```

### Configuration Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `apiKey` | `string` | Required | Your TrendDraft API key |
| `defaultStyleProfile` | `string` | - | Default style profile ID |
| `enableOpinionAlignment` | `boolean` | `true` | Include opinions in generation |
| `fields` | `string[]` | `['body', 'content', 'excerpt', 'description']` | Fields to enable TrendDraft on |
| `defaultWordCount` | `number` | `800` | Default target word count |
| `apiUrl` | `string` | - | Custom API URL (for testing) |

## Usage

Once configured, TrendDraft adds a "Generate with TrendDraft" button below enabled text fields:

1. Click "Generate with TrendDraft" on any enabled field
2. Enter a title/topic for your content
3. Select a style profile (optional)
4. Add keywords (optional)
5. Choose content goals (optional)
6. Set target word count
7. Click "Generate"

The generated content will be inserted into the field.

## Getting an API Key

1. Sign up at [trenddraft.xyz](https://trenddraft.xyz/signup)
2. Go to **Settings** → **API Keys**
3. Click "Create API Key"
4. Name it "Sanity Studio"
5. Select scopes:
   - `content:generate` (required)
   - `profiles:read` (required)
   - `opinions:read` (optional)
6. Copy the key (shown only once)

## Advanced Usage

### Custom Input Component

For more control, you can use the input component directly:

```typescript
import { createTrendDraftInput } from '@resonia/sanity-plugin-trenddraft';

const CustomTrendDraftInput = createTrendDraftInput({
  apiKey: process.env.SANITY_STUDIO_TRENDDRAFT_API_KEY!,
  defaultWordCount: 1200,
});
```

### Using Hooks

Build custom integrations with the provided hooks:

```typescript
import { useTrendDraftApi, useStyleProfiles } from '@resonia/sanity-plugin-trenddraft';

function MyComponent() {
  const { api, generateContent, isGenerating, error } = useTrendDraftApi({
    apiKey: process.env.SANITY_STUDIO_TRENDDRAFT_API_KEY!,
  });

  const { profiles, selectedProfile, selectProfile } = useStyleProfiles({
    api,
  });

  // ... your custom implementation
}
```

### Direct API Access

```typescript
import { createApiClient } from '@resonia/sanity-plugin-trenddraft';

const api = createApiClient(process.env.SANITY_STUDIO_TRENDDRAFT_API_KEY!);

// Get profiles
const profiles = await api.getStyleProfiles();

// Generate content
const content = await api.generateContent({
  title: 'My Article Title',
  styleProfileId: 'profile-id',
  targetWordCount: 1000,
});
```

## Supported Field Types

TrendDraft automatically enables on these field types (when field name matches):

**Simple Text Fields:**
- `string`
- `text`

**Rich Text / Portable Text Fields:**
- `array` of `block` (e.g., `blockContent`, `body`)
- Any Portable Text field

**Default Field Names:**
- `body`
- `content`
- `excerpt`
- `description`
- `text`
- `blockContent`

Customize with the `fields` configuration option.

### Portable Text Support

For Portable Text (block content) fields, TrendDraft will:
- Convert generated text to Portable Text block format
- Support Markdown-style headings (## H2, ### H3, #### H4)
- Support blockquotes (> quote)
- Allow appending to existing content or replacing it

Example schema that works with TrendDraft:

```typescript
defineField({
  name: 'body',
  title: 'Body',
  type: 'array',
  of: [
    { type: 'block' },
    { type: 'image' },
    { type: 'code' },
  ],
})
```

## Requirements

- Sanity Studio v3.0.0+
- React 18+
- TrendDraft account with API key

## Support

- [Documentation](https://trenddraft.xyz/docs/sanity)
- [TrendDraft Support](https://trenddraft.xyz/support)
- [GitHub Issues](https://github.com/resonia/sanity-plugin-trenddraft/issues)

## License

MIT © [Resonia Inc](https://resonia.com)