Skip to content

Content Types

This page documents the core content type definitions that form the foundation of the ReX content system.

Core Content Interface

typescript
/**
 * Represents a unit of content with generic data type
 * @template T The type of the content data, defaults to string
 */
interface Content<T = string> {
  /**
   * The actual content data
   */
  data: T

  /**
   * MIME type of the content
   */
  contentType: string

  /**
   * Metadata associated with the content
   */
  metadata: ContentMetadata
}

The Content<T> interface is the central type in the content system. It uses a generic type parameter to support different content representations, defaulting to string for text-based content.

Content Specializations

The system provides several specialized content types based on the generic interface:

Text Content

typescript
/**
 * Represents text-based content (Markdown, HTML, plain text, etc.)
 */
type TextContent = Content<string>

Binary Content

typescript
/**
 * Represents binary content (images, PDFs, etc.)
 */
type BinaryContent = Content<Uint8Array>

/**
 * Node.js-specific binary content representation
 */
type NodeBinaryContent = Content<Buffer>

Structured Content

typescript
/**
 * Represents JSON or other structured content
 */
type JsonContent = Content<object>

/**
 * Represents structured content with a schema
 */
interface SchemaContent<T extends object> extends Content<T> {
  metadata: ContentMetadata & {
    schema?: string
  }
}

Content Collections

typescript
/**
 * Represents a collection of content items
 */
interface ContentCollection<T = string> {
  /**
   * Map of URI to content
   */
  [uri: string]: Content<T>
}

/**
 * Represents a paginated collection of content items
 */
interface PaginatedContentCollection<T = string> {
  /**
   * The items in the current page
   */
  items: ContentCollection<T>

  /**
   * Total number of items across all pages
   */
  total: number

  /**
   * Current page number (1-based)
   */
  page: number

  /**
   * Number of items per page
   */
  pageSize: number

  /**
   * Whether there are more pages
   */
  hasMore: boolean
}

Content URI Types

typescript
/**
 * String representing a content URI
 */
type ContentURI = string

/**
 * Parsed representation of a content URI
 */
interface ParsedContentURI {
  /**
   * Original URI string
   */
  original: string

  /**
   * Path segments
   */
  segments: string[]

  /**
   * File extension (if any)
   */
  extension?: string

  /**
   * Content type inferred from extension
   */
  contentType?: string
}

Type Guards

The system provides type guards for checking content types:

typescript
/**
 * Checks if content is text-based
 */
function isTextContent(content: Content<unknown>): content is Content<string> {
  return typeof content.data === 'string'
}

/**
 * Checks if content is binary
 */
function isBinaryContent(
  content: Content<unknown>
): content is Content<Uint8Array> | Content<Buffer> {
  return (
    content.data instanceof Uint8Array ||
    (typeof Buffer !== 'undefined' && content.data instanceof Buffer)
  )
}

/**
 * Checks if content is structured (object)
 */
function isJsonContent(content: Content<unknown>): content is Content<object> {
  return (
    typeof content.data === 'object' &&
    content.data !== null &&
    !(content.data instanceof Uint8Array) &&
    !(typeof Buffer !== 'undefined' && content.data instanceof Buffer)
  )
}

/**
 * Checks if content is Markdown
 */
function isMarkdownContent(
  content: Content<unknown>
): content is Content<string> {
  return (
    isTextContent(content) &&
    (content.contentType === 'text/markdown' ||
      content.contentType === 'text/x-markdown')
  )
}

/**
 * Checks if content is MDX
 */
function isMdxContent(content: Content<unknown>): content is Content<string> {
  return (
    isTextContent(content) &&
    (content.contentType === 'text/mdx' || content.contentType === 'text/x-mdx')
  )
}

Content Reference Types

typescript
/**
 * Types of content references
 */
type ContentReferenceType = 'embed' | 'link' | 'dependency'

/**
 * Represents a reference to another content item
 */
interface ContentReference {
  /**
   * URI of the referenced content
   */
  uri: ContentURI

  /**
   * Type of reference
   */
  type: ContentReferenceType

  /**
   * Optional title for the reference
   */
  title?: string

  /**
   * Optional description of the reference
   */
  description?: string
}

Usage Examples

Creating Different Content Types

typescript
// Text content (Markdown)
const markdownContent: Content<string> = {
  data: '# Hello World\n\nThis is a sample post.',
  contentType: 'text/markdown',
  metadata: {
    title: 'Hello World',
    createdAt: new Date(),
  },
}

// Binary content (image)
const imageContent: Content<Uint8Array> = {
  data: new Uint8Array([
    /* binary data */
  ]),
  contentType: 'image/png',
  metadata: {
    title: 'Logo',
    width: 200,
    height: 100,
  },
}

// JSON content
const configContent: Content<object> = {
  data: {
    apiKey: 'abc123',
    endpoint: 'https://api.example.com',
    timeout: 5000,
  },
  contentType: 'application/json',
  metadata: {
    title: 'API Configuration',
    updatedAt: new Date(),
  },
}

Working with Content References

typescript
// Content with references
const articleWithImages: Content<string> = {
  data: '# Article with Images\n\n![Image 1](image1.png)\n\n![Image 2](image2.png)',
  contentType: 'text/markdown',
  metadata: {
    title: 'Article with Images',
    references: [
      { uri: 'image1.png', type: 'embed', title: 'First image' },
      { uri: 'image2.png', type: 'embed', title: 'Second image' },
    ],
  },
}

// Extracting references
const references = articleWithImages.metadata.references || []
const embedRefs = references.filter(ref => ref.type === 'embed')

Migrating from Legacy Types

Prior to ADR-009 Unified Type System, the content system used separate interfaces for different content types. Use this migration guide when updating older code:

Legacy TypeNew Type
RawContentContent<string>
BinaryContentContent<Uint8Array> or Content<Buffer>
JsonContentContent<object>
MarkdownContentContent<string> with appropriate contentType

Released under the MIT License.