thewinger / wordpress-to-ndjson

CLI tool that fetch WordPress data to produce ndjson files for sanity.io importation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WordPress to NDJSON formatter

Simple NodeJS CLI-Application that fetch and format data from WordPress, and create NDJSON output to import it in Sanity.io CMS.

Getting started

Download and install the repository locally

git clone wordpress-to-ndjson https://github.com/Junscuzzy/wordpress-to-ndjson
cd wordpress-to-ndjson
npm install

Then run

npm run dev --url https://your-wordpress-site.com

Output format

This script makes a *.ndjson files for the following schema:

// studio/schemas/documents/post.js

export default {
    name: 'post',
    type: 'document',
    title: 'Blog posts',
    fields: [
        {
            name: 'title',
            type: 'string',
            title: 'Title',
        },
        {
            name: 'slug',
            type: 'slug',
            title: 'Slug',
            options: {
            source: 'title',
                maxLength: 96
            }
        },
        {
            name: 'mainImage',
            type: 'mainImage',
            title: 'Main image'
        },
        {
            name: 'excerpt',
            type: 'text',
            rows: 4,
            title: 'Excerpt',
        },
        {
            name: 'categories',
            type: 'array',
            title: 'Categories',
            of: [
                {
                    type: 'reference',
                    to: {
                        type: 'category'
                    }
                }
            ]
        },
        {
            name: 'body',
            type: 'bodyPortableText',
            title: 'Body text'
        }
    ]
}

// studio/schemas/documents/category.js

export default {
    name: 'category',
    type: 'document',
    title: 'Categories',
    fields: [
        {
            name: 'title',
            type: 'string',
            title: 'Title'
        },
        {
            name: 'slug',
            type: 'slug',
            title: 'Slug',
            options: {
                source: 'title',
                maxLength: 96
            }
        },
        {
            name: 'description',
            type: 'text',
            title: 'Description'
        }
    ]
}

Evolution

Actually, your can only fetch posts and post categories. If you want edit the formatting schema or add entities:

  1. Update the Typescript interface model in src/interfaces/{post-type}.ts
  2. Update the formatter following typescript support in src/models/{post-type}.ts
  3. If necessary, edit the index file named src/bin/cli.ts.

You can copy this repository and adapt this following your business logic or submit a PR ;).

To-do

  • CPT support
  • ACF support
  • Custom sanity.io output format
  • Use wp-api instead REST API
  • Authenticated connection to wordPress
  • Add unit tests

License

Un-licensed, you can use, copy, edit, share (...) as you want without credit or permission.

About

CLI tool that fetch WordPress data to produce ndjson files for sanity.io importation


Languages

Language:TypeScript 90.4%Language:JavaScript 9.6%