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" ) ;
10
10
11
11
function parseDate ( str ) {
12
12
if ( ! str ) {
13
13
// If there's no date, use right now
14
- debug ( ' using now' )
15
- return moment . utc ( )
14
+ debug ( " using now" ) ;
15
+ return moment . utc ( ) ;
16
16
} else {
17
- return moment . utc ( str , [ ' MMM Do, YYYY' ] )
17
+ return moment . utc ( str , [ " MMM Do, YYYY" ] ) ;
18
18
}
19
19
}
20
20
21
21
function slugify ( str ) {
22
- return mkSlug ( str , { mode : ' rfc3986' } ) ;
22
+ return mkSlug ( str , { mode : " rfc3986" } ) ;
23
23
}
24
24
25
25
module . exports = function ( json ) {
26
- debug ( ' loading' ) ;
26
+ debug ( " loading" ) ;
27
27
// Signal to webpack this is cacheable
28
28
this . cacheable ( ) ;
29
29
/**
30
30
* Get the filename for the currently loading content
31
31
* given `/whatever/post-a.post`, will return `post-a`
32
32
*/
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" , {
37
35
content : json
38
36
} ) ;
39
37
40
38
var headerImg ;
41
39
// TODO: check if header image exists via headerImagePath
42
- if ( filename === ' index' ) {
40
+ if ( filename === " index" ) {
43
41
try {
44
42
fs . accessSync ( headerImagePath , fs . F_OK ) ;
45
43
this . addDependency ( headerImagePath ) ;
@@ -48,19 +46,17 @@ module.exports = function(json) {
48
46
// console.log(this);
49
47
// console.log(headerImg);
50
48
} catch ( e ) {
51
- console . log ( ' doesnt exist' , e ) ;
49
+ console . log ( " doesnt exist" , e ) ;
52
50
}
53
51
}
54
52
// 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" } ;
58
54
category . slug = slugify ( category . display ) ;
59
55
60
56
// Ensure a title exists
61
57
var title = json . attributes . title ;
62
58
// 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" ) {
64
60
filename ;
65
61
}
66
62
@@ -75,19 +71,19 @@ module.exports = function(json) {
75
71
*/
76
72
var url = json . attributes . url ;
77
73
if ( ! url ) {
78
- url = '/' + slug ;
74
+ url = "/" + slug ;
79
75
}
80
76
81
77
// Momentize the publish date
82
78
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" ) ;
85
81
}
86
82
// If there's no updatedAt value, use publishedAt
87
83
let updatedAt = json . attributes . updatedAt ;
88
84
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" ) ;
91
87
}
92
88
93
89
/**
@@ -97,16 +93,14 @@ module.exports = function(json) {
97
93
* an average word per minute reading pace.
98
94
*/
99
95
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 : [ ] } ) ;
104
98
var avgWPM = 265 ;
105
- var wordCount = pureText . split ( ' ' ) . length ;
99
+ var wordCount = pureText . split ( " " ) . length ;
106
100
// timeToRead in minutes
107
101
timeToRead = Math . round ( wordCount / avgWPM ) ;
108
102
// super hacky way to make sure "0 min read" never happens
109
- if ( timeToRead === 0 ) {
103
+ if ( timeToRead === 0 ) {
110
104
timeToRead = 1 ;
111
105
}
112
106
}
@@ -119,19 +113,19 @@ module.exports = function(json) {
119
113
...json ,
120
114
attributes : {
121
115
...json . attributes ,
122
- contentType : ' leo-blogpost' ,
116
+ contentType : " leo-blogpost" ,
123
117
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" ) ,
126
120
slug : slug ,
127
121
url : url ,
128
122
excerpt : excerpt ( json . body ) ,
129
123
timeToRead : timeToRead
130
124
}
131
125
} ;
132
126
133
- if ( headerImg ) {
134
- debug ( ' headerImg' , headerImg ) ;
127
+ if ( headerImg ) {
128
+ debug ( " headerImg" , headerImg ) ;
135
129
/**
136
130
* If headerImg exists, we were able to access the header.png
137
131
* file. This means we should include it as a require, so it
@@ -142,12 +136,14 @@ module.exports = function(json) {
142
136
module.exports = Object.assign(${ JSON . stringify ( finalContent ) } ,
143
137
{
144
138
attributes: Object.assign(
145
- ${ JSON . stringify ( finalContent . attributes ) } ,
139
+ ${ JSON . stringify (
140
+ finalContent . attributes
141
+ ) } ,
146
142
{ headerImage: '/' + require('${ headerImagePath } ') }
147
143
)
148
144
})
149
145
` ;
150
146
} else {
151
- return ' module.exports = ' + JSON . stringify ( finalContent ) ;
147
+ return " module.exports = " + JSON . stringify ( finalContent ) ;
152
148
}
153
- }
149
+ } ;
0 commit comments