Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
request all (if more than 1000) blog posts from Contentful
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Seckinger committed Nov 13, 2019
1 parent 52bb908 commit 4005ef8
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 45 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": ["babel-preset-gatsby"],
"env": {
"test": {
"plugins": ["require-context-hook"]
}
}
}
8 changes: 0 additions & 8 deletions .babelrc.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
const FilterPostsToProcess = require('../../../../src/functions/blogMethods/filter-posts-to-process');
const FilterPostsToProcess = require('../filter-posts-to-process');

const originalConsoleInfo = console.info;
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const GetAllContentfulBlogPosts = require('../get-all-contentful-blog-posts');

const posts = [...Array(2 ** 16).keys()].map(i => ({ slug: `post-${i}` }));

const mockGetEntries = jest.fn(({ skip, limit }) => ({
skip,
limit,
total: posts.length,
items: posts.slice(skip, skip + limit),
}));
const environment = { getEntries: mockGetEntries };

beforeEach(() => {
mockGetEntries.mockClear();
});

it('requests blog posts', async () => {
await GetAllContentfulBlogPosts(environment);
mockGetEntries.mock.calls.map(([{ content_type }]) => {
expect(content_type).toBe('blogPost');
});
});

it('requests until it has all entries and returns them', async () => {
expect(await GetAllContentfulBlogPosts(environment)).toEqual(posts);
});

