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

feat(gatsby-transformer-remark): add excerptAst to be exported as a GraphQL field #11237

Merged
merged 7 commits into from
Feb 25, 2019

Conversation

chitoku-k
Copy link
Contributor

Description

This PR adds/refactors excerpt + excerptAst (which is a new field) because I personally found it incovenient gatsby-transformer-remark not exporting an excerpt field in AST. What I did was basically refactoring by splitting out the resolver of excerpt that was internally converting ASTs to HTML to two functions that have similar signatures to the getHtml() and getHtmlAst(), with some caching feature as well. The tests have been added for these ASTs.

Documentation could be added something like this:

diff --git a/packages/gatsby-transformer-remark/README.md b/packages/gatsby-transformer-remark/README.md
index 423329b..36a0d59 100644
--- a/packages/gatsby-transformer-remark/README.md
+++ b/packages/gatsby-transformer-remark/README.md
@@ -119,6 +119,7 @@ By default, excerpts have a maximum length of 140 characters. You can change the
       node {
         html
         excerpt(pruneLength: 500)
+        excerptAst(pruneLength: 500)
       }
     }
   }
@@ -178,14 +181,15 @@ Any file that does not have the given `excerpt_separator` will fall back to the

 ### Excerpts for non-latin languages

-By default, `excerpt` uses `underscore.string/prune` which doesn't handle non-latin characters ([https://github.com/epeli/underscore.string/issues/418](https://github.com/epeli/underscore.string/issues/418)).
+By default, `excerpt` and `excerptAst` use `underscore.string/prune` which doesn't handle non-latin characters ([https://github.com/epeli/underscore.string/issues/418](https://github.com/epeli/underscore.string/issues/418)).

-If that is the case, you can set `truncate` option on `excerpt` field, like:
+If that is the case, you can set `truncate` option on `excerpt` and `excerptAst` field, like:

 ```graphql
 {
   markdownRemark {
     excerpt(truncate: true)
+    excerptAst(truncate: true)
   }
 }
@chitoku-k chitoku-k changed the title chore(gatsby-transformer-remark): add excerptAst to be exported as a GraphQL field Jan 23, 2019
@chitoku-k
Copy link
Contributor Author

@DSchau Could you help me find why this test sub-plugin caching fails while the others pass? I dug into an actual problem that caused the test to fail but got no luck with it. Thanks in advance.

@chitoku-k
Copy link
Contributor Author

I have finally figured out that the test added in #10892 did not handle Promises that are passed to visit. Each Promise must be wrapped by Promise.all() so that the gatsby-transformer-remark knows the sub-plugins finished their procedures; otherwise, it can easily break with such an unrelated change.

@DSchau
Copy link
Contributor

DSchau commented Jan 24, 2019

@chitoku-k so this PR seems to make sense in general. If we have htmlAST we should also expose an excerptAST similarly.

However - if you don't mind me asking, apart from being consistent here, what is this PR solving?

@chitoku-k
Copy link
Contributor Author

@DSchau My project currently tries to utilize gatsby-remark-component that requires the AST be passed to its compiler. It works fine so far with htmlAsts, but the excerpts had to be manually generated using the same logic. This PR solves this problem rather than making consistency here.
Thanks.

Copy link
Contributor

@DSchau DSchau left a comment

Choose a reason for hiding this comment

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

This looks good! Left a small comment.

Nice catch on the async issue, as well!

`transformer-remark-markdown-excerpt-${format}-${
node.internal.contentDigest
}-${pluginsCacheStr}-${pathPrefixCacheStr}`
const excerptAstCacheKey = node =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't the excerptCacheKey be used with a format? Instead of two of the same function basically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, does it look better?

@chitoku-k
Copy link
Contributor Author

@DSchau Any further thoughts for this?

@DSchau DSchau added status: awaiting author response Additional information has been requested from the author status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response and removed status: awaiting author response Additional information has been requested from the author labels Feb 21, 2019
@DSchau
Copy link
Contributor

DSchau commented Feb 21, 2019

@chitoku-k checking it out now. Thanks for the reminder!

@DSchau DSchau changed the title fix(gatsby-transformer-remark): add excerptAst to be exported as a GraphQL field Feb 21, 2019
@DSchau
Copy link
Contributor

DSchau commented Feb 21, 2019

I took a look! It looks like the arguments passed to excerpt (e.g. pruneLength) do not seem to be taking effect.

For instance, if we consider the following query, we'd expect the excerpt to be 1 character, as well as the excerpt provided in the AST should be 5 characters.

{
  allMarkdownRemark(limit:1) {
    edges {
      node {
        excerpt(pruneLength:1)
        excerptAst(pruneLength:5)
      }
    }
  }
}

This, unfortunately does not seem to be the case! I can dive in in a bit, but I don't believe this to be in a mergeable state quite yet. Would you be able to validate on your end that you're seeing the same?

For the record - I used gatsbyjs.org to test!

For posterity, here's the result of the above GraphQL query.

@DSchau DSchau added status: blocked This issue/PR can't be solved at the moment and shouldn't be closed/merged status: awaiting author response Additional information has been requested from the author status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response and removed status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response status: blocked This issue/PR can't be solved at the moment and shouldn't be closed/merged labels Feb 21, 2019
@chitoku-k
Copy link
Contributor Author

@DSchau Thank you for reviewing and detailed description for the bug!
It turned out that the parameterized fields can never been cached without parameterized cache keys, as any later changes would no longer take effect. There should be chances that these parameters can be included to cache key, but I removed them because it didn't sound good choice to me.

@chitoku-k
Copy link
Contributor Author

Fixed the regression and rebased to master (due to failures on e2e tests).

@DSchau DSchau removed the status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response label Feb 25, 2019
@DSchau
Copy link
Contributor

DSchau commented Feb 25, 2019

@chitoku-k thank you! I'll take one more look at this, and then hopefully get it merged and published today. Thank you for your patience, and for fixing the issues as they arose!

Copy link
Contributor

@DSchau DSchau left a comment

Choose a reason for hiding this comment

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

Doing some final validations now, but I think this looks good. Will merge in a few unless I find anything!

Thanks!

@DSchau DSchau merged commit e59d4ca into gatsbyjs:master Feb 25, 2019
@chitoku-k
Copy link
Contributor Author

I appreciate all your reviews! Thanks.

@chitoku-k chitoku-k deleted the feature/transformer-remark branch February 26, 2019 02:53
DSchau pushed a commit that referenced this pull request Mar 19, 2019
…12647)

## Description

Fixes a bug in `cloneTreeUntil` function in `gatsby-transformer-remark`, where it only returns the last child node. This function is expected to clone a `root` node, but the current implementation forgets to revert the variable back to the `root` node. Reassigning `clonedRoot = newNode` (originally this is the argument `root`) should fix this. By resolving this bug, it appears to correctly returns the entire root node of AST in `excerptAst` of the markdown node, instead of returning the partial result.

## Related Issues

Related to #11237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting author response Additional information has been requested from the author
2 participants