Browse Source

rewrite latest posts to use promises (#203)

pull/208/head
Cian Butler 4 years ago
committed by GitHub
parent
commit
b71ba03863
4 changed files with 59 additions and 66 deletions
  1. +5
    -4
      .eslintrc
  2. +2
    -2
      lib/emailNewPosts.js
  3. +42
    -47
      lib/getLatestPosts.js
  4. +10
    -13
      routes/posts.js

+ 5
- 4
.eslintrc View File

@@ -2,12 +2,8 @@
"extends": "standard",
"env": {
"es6": true,
"jest": true,
"node": true
},
"globals": {
"GENTLY": true
},
"rules": {
"no-multi-spaces": 0,
"comma-dangle": [
@@ -36,6 +32,11 @@
"no-var": [
"error"
],
'prefer-arrow-callback': [
'error', {
'allowNamedFunctions': true,
},
],
"prefer-const": [
"error",
{


+ 2
- 2
lib/emailNewPosts.js View File

@@ -18,8 +18,8 @@ const configFile = fs.readFileSync('_config.yml', 'utf8');
const config = yaml.safeLoad(configFile).server;
const sendMailDefault = true;

const postsDirectory = path.join(process.cwd(), 'source/_posts');
const emailLogFilename = path.join(process.cwd(), 'logs/email_update_log');
const postsDirectory = path.join(process.cwd(), 'source', '_posts');
const emailLogFilename = path.join(process.cwd(), 'logs', 'email_update_log');
const mailingListFilename = path.join(process.cwd(), 'mailing_list');

function sendEmail ({ senderName, subject, body }, address, callback) {


+ 42
- 47
lib/getLatestPosts.js View File

@@ -14,59 +14,54 @@ const config = yaml.safeLoad(configFile).server;

const postsDirectory = path.join(process.cwd(), 'source/_posts');

function getLatestPosts (options, callback) {
options.offset = options.offset || 0;
options.limit = options.limit || 10;
options.include = options.include || [];
const getLatestPosts = (options) => {
return new Promise((resolve, reject) => {
options.offset = options.offset || 0;
options.limit = options.limit || 10;
options.include = options.include || [];

fs.readdir(postsDirectory, (err, filenames) => {
if (err) {
return callback(err);
}

const postFilenames = filenames.filter(filename => filename.indexOf('.md') !== -1);
const fullpaths = postFilenames.map(filename => path.join(postsDirectory, filename));
const slugs = postFilenames.map(filename => filename.slice(0, -3));

async.map(fullpaths, readFileAsString, (err, posts) => {
fs.readdir(postsDirectory, (err, filenames) => {
if (err) {
return callback(err);
return reject(err);
}

const postDataList = posts.map((post, index) => {
const frontMatter = parseFrontMatter(post);
const directory = moment(frontMatter.date).format('YYYY/MM/DD');
const permalink = `${config.siteroot}/${path.join(directory, slugs[index])}`;
const thumbnail = frontMatter.thumbnail ? `${permalink}/${frontMatter.thumbnail}` : void 0;

const data = {
title : frontMatter.title,
date : frontMatter.date,
author: frontMatter.author,
tags : frontMatter.tags,
permalink,
thumbnail,
};

// strip out images
const content = frontMatter._content.split(/!\[[\w\W]*]\([\w\W]*\)/).join('');

if (options.include.indexOf('content') !== -1) {
data.content = content.split('<!-- more -->').join('');
const postFilenames = filenames.filter(filename => filename.indexOf('.md') !== -1);
const fullpaths = postFilenames.map(filename => path.join(postsDirectory, filename));
const slugs = postFilenames.map(filename => filename.slice(0, -3));
async.map(fullpaths, readFileAsString, (err, posts) => {
if (err) {
return reject(err);
}

if (options.include.indexOf('excerpt') !== -1) {
data.excerpt = content.split('<!-- more -->')[0];
}

return data;
const postDataList = posts.map((post, index) => {
const frontMatter = parseFrontMatter(post);
const directory = moment(frontMatter.date, 'YYYY/MM/DD').format('YYYY/MM/DD');
const permalink = `${config.siteroot}/${path.join(directory, slugs[index])}`;
const thumbnail = frontMatter.thumbnail ? `${permalink}/${frontMatter.thumbnail}` : void 0;

const data = {
title : frontMatter.title,
date : frontMatter.date,
author: frontMatter.author,
tags : frontMatter.tags,
permalink,
thumbnail,
};

// strip out images
const content = frontMatter._content.split(/!\[[\w\W]*]\([\w\W]*\)/).join('');
if (options.include.indexOf('content') !== -1) {
data.content = content.split('<!-- more -->').join('');
}
if (options.include.indexOf('excerpt') !== -1) {
data.excerpt = content.split('<!-- more -->')[0];
}
return data;
});

const selectedPostData = orderBy(postDataList, ({ date }) => new Date(date)).reverse().slice(options.offset, options.offset + options.limit);
resolve(selectedPostData);
});

const selectedPostData = orderBy(postDataList, ({ date }) => new Date(date)).reverse().slice(options.offset, options.offset + options.limit);

callback(null, selectedPostData);
});
});
}
};

module.exports = getLatestPosts;

+ 10
- 13
routes/posts.js View File

@@ -9,19 +9,16 @@ const getLatestPosts = require('../lib/getLatestPosts');
* - include (comma-separated list possibly including 'content,excerpt')
*/
router.get('/posts', ({ query }, res) => {
getLatestPosts(
{
offset : parseInt(query.offset),
limit : parseInt(query.limit),
include: (query.include || '').split(','),
},
(err, posts) => {
if (err) {
return res.status(500).json(err).end();
}
res.json(posts).end();
}
);
getLatestPosts({
offset : parseInt(query.offset),
limit : parseInt(query.limit),
include: (query.include || '').split(','),
}).then((posts) => {
res.json(posts).end();
})
.catch((err) => {
return res.status(500).json(err).end();
});
});

module.exports = router;

Loading…
Cancel
Save