@nakedcreativity/cee-ms-node 0.3.24 → 0.3.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +82 -31
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -114,8 +114,6 @@ function ceems(config) {
114
114
  path = this.match[0]
115
115
  }
116
116
 
117
-
118
-
119
117
  if(site.pages[path]){
120
118
  var page = site.pages[path]
121
119
 
@@ -160,19 +158,23 @@ function ceems(config) {
160
158
  render: function(scope, hash){
161
159
  // var filepath = Liquid.evalValue(this.match[1]+'.liquid', scope);
162
160
 
163
- //console.log('look for data collection', this.match[0])
164
-
165
- if(site.collections[this.match[0]]){
166
- //console.log(site.collections[this.match[0]]._id)
167
- var collection = JSON.parse(fs.readFileSync('./data/'+site.collections[this.match[0]]._id+'.json', 'utf8'));
161
+ var collectionName = Liquid.evalValue(this.match[0], scope)
162
+
163
+ if(site.collections[collectionName]){
164
+ //console.log(site.collections[collection]._id)
165
+ var collection = JSON.parse(fs.readFileSync('./data/'+site.collections[collectionName]._id+'.json', 'utf8'));
168
166
 
169
167
  var scopeObj = {}
170
- scopeObj[this.match[0]] = collection.data
168
+ scopeObj['collection'] = collection
171
169
  scope.push(scopeObj)
172
170
 
173
171
  //console.log(scope)
174
-
175
- return engine.handleCache(this.match[1]+'.liquid')
172
+ var template = Liquid.lexical.parseLiteral(this.match[1])
173
+ if (!template) {
174
+ template = this.match[1]
175
+ }
176
+
177
+ return engine.handleCache(template+'.liquid')
176
178
  .then((templates) => {
177
179
  return engine.renderer.renderTemplates(templates, scope);
178
180
  })
@@ -245,6 +247,11 @@ function ceems(config) {
245
247
  render: function(scope, hash) {
246
248
  var image = Liquid.evalExp(this.args[0], scope);
247
249
  //console.log(image)
250
+
251
+ if(!image){
252
+ return Promise.resolve('')
253
+ }
254
+
248
255
  if(this.args[1] && image.variants && image.variants[this.args[1]]){
249
256
  var src = image.variants[this.args[1]]
250
257
  }else {
@@ -261,6 +268,11 @@ function ceems(config) {
261
268
  },
262
269
  render: function(scope, hash) {
263
270
  var image = Liquid.evalExp(this.args[0], scope);
271
+
272
+ if(!image){
273
+ return Promise.resolve('')
274
+ }
275
+
264
276
  //console.log(image)
265
277
  if(this.args[1] && image.variants && image.variants[this.args[1]]){
266
278
  var src = image.variants[this.args[1]]
@@ -311,15 +323,31 @@ function ceems(config) {
311
323
  },
312
324
  render: function(scope, hash){
313
325
 
314
- console.log('render blog', this.match[0])
315
- console.log(site.blogs[this.match[0]])
326
+ var articlesPerPage = 10
316
327
 
317
328
  if(!site.blogs[this.match[0]]){
318
- console.log('no match')
319
329
  return
320
330
  }
321
- console.log(this.match[1], 'look for liquid template')
322
- scope.push(site.blogs[this.match[0]])
331
+ var blog = site.blogs[this.match[0]]
332
+ if(scope.scopes[0].page.pagination){
333
+ blog.pagination = scope.scopes[0].page.pagination
334
+ }else {
335
+ blog.pagination = 1
336
+ }
337
+
338
+ blog.start = articlesPerPage * (blog.pagination - 1)
339
+ blog.end = blog.start + articlesPerPage
340
+
341
+ blog.lastPage = Math.ceil(blog.articles.length / articlesPerPage)
342
+
343
+ scope.push(blog)
344
+
345
+ // is page greater than the number of articles?
346
+ // throw a 404
347
+ if(blog.start >= blog.articles.length){
348
+ throw new Error('This page does not exist')
349
+ }
350
+
323
351
  return engine.handleCache(this.match[1]+'.liquid')
324
352
  .then((templates) => {
325
353
  return engine.renderer.renderTemplates(templates, scope);
@@ -327,7 +355,10 @@ function ceems(config) {
327
355
  .then((html) => {
328
356
  scope.pop();
329
357
  return html;
330
- });
358
+ })
359
+ .catch((error) => {
360
+ throw error
361
+ })
331
362
  }
332
363
 
333
364
  })
@@ -386,6 +417,16 @@ ceems.prototype.load = function(req, res){
386
417
 
387
418
  site = this.site //????
388
419
 
420
+ // this may be better in middleware?
421
+ // test if path matches a pagination url
422
+ var regex = new RegExp("(.+)(\/[0-9]{1,})$")
423
+ if(regex.test(path)){
424
+ // looks like a blog url
425
+ var matches = path.match(regex)
426
+ var path = matches[1]
427
+ var pagination = parseInt(matches[2].substring(1))
428
+ }
429
+
389
430
  if(this.site.pages[path]){
390
431
  var page = this.site.pages[path]
391
432
 
@@ -396,6 +437,10 @@ ceems.prototype.load = function(req, res){
396
437
  var page = JSON.parse(fs.readFileSync('./pages/'+page._id+'.json', 'utf8'));
397
438
  page.fullpath = this.site.pages[path].url
398
439
  page.path = page.fullpath.split('/')
440
+
441
+ if(pagination)
442
+ page.pagination = pagination
443
+
399
444
  return page
400
445
 
401
446
  }
@@ -417,17 +462,17 @@ ceems.prototype.render = function(req, res, page=null) {
417
462
  page = this.load(req, res)
418
463
  }
419
464
  // get any external data, and save this into the page
420
- if(page.external){
421
- _.each(page.external, function(ext, key){
465
+ // if(page.external){
466
+ // _.each(page.external, function(ext, key){
422
467
 
423
- try {
424
- page.external[key].data = JSON.parse(fs.readFileSync('./'+ext.file, 'utf8'));
425
- }catch(error){
426
- console.log(error)
427
- }
468
+ // try {
469
+ // page.external[key].data = JSON.parse(fs.readFileSync('./'+ext.file, 'utf8'));
470
+ // }catch(error){
471
+ // console.log(error)
472
+ // }
428
473
 
429
- })
430
- }
474
+ // })
475
+ // }
431
476
 
432
477
  // if there are whitelisted cookies, add these to the page so they can be rendered
433
478
  if(this.config.whitelistedCookies){
@@ -438,7 +483,8 @@ ceems.prototype.render = function(req, res, page=null) {
438
483
  }
439
484
  })
440
485
  }
441
-
486
+
487
+ console.log('pagination, ', page.pagination)
442
488
 
443
489
  var renderedRegions = {}
444
490
  var jobs = []
@@ -476,7 +522,10 @@ ceems.prototype.render = function(req, res, page=null) {
476
522
 
477
523
  renderedRegions[key][index] = html
478
524
  return resolve();
479
- });
525
+ })
526
+ .catch(error=>{
527
+ reject(error)
528
+ })
480
529
  }))
481
530
 
482
531
  }, this)
@@ -511,7 +560,10 @@ ceems.prototype.render = function(req, res, page=null) {
511
560
  page.modules[key] = html
512
561
  return resolve();
513
562
 
514
- });
563
+ })
564
+ .catch(error=>{
565
+ reject(error)
566
+ })
515
567
  }))
516
568
 
517
569
  }, this)
@@ -540,11 +592,10 @@ ceems.prototype.render = function(req, res, page=null) {
540
592
  res.send(html)
541
593
  })
542
594
 
543
- }, reason => {
544
- console.log(reason)
545
595
  }).catch(error=>{
596
+ console.log('error in')
546
597
  console.log(error)
547
- res.status(500).send('Internal server error');
598
+ res.status(500).send(error.message);
548
599
  })
549
600
  }
550
601
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakedcreativity/cee-ms-node",
3
- "version": "0.3.24",
3
+ "version": "0.3.26",
4
4
  "description": "Cee.ms for Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {