@tomosull/gridsome-source-wordpress 1.4.13 → 1.5.0

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 (3) hide show
  1. package/README.md +34 -0
  2. package/index.js +178 -50
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,6 +2,40 @@
2
2
 
3
3
  > WordPress source for Gridsome with support for downloading images.
4
4
 
5
+ Fixes false 404 issue which was showing on fonts and missing image alt issues by changing
6
+
7
+ if ([401, 403].includes(status)) {
8
+ console.warn(`Error: Status ${status} - ${url}`)
9
+ return { ...response, data: fallbackData }
10
+ } else {
11
+ throw new Error(`${status} - ${url}`)
12
+ }
13
+
14
+ to
15
+
16
+ if ([401, 403, 404].includes(status)) {
17
+ console.warn(`Error: Status ${status} - ${url}`)
18
+ return { ...response, data: fallbackData }
19
+ } else {
20
+ throw new Error(`${status} - ${url}`)
21
+ }
22
+
23
+ Also update
24
+
25
+ return {
26
+ src: path.resolve(DOWNLOAD_DIR, filename),
27
+ title: value.title,
28
+ alt: value.description
29
+ }
30
+
31
+ to
32
+
33
+ return {
34
+ src: path.resolve(DOWNLOAD_DIR, filename),
35
+ title: value.title,
36
+ alt: value.alt
37
+ }
38
+
5
39
  ## Install
6
40
  - `yarn add gridsome-source-wordpress`
7
41
  - `npm install gridsome-source-wordpress`
package/index.js CHANGED
@@ -57,9 +57,21 @@ class WordPressSource {
57
57
 
58
58
  const baseUrl = trimEnd(opts.baseUrl, '/')
59
59
 
60
- this.client = axios.create({
61
- baseURL: `${baseUrl}/${opts.apiBase}`
62
- })
60
+ this.client = axios.create({
61
+ baseURL: `${baseUrl}/${opts.apiBase}`,
62
+ responseType: 'json',
63
+ transformResponse: [function (data) {
64
+ if (typeof data === 'string') {
65
+ try {
66
+ return JSON.parse(data)
67
+ } catch (err) {
68
+ return data
69
+ }
70
+ }
71
+
72
+ return data
73
+ }]
74
+ })
63
75
 
64
76
  this.routes = this.options.routes || {}
65
77
 
@@ -82,21 +94,65 @@ class WordPressSource {
82
94
  })
83
95
  }
84
96
 
