项目作者: wkentdag

项目描述 :
wordpress plugin for spike
高级语言: JavaScript
项目地址: git://github.com/wkentdag/spike-wordpress.git
创建时间: 2016-08-22T00:12:46Z
项目社区:https://github.com/wkentdag/spike-wordpress

开源协议:Other

下载


spike wordpress

npm tests dependencies Coverage Status

use wordpress as a backend for your spike static project

installation

  1. npm i -S spike-wordpress

setup

  • create a wordpress site (self-hosted or on wordpress.com)
    • check the 1click wordpress app on digital ocean for a quick cheap self-hosted option
  • install and activate wordpress jetpack plugin
  • make sure the “JSON API” feature is turned on within jetpack
  • :beers:

check out this video demonstrating how you can easily set up a wordpress-powered static site that recompiles whenever you publish a new post or push to github using roots-wordpress, which this project is based on :eyes:

usage

add the plugin to your app.js file…

  1. // app.js
  2. const Wordpress = require('spike-wordpress')
  3. const standard = require('reshape-standard')
  4. const locals = {}
  5. module.exports = {
  6. plugins: [
  7. new Wordpress({
  8. site: 'my_wordpress_site.com',
  9. addDataTo: locals
  10. })
  11. ],
  12. reshape: standard({ locals }),
  13. // ...other config...
  14. }

…and then access your posts as local variables in your view files:

  1. // some_template.sgr
  2. extends(src='layout.sgr')
  3. block(name='content')
  4. h1 My awesome static blog
  5. h2 Recent posts
  6. .recent
  7. each(loop='post, i in wordpress.posts')
  8. if(condition='i < 10')
  9. h1 {{ post.title }}
  10. h2 {{ post.excerpt }}
  11. h3 by {{ post.author.name }} on {{ post.date }}

features

PRs welcome for new features!

filter results with query params

by default the plugin dumps all your posts into a single wordpress.posts array in the view locals, but you can configure multiple queries in the posts config key. pass in an array of config objects with a name (required, so that you can access it in your locals at wordpress[name]) and any parameter supported by the wordpress v1 api:

  1. const locals = {}
  2. new Wordpress({
  3. name: 'my_wordpress_site',
  4. addDataTo: locals,
  5. posts: [
  6. {
  7. name: 'posts'
  8. number: '10' // default limit is 20, max is 100
  9. },
  10. {
  11. name: 'interviews',
  12. category: 'interview',
  13. order: 'ASC',
  14. search: 'some text'
  15. }
  16. ]
  17. })

transform function

you can also include an arbitrary transform function to apply to posts on the fly during the build process (see postTransform hook if you want to manipulate your locals after they’ve already been processed):

  1. new Wordpress({
  2. name: 'my_wordpress_site',
  3. addDataTo: locals,
  4. posts: [{
  5. name: 'funtimes',
  6. transform: (post) => {
  7. post.foo = 'bar'
  8. return post
  9. }
  10. }]
  11. })

posts are run through a generic transform function by default; you can pass transform: false to bypass it and return the raw result

render posts to a template

you can add an optional template param that will render each item in posts[param]
to a specific view template with a configurable output path:

  1. const locals = {}
  2. new Wordpress({
  3. name: 'my_wordpress_site',
  4. addDataTo: locals,
  5. posts: [{
  6. name: 'portfolio',
  7. template: {
  8. path: 'views/portfolio-item.sgr',
  9. output: (item) => `posts/${item.slug}.html`
  10. }
  11. }]
  12. })

Any post that gets rendered to a template gets a convenient _url key tacked on as well (the result of the output function you must pass):

  1. // portfolio-item.sgr
  2. a(href={{ item._url }})
  3. h1 {{ item.title }}
  4. img(src={{ item.featured_image }})
  5. p {{ item.content }}

save output to json

pass a file name to the json param to write the locals.wordpress output to:

  1. const locals = {}
  2. new Wordpress({
  3. name: 'my_wordpress_site',
  4. addDataTo: locals,
  5. json: 'data.json'
  6. })

postTransform hook

add a postTransform hook to modify posts and locals before
your templates get compiled, but after each post‘s (optional) transform function runs:

  1. const fs = require('fs')
  2. const locals = {}
  3. new Wordpress({
  4. name: 'my_wordpress_site',
  5. addDataTo: locals,
  6. posts: [{
  7. name: 'posts',
  8. transform: (post) => { // this runs first...
  9. return post.title
  10. }
  11. }],
  12. hooks: {
  13. postTransform: (posts, locals) => {
  14. posts.map(p => p.toUpperCase())
  15. return [posts, locals] // posts = ['TITLE1', 'TITLE 2', etc]
  16. }
  17. }
  18. })

testing

The tests depend on a jetpack-enabled test wordpress instance.
If you are developing a new feature and want to run the test suite either submit a PR (they’ll be auto run by travis), or file an issue and I’ll get you access to the test server :)

  1. # install module dependencies
  2. npm i
  3. # create a config file from the boilerplate
  4. mv .env.sample .env
  5. # run the tests
  6. npm test