Skip to content

WordPress importer fails on post types with dashes in the slug #170

@ilicfilip

Description

@ilicfilip

Bug

The WordPress importer passes custom post type slugs through to EmDash collection slugs without sanitization. WordPress allows dashes in post type keys (e.g. my-custom-type), but EmDash's SLUG_VALIDATION_PATTERN only allows lowercase letters, numbers, and underscores (/^[a-z][a-z0-9_]*$/). This causes the import to fail for any custom post type containing a dash.

Reproduction

  1. Export a WordPress site that has a custom post type with a dash in the key (e.g. book-review)
  2. Run emdash import wordpress export.xml --execute
  3. Import fails with: collection slug must start with a letter and contain only lowercase letters, numbers, and underscores

Cause

In packages/emdash/src/cli/commands/import/wordpress.ts, the mapPostTypeToCollection function (line ~445) returns unmapped post types as-is:

function mapPostTypeToCollection(postType: string): string {
    const mapping: Record<string, string> = {
        post: "posts",
        page: "pages",
        // ...known mappings
    };
    return mapping[postType] || postType; // no sanitization
}

The slug validation in packages/emdash/src/schema/registry.ts (line 27) then rejects it:

const SLUG_VALIDATION_PATTERN = /^[a-z][a-z0-9_]*$/;

Proposed fix

Sanitize the post type slug in mapPostTypeToCollection before returning it:

function mapPostTypeToCollection(postType: string): string {
    const mapping: Record<string, string> = {
        post: "posts",
        page: "pages",
        // ...
    };
    if (mapping[postType]) return mapping[postType];
    // Convert dashes to underscores to match EmDash slug requirements
    return postType.replace(/-/g, "_");
}

Context

WordPress post type keys: "Must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores." The only incompatibility is the dash, so a simple replace(/-/g, "_") covers all cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions