Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use graymatter excerpt in gatsby-transformer-remark #2883

Merged
merged 20 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions examples/using-faker/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ import Link from "gatsby-link"
import "./index.css"

const IndexPage = ({ data }) => (
<div>
<div className="page-grid">
<div className="address-section">
<h4>Address</h4>
<div>{data.personalData.address.streetAddress}</div>
<div>{data.personalData.address.streetName}</div>
<div>{data.personalData.address.city}</div>
<div>
{data.personalData.address.state} -
{data.personalData.address.zipCode}
</div>
</div>
<div className="email-section">
{data.personalData.internet.email} /
{data.personalData.phone.phoneNumber}
</div>
<div className="summary-section">
<h4> Summary</h4>
{data.personalData.lorem.paragraph}
<div>
<div className="page-grid">
<div className="address-section">
<h4>Address</h4>
<div>{data.personalData.address.streetAddress}</div>
<div>{data.personalData.address.streetName}</div>
<div>{data.personalData.address.city}</div>
<div>
{data.personalData.address.state} -
{data.personalData.address.zipCode}
</div>
</div>
<div className="company-section">
<h3>Worked at</h3>
{data.allCompanyData.edges.map(({ node }) => (
<div>
<div>{node.company.companyName}</div>
<div>{node.company.companySuffix}</div>
</div>
))}
<div className="email-section">
{data.personalData.internet.email} /
{data.personalData.phone.phoneNumber}
</div>
<div className="summary-section">
<h4> Summary</h4>
{data.personalData.lorem.paragraph}
</div>
</div>
)
<div className="company-section">
<h3>Worked at</h3>
{data.allCompanyData.edges.map(({ node }) => (
<div>
<div>{node.company.companyName}</div>
<div>{node.company.companySuffix}</div>
</div>
))}
</div>
</div>
)

export default IndexPage

Expand Down
1 change: 1 addition & 0 deletions examples/using-remark/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
{
resolve: `gatsby-transformer-remark`,
options: {
excerpt_separator: `<!-- end -->`,
plugins: [
{
resolve: `gatsby-remark-images`,
Expand Down
38 changes: 38 additions & 0 deletions examples/using-remark/src/pages/2017-11-14---excerpts/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: "Using Excerpts"
date: "2017-11-14"
draft: false
author: Jay Gatsby
tags:
- remark
- excerpts
---

`gatsby-transformer-remark` allows you to get an excerpt from a markdown post. By default, it will prune the first 140 characters, but you can optionally specify a `pruneLength` in the graphql query.

```graphql
{
allMarkdownRemark {
edges {
node {
excerpt(pruneLength: 280)
}
}
}
}
```

You can also manually mark in your markdown where to stop excerpting—similar to Jekyl. `gatsby-transformer-remark` uses [gray-matter]() to parse markdown frontmatter, so you can specify an excerpt_separator, as well as any of the other options mentioned [here](), in the `gatsby-config.js` file.

```json
{
resolve: `gatsby-transformer-remark`,
options: {
excerpt_separator: `<!-- end -->`
}
}
```

Any file that does not have the given excerpt_separator will fall back to the default pruning method.

You can see the results [here](/excerpt-example)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: "Getting an Excerpt with a Separator"
draft: false
example: true
author: Daisy Buchanan
---

This example uses a custom excerpt_separator.

You can manually mark in your markdown where to stop excerpting—similar to Jekyl. `gatsby-transformer-remark` uses [gray-matter]() to parse markdown frontmatter, so you can specify an excerpt_separator, as well as any of the other options mentioned [here](), in the `gatsby-config.js` file.

<!-- end -->

```json
{
resolve: `gatsby-transformer-remark`,
options: {
excerpt_separator: `<!-- end -->`
}
}
```

Any file that does not have the given excerpt_separator will fall back to the default pruning method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "A Default, 140 Character Excerpt"
draft: false
example: true
author: Daisy Buchanan
---

This example uses the default pruning method.

`gatsby-transformer-remark` allows you to get an excerpt from a markdown post. By default, it will prune the first 140 characters, but you can optionally specify a `pruneLength` in the graphql query.

```graphql
{
allMarkdownRemark {
edges {
node {
excerpt(pruneLength: 280)
}
}
}
}
```
77 changes: 77 additions & 0 deletions examples/using-remark/src/pages/excerpt-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from "react"
import Link from "gatsby-link"
import styles from "../styles"
import presets from "../utils/presets"
import { rhythm, scale } from "../utils/typography"

class Index extends React.Component {
render() {
const posts = this.props.data.allMarkdownRemark.edges

return (
<div>
<div>
<h1
css={{
...scale(4 / 5),
fontWeight: `800`,
marginBottom: rhythm(2),
}}
>
This page demonstrates the different types of excerpts you can use
with gatsby-transformer-remark
</h1>
<ul
css={{
marginBottom: rhythm(2),
marginTop: rhythm(2),
marginLeft: 0,
listStyle: `none`,
}}
>
{posts.map(post => (
<li key={post.node.fields.slug}>
<span
css={{
color: styles.colors.light,
display: `block`,
[presets.Tablet]: {
float: `right`,
marginLeft: `1rem`,
},
}}
>
{post.node.frontmatter.date}
</span>
<Link to={post.node.fields.slug} className="link-underline">
{post.node.frontmatter.title}
</Link>
<p>{post.node.excerpt}</p>
</li>
))}
</ul>
</div>
</div>
)
}
}

export default Index

export const pageQuery = graphql`
query ExcerptExampleQuery {
allMarkdownRemark(filter: { frontmatter: { example: { eq: true } } }) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
excerpt
}
}
}
}
`
2 changes: 1 addition & 1 deletion examples/using-remark/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const pageQuery = graphql`
allMarkdownRemark(
limit: 2000
sort: { fields: [frontmatter___date], order: DESC }
filter: { frontmatter: { draft: { ne: true } } }
filter: { frontmatter: { draft: { ne: true }, example: { ne: true } } }
) {
edges {
node {
Expand Down
2 changes: 1 addition & 1 deletion examples/using-remark/src/pages/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const pageQuery = graphql`
}
allMarkdownRemark(
limit: 2000
filter: { frontmatter: { draft: { ne: true } } }
filter: { frontmatter: { draft: { ne: true }, example: { ne: true } } }
) {
group(field: frontmatter___tags) {
fieldValue
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"babel-runtime": "^6.26.0",
"bluebird": "^3.5.0",
"gray-matter": "^2.1.0",
"gray-matter": "^3.0.0",
"hast-util-to-html": "^3.0.0",
"lodash": "^4.17.4",
"mdast-util-to-hast": "^2.4.0",
Expand Down
Loading