Pages CMS Documentation

repository·main·Indexed 26 days ago

https://github.com/hunvreus/pagescms

An open-source CMS for GitHub repositories optimized for static site generators like Next.js, Astro, Hugo, Nuxt, and Jekyll. Version 2.1.8 supports both hosted and self-hosted deployments. The documentation covers local setup using PostgreSQL and Docker, GitHub App configuration, environment variable management, and the implementation of custom and core fields (Boolean, Code, Date). It also details the Tiptap-based rich text Editor component and Drizzle ORM database configuration.

Tokens
7.3K
Snippets
8
Records
52
Agent score
87%

What's inside pagescms

  1. Set up Pages CMS for local development

    main

    To run Pages CMS locally, you need PostgreSQL, a GitHub App, a local .env.local file, and the repository checked out. Follow these steps:

    1. Clone the repository:

      git clone https://github.com/pagescms/pagescms.git
      cd pagescms
    2. Start PostgreSQL locally using Docker:

      docker run --name pagescms-db -e POSTGRES_USER=pagescms -e POSTGRES_PASSWORD=pagescms -e POSTGRES_DB=pagescms -p 5432:5432 -d postgres:16
    3. Install dependencies:

      npm install
    4. Configure environment variables by creating a .env.local file.

    5. Create a GitHub App using the setup helper:

      npm run setup:github-app -- --base-url http://localhost:3000
    6. Run database migrations:

      npm run db:migrate
    7. Start the application:

      npm run dev
    git clone https://github.com/pagescms/pagescms.git
    cd pagescms
    docker run --name pagescms-db -e POSTGRES_USER=pagescms -e POSTGRES_PASSWORD=pagescms -e POSTGRES_DB=pagescms -p 5432:5432 -d postgres:16
    npm install
    npm run setup:github-app -- --base-url http://localhost:3000
    npm run db:migrate
    npm run dev
  2. Add custom fields to Pages CMS

    main

    You can extend Pages CMS by adding custom fields within the fields/custom/ directory. Each custom field must reside in its own folder and must include an index.ts or index.tsx file.

    Important: Custom field names must be unique. If you use a name that matches a core field, you will override the core field implementation.

  3. Configure Pages CMS environment variables

    main

    Create a .env.local file with the following required and optional variables:

    Required Variables:

    • DATABASE_URL: Connection string for PostgreSQL. Example: postgresql://pagescms:pagescms@localhost:5432/pagescms
    • BETTER_AUTH_SECRET: A random secret for authentication.
    • CRYPTO_KEY: A random secret for cryptographic operations.

    Optional Variables:

    • BASE_URL: The single canonical URL for the app (e.g., https://cms.example.com). In local development, use a public tunnel URL if you need GitHub webhooks to reach your local instance.
    • ADMIN_EMAILS: A comma-separated list of email addresses allowed to access the admin panel.

    Use openssl rand -base64 32 to generate secure secrets.

    DATABASE_URL=postgresql://pagescms:pagescms@localhost:5432/pagescms
    BETTER_AUTH_SECRET=your-random-secret
    CRYPTO_KEY=your-random-secret
    
    # Optional
    BASE_URL=https://cms.example.com
    ADMIN_EMAILS=admin@example.com
  4. Configure EditorProps

    main

    The EditorProps object defines the configuration for the Editor component:

    PropTypeDescription
    valuestringThe initial content of the editor
    onChange(value: string) => voidCallback triggered when content changes
    disabledbooleanIf true, the editor becomes read-only
    format"html" | "markdown"The output format of the editor
    enableImagesbooleanEnables/disables image support
    enableImagePasteDropbooleanEnables/disables pasting and dropping images
    onUploadImageImageUploadHandlerCustom handler for uploading images
    imageFallback"data-url" | "prompt-url" | "none"Mode for handling images when no handler is provided
    maxImageBytesnumberMaximum size allowed for images when using data-url fallback
    onRequestImageImagePickerHandlerHandler for image requests via slash commands
    onPendingUploadsChange(count: number) => voidCallback providing the current number of active uploads
    classNamestringContainer class name
    editorClassNamestringTiptap surface class name
  5. Configure Drizzle ORM with drizzle-kit

    main

    The project uses drizzle-kit for database schema management and migrations. The configuration is defined using defineConfig and requires a PostgreSQL dialect. The configuration relies on the DATABASE_URL environment variable for connection credentials.

    import { defineConfig } from "drizzle-kit";
    
    export default defineConfig({
      dialect: "postgresql",
      schema: "./db/schema.ts",
      out: "./db/migrations",
      strict: true,
      verbose: true,
      dbCredentials: {
        url: process.env.DATABASE_URL!,
      },
    });
  6. Configure the File field

    main

    The File field allows users to manage file paths. It supports single or multiple file selection and can be integrated with a media schema to handle path transformations (swapping input/output prefixes) and extension validation.

    Configuration Options

    • multiple: (boolean) If true, the field accepts an array of file paths instead of a single string.
    • unique: (boolean) When multiple is true, ensures all file paths in the array are unique.
    • required: (boolean) If true, the field cannot be empty.
    • media: (string | boolean)
      • If false, media path transformations are disabled.
      • If a string, specifies the name of the media schema to use for path mapping.
      • If omitted, it defaults to the first schema in the media array of the object.
    • extensions: (string[]) An explicit list of allowed file extensions.
    • categories: (string[]) A list of extension categories (e.g., 'images') to determine allowed extensions via extensionCategories.

    Path Transformation

    When a media configuration is provided, the field automatically uses read and write functions to swap path prefixes between the storage input and the public output paths.

  7. Configure a Date field in Pages CMS

    main

    The Date field allows users to select dates or date-times. It supports two modes based on the time option and allows for custom storage formats and validation constraints.

    Configuration Options

    • time (boolean): If set to true, the field uses datetime-local input and the format yyyy-MM-dd'T'HH:mm. If false or omitted, it uses date input and the format yyyy-MM-dd.
    • format (string): The format string used when saving the date value (e.g., using date-fns tokens). If not provided, it defaults to the input format.
    • min (string): A minimum date constraint. The value must be a valid date string matching the input format.
    • max (string): A maximum date constraint. The value must be a valid date string matching the input format.
    • required (boolean): If true, the field cannot be empty.
  8. Configure the Image field

    main

    The Image field allows users to manage single or multiple image paths. It supports integration with a media configuration schema to handle path prefix swapping (e.g., converting between local development paths and production output paths) and extension validation.

    Key Options

    • multiple: (boolean) If true, the field accepts an array of image paths instead of a single string.
    • unique: (boolean) When multiple is true, setting this to true ensures all image paths in the array are unique after normalization.
    • media: (string | boolean)
      • If false, media path swapping and validation are disabled.
      • If a string, it specifies the name of the media schema to use from the configuration object.
      • If not provided, it defaults to the first media schema found in the configuration.
    • extensions: (string[]) An explicit list of allowed file extensions.
    • categories: (string[]) A list of extension categories (e.g., from extensionCategories) to include.
    • required: (boolean) If true, the field cannot be empty.
  9. Implement a custom field in Pages CMS

    main

    To create a fully functional custom field, your index.ts or index.tsx file should export the following properties:

    • schema: A Zod schema used for validation and format coercion when saving the field.
    • read: A function to transform the field value when reading it from a file (e.g., converting a specific file format into a standard format).
    • write: A function to transform the field value when writing it to a file (e.g., converting a standard format into the specific format required by the file).
    • EditComponent: A React component used for editing the field in the CMS interface.
    • ViewComponent: A React component used to display the field within a collection view.
    • defaultValue: The initial value assigned to the field.
  10. Manage Pages CMS database and cache

    main

    Use the following commands to manage the database state:

    • Run migrations: Apply database schema changes.
      npm run db:migrate
    
    - **Clear cache**: Use this if the cache state is known to be stale or corrupted.
      ```bash
    npm run db:clear-cache
  11. Create a GitHub App for Pages CMS

    main

    Use the setup:github-app script to automate the creation of your GitHub App.

    Command:

    npm run setup:github-app -- [options]

    Options:

    • --base-url <url>: The base URL of your application (e.g., http://localhost:3000).
    • --owner-type <personal|org>: Specify if the owner is a personal account or an organization.
    • --org <slug>: The slug of the organization (required if --owner-type is org).
    • --app-name <string>: The name of the GitHub App.
    • --env <path>: Path to the environment file (e.g., .env.local).
    • --no-open: Prevents the script from automatically opening a browser window.