85
- async getPostTypes (actions) {
86
- const { data } = await this.fetch('wp/v2/types', {}, {})
87
- const addCollection = actions.addCollection || actions.addContentType
97
+ async getPostTypes (actions) {
98
+ const addCollection = actions.addCollection || actions.addContentType
99
+
100
+ const postTypes = {
101
+ post: {
102
+ restBase: 'posts',
103
+ route: this.routes.post || '/blog/:slug'
104
+ },
105
+ page: {
106
+ restBase: 'pages',
107
+ route: this.routes.page || '/:slug'
108
+ },
109
+ attachment: {
110
+ restBase: 'media',
111
+ route: null
112
+ },
113
+ projects: {
114
+ restBase: 'projects',
115
+ route: this.routes.projects || '/projects/:slug'
116
+ },
117
+ products: {
118
+ restBase: 'products',
119
+ route: this.routes.products || '/products/:slug'
120
+ },
121
+ solutions: {
122
+ restBase: 'solutions',
123
+ route: this.routes.solutions || '/:slug'
124
+ },
125
+ cpd: {
126
+ restBase: 'cpd',
127
+ route: this.routes.cpd || '/cpds/:slug'
128
+ },
129
+ team: {
130
+ restBase: 'team',
131
+ route: this.routes.team || '/team/:slug'
132
+ },
133
+ locations: {
134
+ restBase: 'locations',
135
+ route: this.routes.locations || '/locations/:slug'
136
+ },
137
+ samples: {
138
+ restBase: 'samples',
139
+ route: this.routes.samples || '/samples/:slug'
140
+ }
141
+ }
88
142
 
89
- for (const type in data) {
90
- const options = data[type]
143
+ for (const type in postTypes) {
144
+ const { restBase, route } = postTypes[type]
91
145
 
92
- this.restBases.posts[type] = options.rest_base
146
+ this.restBases.posts[type] = restBase
93
147
 
94
- addCollection({
95
- typeName: this.createTypeName(type),
96
- route: this.routes[type] || `/${type}/:slug`
97
- })
98
- }
148
+ addCollection({
149
+ typeName: this.createTypeName(type),
150
+ route
151
+ })
152
+
153
+ console.log(`Added post type: ${type} -> wp/v2/${restBase}`)
99
154
  }
155
+ }
100
156
 
101
157
  async getUsers (actions) {
102
158
  const { data } = await this.fetch('wp/v2/users')
@@ -107,45 +163,66 @@ class WordPressSource {
107
163
  route: this.routes.author
108
164
  })
109
165
 
110
- for (const author of data) {
111
- const fields = this.normalizeFields(author)
112
- const avatars = mapKeys(author.avatar_urls, (v, key) => `avatar${key}`)
166
+ // for (const author of data) {
167
+ // const fields = this.normalizeFields(author)
168
+ // const avatars = mapKeys(author.avatar_urls, (v, key) => `avatar${key}`)
169
+
170
+ // authors.addNode({
171
+ // ...fields,
172
+ // id: author.id,
173
+ // title: author.name,
174
+ // avatars
175
+ // })
176
+ // }
177
+ }
113
178
 
114
- authors.addNode({
115
- ...fields,
116
- id: author.id,
117
- title: author.name,
118
- avatars
119
- })
179
+ async getTaxonomies (actions) {
180
+ const addCollection = actions.addCollection || actions.addContentType
181
+
182
+ const taxonomies = {
183
+ category: {
184
+ restBase: 'categories',
185
+ route: this.routes.category || '/category/:slug'
186
+ },
187
+ post_tag: {
188
+ restBase: 'tags',
189
+ route: this.routes.post_tag || '/tag/:slug'
190
+ },
191
+ product_category: {
192
+ restBase: 'product_category',
193
+ route: this.routes.product_category || '/products/:slug'
194
+ },
195
+ finish: {
196
+ restBase: 'finish',
197
+ route: this.routes.finishes || '/finishes/:slug'
120
198
  }
121
199
  }
122
200
 
123
- async getTaxonomies (actions) {
124
- const { data } = await this.fetch('wp/v2/taxonomies', {}, {})
125
- const addCollection = actions.addCollection || actions.addContentType
201
+ for (const type in taxonomies) {
202
+ const { restBase, route } = taxonomies[type]
126
203
 
127
- for (const type in data) {
128
- const options = data[type]
129
- const taxonomy = addCollection({
130
- typeName: this.createTypeName(type),
131
- route: this.routes[type]
132
- })
204
+ const taxonomy = addCollection({
205
+ typeName: this.createTypeName(type),
206
+ route
207
+ })
133
208
 
134
- this.restBases.taxonomies[type] = options.rest_base
209
+ this.restBases.taxonomies[type] = restBase
135
210
 
136
- const terms = await this.fetchPaged(`wp/v2/${options.rest_base}`)
211
+ console.log(`Added taxonomy: ${type} -> wp/v2/${restBase}`)
137
212
 
138
- for (const term of terms) {
139
- taxonomy.addNode({
140
- id: term.id,
141
- title: term.name,
142
- slug: term.slug,
143
- content: term.description,
144
- count: term.count
145
- })
146
- }
213
+ const terms = await this.fetchPaged(`wp/v2/${restBase}`)
214
+
215
+ for (const term of terms) {
216
+ taxonomy.addNode({
217
+ id: term.id,
218
+ title: term.name,
219
+ slug: term.slug,
220
+ content: term.description,
221
+ count: term.count
222
+ })
147
223
  }
148
224
  }
225
+ }
149
226
 
150
227
  extractImagesFromPostHtml (string) {
151
228
  const regex = /<img[^>]* src=\"([^\"]*)\" alt=\"([^\"]*)\"[^>]*>/gm
@@ -244,6 +321,12 @@ class WordPressSource {
244
321
 
245
322
  for (const type in this.restBases.posts) {
246
323
  const restBase = this.restBases.posts[type]
324
+
325
+ if (!restBase) {
326
+ console.warn(`Skipping post type with invalid rest_base: ${type}`)
327
+ continue
328
+ }
329
+
247
330
  const typeName = this.createTypeName(type)
248
331
  const posts = getCollection(typeName)
249
332
 
@@ -306,8 +389,10 @@ class WordPressSource {
306
389
  throw new Error(`${code} - ${config.url}`)
307
390
  }
308
391
 
392
+
309
393
  const { url } = response.config
310
- const { status } = response.data.data
394
+ const { status } = response.data
395
+
311
396
 
312
397
  if ([401, 403, 404].includes(status)) {
313
398
  console.warn(`Error: Status ${status} - ${url}`)
@@ -433,18 +518,61 @@ class WordPressSource {
433
518
  }
434
519
 
435
520
  function ensureArrayData (url, data) {
436
- if (!Array.isArray(data)) {
521
+ if (Array.isArray(data)) {
522
+ return data
523
+ }
524
+
525
+ if (typeof data === 'string') {
526
+ const trimmed = data.trim()
527
+
437
528
  try {
438
- data = JSON.parse(data)
529
+ const parsed = JSON.parse(trimmed)
530
+
531
+ if (Array.isArray(parsed)) {
532
+ return parsed
533
+ }
534
+
535
+ throw new Error(
536
+ `Failed to fetch ${url}\n` +
537
+ `Expected JSON array but parsed ${typeof parsed}:\n` +
538
+ `${trimmed.substring(0, 300)}...\n`
539
+ )
439
540
  } catch (err) {
541
+ console.warn(`JSON.parse failed for ${url}: ${err.message}`)
542
+
543
+ // WordPress/plugin/theme may be appending warnings, notices, or markup.
544
+ // Try to extract just the JSON array.
545
+ const start = trimmed.indexOf('[')
546
+ const end = trimmed.lastIndexOf(']')
547
+
548
+ if (start !== -1 && end !== -1 && end > start) {
549
+ const possibleJson = trimmed.substring(start, end + 1)
550
+
551
+ try {
552
+ const parsed = JSON.parse(possibleJson)
553
+
554
+ if (Array.isArray(parsed)) {
555
+ console.warn(`Recovered JSON array from noisy response: ${url}`)
556
+ return parsed
557
+ }
558
+ } catch (innerErr) {
559
+ console.warn(`Recovery parse failed for ${url}: ${innerErr.message}`)
560
+ }
561
+ }
562
+
440
563
  throw new Error(
441
564
  `Failed to fetch ${url}\n` +
442
- `Expected JSON response but received:\n` +
443
- `${data.trim().substring(0, 150)}...\n`
565
+ `Expected JSON array but received:\n` +
566
+ `${trimmed.substring(0, 1000)}...\n`
444
567
  )
445
568
  }
446
569
  }
447
- return data
570
+
571
+ throw new Error(
572
+ `Failed to fetch ${url}\n` +
573
+ `Expected JSON array but received ${typeof data}:\n` +
574
+ `${JSON.stringify(data).substring(0, 1000)}...\n`
575
+ )
448
576
  }
449
577
 
450
- module.exports = WordPressSource
578
+ module.exports = WordPressSource
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.13",
2
+ "version": "1.5.0",
3
3
  "name": "@tomosull/gridsome-source-wordpress",
4
4
  "description": "Tomosull Fork of gridsome-source-wordpress 4.13 - WordPress source for Gridsome with image processing support. This fixes minor issues with the original plugin; namely 403 and image alt tags not loading.",
5
5
  "homepage": "https://github.com/gridsome/gridsome/tree/master/packages/source-wordpress#readme",