Git Product home page Git Product logo

wordprismic's Introduction

Wordprismic

npm

Wordprismic is a small and fully configurable Node utility for importing existing Wordpress blogs into the Prismic.io content platform.

Requirements

Make sure you meet the following requirements before using the importer:

  • Wordpress's REST API enabled (included and auto-enabled in core from WP 4.4+)
  • Both Node 7.6+ and Ruby installed
  • Any Wordpress plugins that add or change content models (eg: Advanced Custom Fields, YOAST, etc) hooked up to the REST API, via plugins or otherwise

Configuration

Create a JavaScript configuration file with the following properties

Property Description
wordpress.url The full URL of your wordpress blog
prismic.repo The name of your Prismic repository
prismic.locale The locale of language locale of your imported documents in Prismic (see Settings -> Translations & locales)
prismic.categoriesType (optional) The content type of post categories in prismic, if available
optimizeMediaRequests (optional) Whether to attempt to only fetch required media assets. Shortens import time, but can cause 503 errors on some Wordpress servers.
schema A function to transform Wordpress data to your Prismic content model, see documentation below
module.exports = {
  wordpress: {
    url: 'https://myblog.com'
  },
  prismic: {
    repo: 'myNewBlog',
    locale: 'en-au',
    categoriesType: 'category'
  },
  optimizeMediaRequests: false,
  schema: async function(post, html) {
    return {
      type: 'post',
      uid: post.slug,
      category: {
        id: post.categories[0].prismic.id,
        mask: 'category'
      },
      author: post.author.name,
      title: html.decode(post.title.rendered),
      featured_image: {
        origin: {
          url: post.featured_media.guid.rendered
        },
        alt: post.featured_media.alt_text
      },
      excerpt: await html.parse(post.excerpt.rendered),
      content: await html.parse(post.content.rendered)
    };
  }
};

Defining your schema

The config schema describes how your Wordpress posts map to your Prismic content model. It's written as a function that is given two paramaters:

  1. The imported post from Wordpress
  2. Helper functions html.parse(), which creates Prismic RichText objects out of HTML strings, and html.decode(), which decodes HTML strings with entities

See the Wordpress Posts API Reference for all properties available on the post object provided. However, the following properties on post have been changed by Wordprismic:

  • author is the full user object, rather than just the ID
  • featured_media is the media object object of the asset, rather than just the ID
  • Each item in categories has been populated with a matching Prismic category if it's available (from prismicCategories type in config) as follows: { wordpress: [category], prismic: [document] }

The html.parse() function is asynchronous, so make sure you await it and flag your schema as async.

Importing

You can now run the importer directly from NPM, with the following arguments

Argument Description
--config (-c) Path to your config file
--dest (-d) Location to save zip archive for imorting, defaults to current directory
npx wordprismic -c ./path/to/config.js

Or if you'd prefer, install the module globally first

npm i -g wordprismic

wordprismic -c ./path/to/config.js

Your new Prismic posts will be saved to in a folder called wordprismic-import. Compress the contents (not the folder itself) to a .zip archive, then import it to Prismic.


© MIT Tomorrow

wordprismic's People

Contributors

dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.