it('does not exceed the contentful entry limit', async () => {
await GetAllContentfulBlogPosts(environment);
mockGetEntries.mock.calls.map(([{ limit }]) => {
expect(limit).toBeLessThanOrEqual(1000);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ParseXMLToJSON = require('../../../../src/functions/blogMethods/parse-xml-to-json');
const ParseXMLToJSON = require('../parse-xml-to-json');
const { readFileSync } = require('fs');
const { resolve } = require('path');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
const PublishToContentful = require('../../../../src/functions/blogMethods/publish-to-contentful');
const PublishToContentful = require('../publish-to-contentful');
const until = require('async-wait-until');

const originalConsoleInfo = console.info;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TransformHTMLToMarkdown = require('../../../../src/functions/blogMethods/transform-html-to-markdown');
const TransformHTMLToMarkdown = require('../transform-html-to-markdown');

const tweetBlockquote = `
<blockquote class="twitter-tweet">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TransformMetaData = require('../../../../src/functions/blogMethods/transform-meta-data');
const TransformMetaData = require('../transform-meta-data');

const headerImage = {
sys: {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/blogMethods/__tests__/validate-mdx.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ValidateMdx = require('../../../../src/functions/blogMethods/validate-mdx');
const ValidateMdx = require('../validate-mdx');

it.each`
description | mdx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const AddFrontMatter = require('../../../../../src/functions/blogMethods/custom-mdx/add-frontmatter');
const AddFrontMatter = require('../add-frontmatter');

const post = {
title: 'Blog Title',
Expand Down
20 changes: 6 additions & 14 deletions src/functions/blogMethods/custom-mdx/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
const TransformCustomMdx = require('../../../../../src/functions/blogMethods/custom-mdx');
const TransformImageData = require('../../../../../src/functions/blogMethods/custom-mdx/transform-image-data');
const TransformCustomMdx = require('..');
const TransformImageData = require('../transform-image-data');

jest.mock(
'../../../../../src/functions/blogMethods/custom-mdx/add-frontmatter',
);
jest.mock(
'../../../../../src/functions/blogMethods/custom-mdx/transform-iframes',
);
jest.mock(
'../../../../../src/functions/blogMethods/custom-mdx/transform-image-data',
);
jest.mock(
'../../../../../src/functions/blogMethods/custom-mdx/transform-strings',
);
jest.mock('../add-frontmatter');
jest.mock('../transform-iframes');
jest.mock('../transform-image-data');
jest.mock('../transform-strings');

const post = {
slug: 'Post',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TransformIframes = require('../../../../../src/functions/blogMethods/custom-mdx/transform-iframes');
const TransformIframes = require('../transform-iframes');
const Got = require('got');

jest.mock('got');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jest.mock('../../../../../src/functions/utils/is-prod', () => true);
jest.mock('../../../utils/is-prod', () => true);

const mockUploadToContentful = jest.fn(({ name }) => {
return {
Expand All @@ -7,16 +7,13 @@ const mockUploadToContentful = jest.fn(({ name }) => {
};
});

jest.mock(
'../../../../../src/functions/blogMethods/custom-mdx/upload-image-to-contentful',
() => mockUploadToContentful,
);
jest.mock('../upload-image-to-contentful', () => mockUploadToContentful);

beforeEach(() => {
mockUploadToContentful.mockClear();
});

const TransformImageData = require('../../../../../src/functions/blogMethods/custom-mdx/transform-image-data');
const TransformImageData = require('../transform-image-data');

const img1 = {
caption: 'Image 1 caption',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TransformStrings = require('../../../../../src/functions/blogMethods/custom-mdx/transform-strings');
const TransformStrings = require('../transform-strings');

it.each([
'https://medium.com/yld-blog/',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const UploadImageToContentful = require('../../../../src/functions/blogMethods/custom-mdx/upload-image-to-contentful');
const UploadImageToContentful = require('../upload-image-to-contentful');

const mockPublish = jest.fn(function publish() {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/functions/blogMethods/custom-mdx/transform-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const postPathSegment = new RegExp(`(${slug.source}-${hexUid.source})?`);
const path = new RegExp(`${blogPath.source}(\\/${postPathSegment.source})?`);

const mdLink = new RegExp(`\\(${origin.source}${path.source}\\)`, 'gi');
const mdLinkFull = /\(https?:\/\/medium\.com\/yld(-engineering)?-blog(\/((?<slug>[^)]+)-[0-9a-f]{8,})?)?\)/;
const mdLinkFull = /\(https?:\/\/medium\.com\/yld(-engineering)?-blog(\/((?<slug>[^)]+)-[0-9a-f]{8,})?)?\)/gi;
// composite mdLink and mdLinkFull are provided because one may find either more legible - this ensures they are kept in sync
assert.strictEqual(mdLink.source, mdLinkFull.source);
assert.deepStrictEqual(mdLink, mdLinkFull);

const transformStrings = content =>
content.replace(mdLink, function() {
Expand Down
19 changes: 19 additions & 0 deletions src/functions/blogMethods/get-all-contentful-blog-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const getAllBlogPosts = async environment => {
let total = Number.POSITIVE_INFINITY;
let items = [];

while (items.length < total) {
const { items: cmsPosts, total: cmsTotal } = await environment.getEntries({
content_type: 'blogPost',
limit: 1000,
skip: items.length,
});

items.push(...cmsPosts);
total = cmsTotal;
}

return items;
};

module.exports = getAllBlogPosts;
7 changes: 2 additions & 5 deletions src/functions/blogMethods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const TransformHtmlToMd = require('./transform-html-to-markdown');
const TransformCustomMDX = require('./custom-mdx');
const PublishToContentful = require('./publish-to-contentful');
const FilterPostsToProcess = require('./filter-posts-to-process');
const GetAllContentfulBlogPosts = require('./get-all-contentful-blog-posts');
const ValidateMdx = require('./validate-mdx');
const TransformMetaData = require('./transform-meta-data');

Expand All @@ -25,11 +26,7 @@ const syncMediumToContentful = async data => {

const { allFields, requiredFields } = GetContentTypeFields(contentType);

const { items: cmsBlogPosts } = await environment.getEntries({
limit: 1000,
content_type: 'blogPost',
});

const cmsBlogPosts = await GetAllContentfulBlogPosts(environment);
const parsedPostsInJson = await ParseXMLToJSON(data);

const postsToProcess = FilterPostsToProcess(parsedPostsInJson, {
Expand Down

0 comments on commit 4005ef8

Please sign in to comment.