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

Fix/4459/excerpt formatting ignored #9716

Merged
merged 28 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
505a12a
extract markdownNodeAST function
nadrane Nov 5, 2018
099fcd0
use async/await better
nadrane Nov 5, 2018
dea318a
delete unncessary promise to make code more readable
nadrane Nov 5, 2018
87a421d
extract remark target parameter
nadrane Nov 5, 2018
af59b30
update tests and return html excerpt
nadrane Nov 5, 2018
385f2ab
update snapshots
nadrane Nov 5, 2018
1613bbe
begin refactoring code to grab exerpts in the case where no separator…
nadrane Nov 6, 2018
b431269
make headway on ast cloning
nadrane Nov 6, 2018
7b40c31
abandon generalizing preorder traversal. tree duplication works
nadrane Nov 6, 2018
4a31459
pruning mostly works
nadrane Nov 6, 2018
440db7a
get the complicated test expectation correct
nadrane Nov 9, 2018
c7b9cdd
rename complicated test
nadrane Nov 9, 2018
cdc2324
refactor out tree cloning
nadrane Nov 9, 2018
15932e7
make progress towards pruning last text node
nadrane Nov 9, 2018
235daa2
tests passing
nadrane Nov 10, 2018
830e6fc
don't invoke plugin functions a second time if excerpt_separator used
nadrane Nov 10, 2018
76037bf
code works
nadrane Nov 10, 2018
97cfabb
migrate to excerpt and exerptHTML fields
nadrane Nov 13, 2018
014ab92
merge master
nadrane Nov 25, 2018
9fc3aa2
print graphql errors properly
nadrane Nov 25, 2018
df3dcf2
excerpt printing in html
nadrane Nov 25, 2018
2249f7c
more progress
nadrane Nov 25, 2018
4e4c498
final changes to excerpt separator handling
nadrane Nov 27, 2018
64152c7
use enum type for format arg
nadrane Nov 27, 2018
4db7c57
undo accidental jest config changes
nadrane Nov 27, 2018
e755e9b
update README
nadrane Nov 27, 2018
a8c1f05
Merge branch 'master' into fix/4459/excerpt-formatting-ignored
nadrane Nov 27, 2018
dc120d9
Update README.md
DSchau Nov 27, 2018
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
34 changes: 33 additions & 1 deletion packages/gatsby-transformer-remark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ Using the following GraphQL query you'll be able to get the table of contents

By default the tableOfContents is using the field `slug` to generate URLs. You can however provide another field using the pathToSlugField parameter. **Note** that providing a non existing field will cause the result to be null.

### Excerpt length
### Excerpts

#### Length

By default, excerpts have a maximum length of 140 characters. You can change the default using the `pruneLength` argument. For example, if you need 500 characters, you can specify:

Expand All @@ -98,6 +100,36 @@ By default, excerpts have a maximum length of 140 characters. You can change the
}
```

#### Format

By default, Gatsby will return excerpts as plain text. This might be useful for populating [opengraph](https://en.wikipedia.org/wiki/Facebook_Platform#Open_Graph_protocol) HTML tags for SEO reasons. You can also explicitly specify a `PLAIN` format like so:

```graphql
{
allMarkdownRemark {
edges {
node {
excerpt(format: PLAIN)
}
}
}
}
```

It's also possible to ask Gatsby to return excerpts formatted as HTML. You might use this if you have a blog post whose an excerpt contains markdown content--e.g. header, link, etc.--and you want these links to render as HTML.

```graphql
{
allMarkdownRemark {
edges {
node {
excerpt(format: HTML)
}
}
}
}
```

## Troubleshooting

### Excerpts for non-latin languages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ Object {
}
`;

exports[`Excerpt is generated correctly from schema given an html format, it correctly maps nested markdown to html 1`] = `
Object {
"excerpt": "<p>Where oh <a><em>where</em></a> <strong><em>is</em></strong> that pony?</p>",
"frontmatter": Object {
"title": "my little pony",
},
}
`;

exports[`Excerpt is generated correctly from schema given an html format, it respects the excerpt_separator 1`] = `
Object {
"excerpt": "<p>Where oh where is that pony? Is he in the stable…</p>",
"frontmatter": Object {
"title": "my little pony",
},
}
`;

exports[`Links are correctly prefixed correctly prefixes links 1`] = `
Object {
"html": "<p>This is <a href=\\"/prefix/path/to/page1\\">a link</a>.</p>
Expand Down
68 changes: 68 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,74 @@ In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincid
expect(node.excerpt.length).toBe(50)
}
)

bootstrapTest(
`given an html format, it correctly maps nested markdown to html`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---

Where oh [*where*](nick.com) **_is_** that pony?`,
`excerpt(format: HTML)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toMatch(
`<p>Where oh <a><em>where</em></a> <strong><em>is</em></strong> that pony?</p>`
)
}
)

bootstrapTest(
`given an html format, it prunes large excerpts`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---

Where oh where is that pony? Is he in the stable or down by the stream?`,
`excerpt(format: HTML, pruneLength: 50)
frontmatter {
title
}
`,
node => {
// expect(node).toMatchSnapshot()
expect(node.excerpt).toMatch(
`<p>Where oh where is that pony? Is he in the stable…</p>`
)
}
)

bootstrapTest(
`given an html format, it respects the excerpt_separator`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---

Where oh where is that pony? Is he in the stable or by the stream?

<!-- end -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
`,
`excerpt(format: HTML, pruneLength: 50)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toMatch(
`<p>Where oh where is that pony? Is he in the stable…</p>`
)
},
{ excerpt_separator: `<!-- end -->` }
)
})

describe(`Wordcount and timeToRead are generated correctly from schema`, () => {
Expand Down
Loading