Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ export API_URL=https://demo.ckan.org/api/3/action/

### CMS

To use a CMS plugin, enable the plugin in your environment.

Example:

in `.env`:
```
PLUGINS="wp"
```

#### Wordpress
The wordpress plugin (`/plugins/wp`) ships with frontend-v2.

Use `WP_URL` environment variable to point to your WordPress instance. For example, we have test wordpress blog here https://edscms.home.blog/ so it would be:

```
Expand Down
2 changes: 1 addition & 1 deletion env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PLUGINS="example"
PLUGINS="example wp"
WP_TOKEN=
NODE_ENV=development
THEME=example
Expand Down
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const i18n = require("i18n")

const config = require('./config')
const dmsRoutes = require('./routes/dms')
const cmsRoutes = require('./routes/cms')
const {loadTheme, loadPlugins} = require('./utils')

module.exports.makeApp = function () {
Expand Down Expand Up @@ -67,7 +66,6 @@ module.exports.makeApp = function () {

// Controllers
app.use([
cmsRoutes(),
dmsRoutes()
])

Expand Down
2 changes: 1 addition & 1 deletion lib/cms.js → plugins/wp/cms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const config = require('../config')
const config = require('../../config')

const wpcom = require('wpcom')(config.get('WP_TOKEN'))

Expand Down
34 changes: 26 additions & 8 deletions routes/cms.js → plugins/wp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@
const express = require('express')
const moment = require('moment')

const cms = require('../lib/cms')
const cms = require('./cms')


module.exports = function () {
const router = express.Router()
module.exports = function (app) {
const Model = new cms.CmsModel()

router.get('/news', listStaticPages)
router.get('/news/:page', showPostPage)
router.get(['/:page', '/:parent/:page'], showStaticPage)
app.get('/', async (req, res) => {
// Get latest 3 blog posts and pass it to home template
const size = 3
let posts = await Model.getListOfPosts(size)
posts = posts.map(post => {
return {
slug: post.slug,
title: post.title,
content: post.content,
published: moment(post.date).format('MMMM Do, YYYY'),
modified: moment(post.modified).format('MMMM Do, YYYY'),
image: post.featured_image
}
})
res.render('home.html', {
title: 'Home',
posts
})
})

app.get('/news', listStaticPages)
app.get('/news/:page', showPostPage)
app.get(['/:page', '/:parent/:page'], showStaticPage)

async function listStaticPages(req, res) {
// Get latest 10 blog posts
Expand Down Expand Up @@ -63,6 +82,7 @@ module.exports = function () {
slug += `-${locale}`
}
try {
console.log('get post')
const post = await Model.getPost(slug)
res.render('static.html', {
slug: post.slug,
Expand All @@ -82,6 +102,4 @@ module.exports = function () {
}
}
}

return router
}
18 changes: 2 additions & 16 deletions routes/dms.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ const bytes = require('bytes')

const config = require('../config')
const dms = require('../lib/dms')
const cms = require('../lib/cms')
const utils = require('../utils')


module.exports = function () {
const router = express.Router()
const Model = new dms.DmsModel(config)
const CmsModel = new cms.CmsModel()

// -----------------------------------------------
// Redirects
Expand All @@ -36,22 +34,10 @@ module.exports = function () {
// -----------------------------------------------

router.get('/', async (req, res) => {
// Get latest 3 blog posts and pass it to home template
const size = 3
let posts = await CmsModel.getListOfPosts(size)
posts = posts.map(post => {
return {
slug: post.slug,
title: post.title,
content: post.content,
published: moment(post.date).format('MMMM Do, YYYY'),
modified: moment(post.modified).format('MMMM Do, YYYY'),
image: post.featured_image
}
})
// If no CMS is enabled, show home page without posts
res.render('home.html', {
title: 'Home',
posts
posts: []
})
})

Expand Down
8 changes: 4 additions & 4 deletions tests/lib/index.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const test = require('ava')

const cms = require('../../lib/cms')
const wp = require('../../plugins/wp/cms')
const dms = require('../../lib/dms')
const config = require('../../config')
const mocks = require('../../fixtures')

mocks.initMocks()

const CmsModel = new cms.CmsModel()
const WPModel = new wp.CmsModel()
const DmsModel = new dms.DmsModel(config)


test('getPost api works', async t => {
t.plan(1)

const slug = 'about'
const result = await CmsModel.getPost(slug)
const result = await WPModel.getPost(slug)

t.is(result.title, 'Welcome to Data Service!')
})
Expand All @@ -24,7 +24,7 @@ test('getPost api works', async t => {
test('getListOfPosts api works', async t => {
t.plan(1)

const result = await CmsModel.getListOfPosts()
const result = await WPModel.getListOfPosts()

t.is(result.length, 2)
})
Expand Down
10 changes: 9 additions & 1 deletion tests/routes/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('Theme defined route does NOT exists when THEME is not set', async t => {
config.set('THEME', 'opendk')
const app = require('../../index').makeApp()
const res = await request(app)
.get('/foo')
.get('/absolutely-not-a-chance')

t.is(res.statusCode, 500)
})
Expand Down Expand Up @@ -69,6 +69,8 @@ test('Missing plugin load is caught and app still loads', async t => {

// CMS
test('About page works', async t => {
config.set('PLUGINS', "wp")
const app = require('../../index').makeApp()
t.plan(2)

const res = await request(app)
Expand All @@ -80,6 +82,9 @@ test('About page works', async t => {


test('News home page works', async t => {
config.set('PLUGINS', "wp")
const app = require('../../index').makeApp()

t.plan(2)

const res = await request(app)
Expand All @@ -91,6 +96,9 @@ test('News home page works', async t => {


test('Single post page works', async t => {
config.set('PLUGINS', "wp")
const app = require('../../index').makeApp()

t.plan(2)

const res = await request(app)
Expand Down