Skip to content

Commit 8ce8ea1

Browse files
prettier blogpost
1 parent 9525500 commit 8ce8ea1

File tree

4 files changed

+103
-103
lines changed

4 files changed

+103
-103
lines changed
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module.exports = function configure(config) {
2-
32
config.module.rules.push({
43
test: /\.post$/,
54
exclude: /node_modules/,
6-
use: [{
7-
loader: '@sa-labs/leo-plugin-blogpost/loader'
8-
},{
9-
loader: '@sa-labs/leo-plugin-markdown/loader'
10-
}, {
11-
loader: 'frontmatter-loader'
12-
}]
5+
use: [
6+
{ loader: "@sa-labs/leo-plugin-blogpost/loader" },
7+
{ loader: "@sa-labs/leo-plugin-markdown/loader" },
8+
{ loader: "frontmatter-loader" }
9+
]
1310
});
1411

1512
return config;
16-
}
13+
};
Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
'use strict';
2-
var fs = require('fs');
3-
var mkSlug = require('slug');
4-
var loaderUtils = require('loader-utils');
5-
var excerpt = require('excerpt-html');
6-
var moment = require('moment');
7-
var sanitizeHTML = require('sanitize-html');
8-
var debug = require('debug')('leo:plugin-blogpost:loader');
9-
var fileLoader = require('file-loader');
1+
"use strict";
2+
var fs = require("fs");
3+
var mkSlug = require("slug");
4+
var loaderUtils = require("loader-utils");
5+
var excerpt = require("excerpt-html");
6+
var moment = require("moment");
7+
var sanitizeHTML = require("sanitize-html");
8+
var debug = require("debug")("leo:plugin-blogpost:loader");
9+
var fileLoader = require("file-loader");
1010

1111
function parseDate(str) {
1212
if (!str) {
1313
// If there's no date, use right now
14-
debug('using now')
15-
return moment.utc()
14+
debug("using now");
15+
return moment.utc();
1616
} else {
17-
return moment.utc(str, ['MMM Do, YYYY'])
17+
return moment.utc(str, [ "MMM Do, YYYY" ]);
1818
}
1919
}
2020

2121
function slugify(str) {
22-
return mkSlug(str, { mode: 'rfc3986' });
22+
return mkSlug(str, { mode: "rfc3986" });
2323
}
2424

