Skip to content

Commit

Permalink
feat(MediumPosts): Component to automatically generate latest Medium …
Browse files Browse the repository at this point in the history
…posts on build time. (#482)

* v0.0.0

* v1.19.1

* new MediumPost component and all the supporting docs

* removed version from package.json

* added an anchorlink for Medium

* v1.15.3

* v1.15.3

* v1.15.3

* v1.15.3

* v1.15.3

* images now use object-fit to maintain aspect ratio

* fixed the height on xlg screens

* fixed aspect ratio

* fixed again
  • Loading branch information
sstrubberg authored and jeanservaas committed Nov 12, 2019
1 parent 485aa7f commit 19b5e70
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/example/src/data/nav-items.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
path: /components/InlineNotification
- title: Markdown
path: /components/markdown
- title: MediumPosts
path: /components/MediumPosts
- title: MDX
path: /components/MDX
- title: PageDescription
Expand Down
27 changes: 27 additions & 0 deletions packages/example/src/pages/components/MediumPosts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: MediumPosts
description: Guide for pulling Medium posts through their RSS feed using a Gatsby plugin.
---

<PageDescription>

Using a Gatsby plugin, this component automatically builds three `ArticleCards` populated with the most recent Medium posts at build time.

</PageDescription>

## Example

<MediumPosts color="dark" posts={3} />

## Code

```jsx path=components/MediumPosts/MediumPosts.js src=https://github.com/carbon-design-system/gatsby-theme-carbon/tree/master/packages/gatsby-theme-carbon/src/components/MediumPosts
<MediumPosts color="dark" posts={3} />
```

## Props

| property | propType | required | default | description |
| -------- | -------- | -------- | ------- | --------------------------------------------------------------------------------------------- |
| color | string | | `light` | Sets the card for the correct color theme, default is `light`, options are `light` and `dark` |
| posts | number | | `3` | Sets the number of posts pulled from Medium, default is `3`, maximum is `10`. |
17 changes: 17 additions & 0 deletions packages/example/src/pages/guides/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gatsby themes allow you to override configuration from the theme by defining the
<AnchorLink>Image compression</AnchorLink>
<AnchorLink>Global search</AnchorLink>
<AnchorLink>Edit on Github link</AnchorLink>
<AnchorLink>Medium</AnchorLink>
<AnchorLink>Other options</AnchorLink>
</AnchorLinks>

Expand Down Expand Up @@ -154,6 +155,22 @@ To add a link to the bottom of each page that points to the current page source
],
```

## Medium

Automatically grab posts from Medium's RSS feed and populate `ArticleCards`. Enable `gatsby-source-medium-feed` by shadowing this plugin and providing a `userName`. For guidance on how to use this plugin checkout [MediumPosts](/components/MediumPosts).
```js
plugins: [
{
resolve: 'gatsby-source-medium-feed',
options: {
userName: '@...', // Medium user name or publication
name: 'MediumFeed', // GraphQL query AllMediumFeed
},
},
];
```
## Other options
- `additionalFontWeights` – add support for additional Plex font weights. Don't forget to specify italics for the additional weights if needed.
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby-theme-carbon/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = themeOptions => {
imageQuality = 75,
lunrOptions = defaultLunrOptions,
repository,
mediumAccount = 'carbondesign',
} = themeOptions;

return {
Expand Down Expand Up @@ -114,6 +115,13 @@ module.exports = themeOptions => {
},
},
`gatsby-plugin-react-helmet`,
{
resolve: 'gatsby-source-medium-feed',
options: {
userName: mediumAccount, // Medium user name
name: 'MediumFeed', // GraphQL query AllMediumFeed
},
},
],
};
};
4 changes: 3 additions & 1 deletion packages/gatsby-theme-carbon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"gatsby-remark-smartypants": "^2.0.9",
"gatsby-remark-unwrap-images": "^1.0.1",
"gatsby-source-filesystem": "^2.0.37",
"gatsby-source-medium-feed": "^1.0.6",
"gatsby-transformer-yaml": "^2.1.12",
"lodash": "^4.17.11",
"mkdirp": "^0.5.1",
Expand All @@ -60,5 +61,6 @@
"slugify": "^1.3.4",
"smooth-scroll": "^16.0.3",
"use-media": "^1.4.0"
}
},
"gitHead": "a50eea99e0b0cce27832395d74c5942cee9221d0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Tab, Tabs } from '../Tabs';
import Link from '../Link';
import { Accordion, AccordionItem } from '../Accordion';
import ArtDirection from '../ArtDirection';
import MediumPosts from '../MediumPosts';

const components = {
wrapper: function Wrapper({ children, ...props }) {
Expand Down Expand Up @@ -61,6 +62,7 @@ const components = {
Tab,
Tabs,
InlineNotification,
MediumPosts,
};

export default components;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';
import { useStaticQuery, graphql } from 'gatsby';
import PropTypes from 'prop-types';
import { Column, Row } from '../Grid';
import ArticleCard from '../ArticleCard';

const MediumPosts = ({ color, posts }) => {
const data = useStaticQuery(graphql`
query {
allMediumFeed(sort: { fields: date, order: DESC }, limit: 10) {
edges {
node {
author
date(formatString: "MMMM Do, YYYY")
slug
thumbnail
title
link
}
}
}
}
`);

const allPosts = data.allMediumFeed.edges.map(({ node }) => node);

const defaultProps = {
defaultPosts: 3,
};

const latestPosts = allPosts.slice(0, posts || defaultProps.defaultPosts);

return (
<>
<Row>
{latestPosts.map(latestPost => (
<Column
colSm={4}
colMd={4}
colLg={4}
noGutterMdLeft
className="medium-posts-article-gutter medium-posts-type-size"
>
<ArticleCard
title={latestPost.title}
author={latestPost.author}
href={latestPost.link}
color={color}
date={latestPost.date}
>
<img
alt={latestPost.title}
src={latestPost.thumbnail}
className="medium-image-container"
/>
</ArticleCard>
</Column>
))}
</Row>
</>
);
};

MediumPosts.propTypes = {
color: PropTypes.string,
posts: PropTypes.number,
};

export default MediumPosts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MediumPosts from './MediumPosts';

export default MediumPosts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Fixes the gutter of the article cards on the homepage
.medium-posts-article-gutter {
padding-right: 0;
padding-left: 0;
@include carbon--breakpoint('md') {
padding-right: 1rem;
padding-left: 1rem;
}
}

// Fixs the breaking type

.medium-posts-type-size h4 {
@include carbon--breakpoint('sm') {
@include carbon--type-style('productive-heading-03');
}

@include carbon--breakpoint('md') {
font-size: 1rem;
font-weight: 400;
line-height: 1.2rem;
letter-spacing: 0;
}

@include carbon--breakpoint('lg') {
font-size: 0.75rem;
font-weight: 400;
line-height: 1rem;
letter-spacing: 0;
}
@include carbon--breakpoint('xlg') {
font-size: 1rem;
font-weight: 400;
line-height: 1.375rem;
letter-spacing: 0;
}
}

// Images pulled from the Medium RSS feed are different sizes and aspect ratios. This will make the cards different sizes. The class below fixes that issue.

.medium-image-container {
object-fit: cover;
width: 100%;

@include carbon--breakpoint('sm') {
height: 315px;
}
@include carbon--breakpoint('md') {
height: 290px;
}
@include carbon--breakpoint('lg') {
height: 165px;
}
@include carbon--breakpoint('xlg') {
height: 215px;
}
}
2 changes: 1 addition & 1 deletion packages/gatsby-theme-carbon/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@import '../components/FeatureCard/feature-card.scss';
@import '../components/ImageCard/image-card.scss';
@import '../components/ImageCard/aspect-ratio.scss';

@import '../components/MediumPosts/medium-posts.scss';
//---------------------------------------
// Included last to ensure that class
// names take precedence over includes
Expand Down
18 changes: 17 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7287,6 +7287,14 @@ gatsby-source-filesystem@^2.0.37:
valid-url "^1.0.9"
xstate "^4.6.7"

gatsby-source-medium-feed@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/gatsby-source-medium-feed/-/gatsby-source-medium-feed-1.0.6.tgz#3ab486b621ea0fc299ea82087e8a1a78b1ffd82c"
integrity sha512-Ey9BWpbEaOkIPk4JNnoVBTZ5uM5ylqhngsQZ+Ijr1l/Oobgpz//FBr9aFpdaayrDHkaeoffsZ7VLgrCl2k+sIw==
dependencies:
"@babel/runtime" "^7.0.0"
rss-parser "3.7.0"

gatsby-telemetry@^1.1.33:
version "1.1.33"
resolved "https://registry.yarnpkg.com/gatsby-telemetry/-/gatsby-telemetry-1.1.33.tgz#2c0956c95e7d341e2740d8c4f3a3ffd6bc636c4e"
Expand Down Expand Up @@ -13655,6 +13663,14 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^3.0.0"
inherits "^2.0.1"

rss-parser@3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/rss-parser/-/rss-parser-3.7.0.tgz#945d82864678a805558c1de68b52f00adeaacd92"
integrity sha512-xN1fwjVxBO0unbrUAOIUK5MAyEaaZTpKWnPY+d3QYigIG4awtbdqxHPOLuOwsTIJFsaKC78nPxIGRJG92p86Hw==
dependencies:
entities "^1.1.1"
xml2js "^0.4.19"

run-async@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"
Expand Down Expand Up @@ -16424,7 +16440,7 @@ xml-parse-from-string@^1.0.0:
resolved "https://registry.yarnpkg.com/xml-parse-from-string/-/xml-parse-from-string-1.0.1.tgz#a9029e929d3dbcded169f3c6e28238d95a5d5a28"
integrity sha1-qQKekp09vN7RafPG4oI42VpdWig=

xml2js@^0.4.5:
xml2js@^0.4.19, xml2js@^0.4.5:
version "0.4.22"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.22.tgz#4fa2d846ec803237de86f30aa9b5f70b6600de02"
integrity sha512-MWTbxAQqclRSTnehWWe5nMKzI3VmJ8ltiJEco8akcC6j3miOhjjfzKum5sId+CWhfxdOs/1xauYr8/ZDBtQiRw==
Expand Down

1 comment on commit 19b5e70

@vercel
Copy link

@vercel vercel bot commented on 19b5e70 Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.