2525
module.exports = function(json) {
26-
debug('loading');
26+
debug("loading");
2727
// Signal to webpack this is cacheable
2828
this.cacheable();
2929
/**
3030
* Get the filename for the currently loading content
3131
* given `/whatever/post-a.post`, will return `post-a`
3232
*/
33-
var filename = loaderUtils.interpolateName(this, '[name]', {
34-
content: json
35-
});
36-
var headerImagePath = loaderUtils.interpolateName(this, '[path]header.png', {
33+
var filename = loaderUtils.interpolateName(this, "[name]", { content: json });
34+
var headerImagePath = loaderUtils.interpolateName(this, "[path]header.png", {
3735
content: json
3836
});
3937

4038
var headerImg;
4139
// TODO: check if header image exists via headerImagePath
42-
if(filename === 'index') {
40+
if (filename === "index") {
4341
try {
4442
fs.accessSync(headerImagePath, fs.F_OK);
4543
this.addDependency(headerImagePath);
@@ -48,19 +46,17 @@ module.exports = function(json) {
4846
// console.log(this);
4947
// console.log(headerImg);
5048
} catch (e) {
51-
console.log('doesnt exist', e);
49+
console.log("doesnt exist", e);
5250
}
5351
}
5452
// Categories are used to generate archival pages
55-
var category = {
56-
display: json.attributes.category || 'Uncategorized'
57-
}
53+
var category = { display: json.attributes.category || "Uncategorized" };
5854
category.slug = slugify(category.display);
5955

6056
// Ensure a title exists
6157
var title = json.attributes.title;
6258
// if there's no title and the filename is not index, use it
63-
if(!json.attributes.title && filename !== 'index') {
59+
if (!json.attributes.title && filename !== "index") {
6460
filename;
6561
}
6662

@@ -75,19 +71,19 @@ module.exports = function(json) {
7571
*/
7672
var url = json.attributes.url;
7773
if (!url) {
78-
url = '/' + slug;
74+
url = "/" + slug;
7975
}
8076

8177
// Momentize the publish date
8278
const publishedAt = parseDate(json.attributes.publishedAt);
83-
if(!publishedAt.isValid()) {
84-
throw new Error(title, 'has an invalid `publishedAt` date');
79+
if (!publishedAt.isValid()) {
80+
throw new Error(title, "has an invalid `publishedAt` date");
8581
}
8682
// If there's no updatedAt value, use publishedAt
8783
let updatedAt = json.attributes.updatedAt;
8884
updatedAt = updatedAt ? parseDate(updatedAt) : publishedAt;
89-
if(!updatedAt.isValid()) {
90-
throw new Error(title, 'has an invalid `updatedAt` date');
85+
if (!updatedAt.isValid()) {
86+
throw new Error(title, "has an invalid `updatedAt` date");
9187
}
9288

9389
/**
@@ -97,16 +93,14 @@ module.exports = function(json) {
9793
* an average word per minute reading pace.
9894
*/
9995
var timeToRead;
100-
if(json.body) {
101-
var pureText = sanitizeHTML(json.body, {
102-
allowTags: []
103-
});
96+
if (json.body) {
97+
var pureText = sanitizeHTML(json.body, { allowTags: [] });
10498
var avgWPM = 265;
105-
var wordCount = pureText.split(' ').length;
99+
var wordCount = pureText.split(" ").length;
106100
// timeToRead in minutes
107101
timeToRead = Math.round(wordCount / avgWPM);
108102
// super hacky way to make sure "0 min read" never happens
109-
if(timeToRead === 0) {
103+
if (timeToRead === 0) {
110104
timeToRead = 1;
111105
}
112106
}
@@ -119,19 +113,19 @@ module.exports = function(json) {
119113
...json,
120114
attributes: {
121115
...json.attributes,
122-
contentType: 'leo-blogpost',
116+
contentType: "leo-blogpost",
123117
category: category,
124-
publishedAt: publishedAt.format('MMM Do, YYYY'),
125-
updatedAt: updatedAt.format('MMM Do, YYYY'),
118+
publishedAt: publishedAt.format("MMM Do, YYYY"),
119+
updatedAt: updatedAt.format("MMM Do, YYYY"),
126120
slug: slug,
127121
url: url,
128122
excerpt: excerpt(json.body),
129123
timeToRead: timeToRead
130124
}
131125
};
132126

133-
if(headerImg) {
134-
debug('headerImg', headerImg);
127+
if (headerImg) {
128+
debug("headerImg", headerImg);
135129
/**
136130
* If headerImg exists, we were able to access the header.png
137131
* file. This means we should include it as a require, so it
@@ -142,12 +136,14 @@ module.exports = function(json) {
142136
module.exports = Object.assign(${JSON.stringify(finalContent)},
143137
{
144138
attributes: Object.assign(
145-
${JSON.stringify(finalContent.attributes)},
139+
${JSON.stringify(
140+
finalContent.attributes
141+
)},
146142
{ headerImage: '/' + require('${headerImagePath}') }
147143
)
148144
})
149145
`;
150146
} else {
151-
return 'module.exports = ' + JSON.stringify(finalContent);
147+
return "module.exports = " + JSON.stringify(finalContent);
152148
}
153-
}
149+
};
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
const oDebug = require('debug');
2-
const debug = oDebug('leo:plugin-blogpost:process');
1+
const oDebug = require("debug");
2+
const debug = oDebug("leo:plugin-blogpost:process");
33
/**
44
* Filter drafts out of the dataset by default
55
* But keep them if we're told to
66
*/
77
function maybeFilterDrafts(obj) {
8-
const isBlogPost = obj.attributes.contentType === 'leo-blogpost';
8+
const isBlogPost = obj.attributes.contentType === "leo-blogpost";
99
const shouldRenderDrafts = Boolean(process.env.BLOGPOST_RENDER_DRAFTS);
1010

11-
if(isBlogPost && !shouldRenderDrafts) {
11+
if (isBlogPost && !shouldRenderDrafts) {
1212
// Filter out any drafts if we shouldn't renderDrafts
13-
if(obj.attributes.status === 'draft') {
14-
debug('filtering out draft: ', obj.attributes.title || obj.attributes.slug);
13+
if (obj.attributes.status === "draft") {
14+
debug(
15+
"filtering out draft: ",
16+
obj.attributes.title || obj.attributes.slug
17+
);
1518
}
1619

17-
return obj.attributes.status !== 'draft';
20+
return obj.attributes.status !== "draft";
1821
} else {
1922
// passthrough for any non-blogposts since we shouldn't be handling them
20-
return true
23+
return true;
2124
}
2225
}
2326

2427
module.exports = function(data, cb) {
2528
cb(null, data.filter(maybeFilterDrafts));
26-
}
29+
};

packages/leo-plugin-blogpost/src/schema.js

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,75 @@ import graphql, {
44
GraphQLInt,
55
GraphQLString,
66
GraphQLList
7-
} from 'graphql/type';
7+
} from "graphql/type";
88

99
import {
1010
connectionDefinitions,
1111
connectionFromArray,
1212
connectionArgs
13-
} from 'graphql-relay';
13+
} from "graphql-relay";
1414

15-
import find from 'lodash/find';
16-
import filter from 'lodash/filter';
17-
import oDebug from 'debug';
18-
import moment from 'moment';
19-
const debug = oDebug('leo-plugin-blogpost:schema');
15+
import find from "lodash/find";
16+
import filter from "lodash/filter";
17+
import oDebug from "debug";
18+
import moment from "moment";
19+
const debug = oDebug("leo-plugin-blogpost:schema");
2020

2121
const BlogPostAttributesType = new GraphQLObjectType({
22-
name: 'BlogPostAttributes',
22+
name: "BlogPostAttributes",
2323
fields: {
2424
title: {
2525
type: GraphQLString,
26-
description: 'Display title for the BlogPost'
26+
description: "Display title for the BlogPost"
2727
},
2828
slug: {
2929
type: GraphQLString,
30-
description: 'url-safe string for use in URLs. Can be derived from Title, Filename or specified in Frontmatter'
30+
description: "url-safe string for use in URLs. Can be derived from Title, Filename or specified in Frontmatter"
3131
},
3232
updatedAt: {
3333
type: GraphQLString,
34-
description: 'The most recent time that this BlogPost was updated, as `MMM Do, YYYY`'
34+
description: "The most recent time that this BlogPost was updated, as `MMM Do, YYYY`"
3535
},
3636
publishedAt: {
3737
type: GraphQLString,
38-
description: 'The time that this BlogPost was published, as `MMM Do, YYYY`'
38+
description: "The time that this BlogPost was published, as `MMM Do, YYYY`"
3939
},
4040
url: {
4141
type: GraphQLString,
42-
description: 'The absolute pathname of the BlogPost, ex. `/post`'
42+
description: "The absolute pathname of the BlogPost, ex. `/post`"
4343
},
4444
canonicalURL: {
4545
type: GraphQLString,
46-
description: 'The Canonical URL for this content. Can be used to deprecate pages for SEO when you want to keep the old content around.'
46+
description: "The Canonical URL for this content. Can be used to deprecate pages for SEO when you want to keep the old content around."
4747
},
4848
excerpt: {
4949
type: GraphQLString,
50-
description: 'A short excerpt of the `body` content'
50+
description: "A short excerpt of the `body` content"
5151
},
5252
timeToRead: {
5353
type: GraphQLInt,
54-
description: 'The time it takes the average person to read this post in minutes'
54+
description: "The time it takes the average person to read this post in minutes"
5555
},
5656
headerImage: {
5757
type: GraphQLString,
58-
description: 'header.png in the post folder.'
58+
description: "header.png in the post folder."
5959
}
6060
}
61-
})
61+
});
6262

6363
const BlogPostType = new GraphQLObjectType({
64-
name: 'BlogPost',
65-
description: 'A Blog Post written in Markdown with Frontmatter.',
64+
name: "BlogPost",
65+
description: "A Blog Post written in Markdown with Frontmatter.",
6666
fields: {
6767
attributes: {
6868
type: BlogPostAttributesType,
69-
description: 'Metadata about the blog post'
69+
description: "Metadata about the blog post"
7070
},
7171
rawBody: {
7272
type: GraphQLString,
73-
description: 'The raw Markdown, excluding any frontmatter'
73+
description: "The raw Markdown, excluding any frontmatter"
7474
},
75-
body: {
76-
type: GraphQLString,
77-
description: 'The Markdown rendered as HTML'
78-
}
75+
body: { type: GraphQLString, description: "The Markdown rendered as HTML" }
7976
}
8077
});
8178

@@ -86,30 +83,37 @@ const { connectionType: BlogPostConnection } = connectionDefinitions({
8683

8784
module.exports = function(data) {
8885
// The set of posts we should return via the API
89-
const allPosts = data.filter(post => post.attributes.contentType === 'leo-blogpost')
90-
// sort by updatedAt date
91-
.sort((postA, postB) => {
92-
const a = moment.utc(postA.attributes.updatedAt, 'MMM Do, YYYY');
93-
const b = moment.utc(postB.attributes.updatedAt, 'MMM Do, YYYY');
94-
return b.diff(a);
95-
});
86+
const allPosts = data
87+
.filter(post => post.attributes.contentType === "leo-blogpost")
88+
.sort((postA, postB) => {
89+
const a = moment.utc(postA.attributes.updatedAt, "MMM Do, YYYY");
90+
const b = moment.utc(postB.attributes.updatedAt, "MMM Do, YYYY");
91+
return b.diff(a);
92+
});
9693

9794
// Get a single post by slug
98-
const getPost = (slug) => {
99-
const post = find(allPosts, ({ attributes: a }) => a ? a.slug === slug : false);
100-
if(!post) {
101-
console.log('leo-plugin-blogpost could not find', slug, 'It may be useful to define `slug` in the frontmatter for this post');
95+
const getPost = slug => {
96+
const post = find(
97+
allPosts,
98+
({ attributes: a }) => a ? a.slug === slug : false
99+
);
100+
if (!post) {
101+
console.log(
102+
"leo-plugin-blogpost could not find",
103+
slug,
104+
"It may be useful to define `slug` in the frontmatter for this post"
105+
);
102106
}
103-
return post
104-
}
107+
return post;
108+
};
105109

106110
return {
107111
post: {
108112
type: BlogPostType,
109113
args: {
110114
slug: {
111115
type: GraphQLString,
112-
description: 'The slugified version of a post title'
116+
description: "The slugified version of a post title"
113117
}
114118
},
115119
resolve: (root, { slug }) => getPost(slug)
@@ -119,5 +123,5 @@ module.exports = function(data) {
119123
args: connectionArgs,
120124
resolve: (_, args) => connectionFromArray(allPosts, args)
121125
}
122-
}
123-
}
126+
};
127+
};

0 commit comments

Comments
 (0)