-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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, gatsby-plugin-mdx) Make timeToRead configurable #19763
Conversation
I went ahead and added support to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me.
I wonder how frequent this feature is requested to warrant it being an option / api.
That is a good question. My guess would be that it isn't something that a lot of people care about. Searching What I do think could be improved more generally with the default calculation is switching away from using the HTML string to something without syntax of any kind. It should be possible to parse Markdown AST to do this in a fairly straightforward manner. If you look at this line of the PR, the expected count should be |
We are doing some pure text extraction / stripping tags for |
Also, if current |
@johnrichter @pieh what do we want to do with this? John, the changes you suggested sound good in any case, do you have space to try and improve that system? |
I have a bit of time next week to try to get a more accurate solution in place. @pieh's idea of adapting the plain excerpt implementation for this is a great starting point. I'd like to keep an escape hatch for those that would want to customize the calculation with the goal of making it useful for edge cases like customizing how long inline React components would take to read. I'm thinking that I could try to do a bit of research to figure out good defaults for the the time it takes to process images and code blocks. The latter of which will be tricky since some languages are much more readable than others. Paul Stamatiou recently mentioned that he
That makes me hopeful that I could find some sort of useful default values. |
@johnrichter Hi, do you have time to wrap up this PR? :) |
Hi @LekoArts, at the moment I don't have the time to go into the level of detail I mentioned above. If the changes in this PR are good enough for now I have enough spare cycles to rebase and complete whatever else is needed to get this shipped. My apologies for not getting back to y'all on this PR sooner! I still want to improve the calculation in a more meaningful way in the future, but if you're looking at officially overhauling it by all means go for it 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the function signatures not match
packages/gatsby-plugin-mdx/README.md
Outdated
{ | ||
resolve: `gatsby-plugin-mdx`, | ||
options: { | ||
timeToRead: (wordCount, mdxNode) => wordCount / 42, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not match timeToRead(wordCount, html, md)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated 👍
1, | ||
Math.round( | ||
_.isFunction(timeToRead) | ||
? timeToRead(words, mdxNode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not match timeToRead(wordCount, html, md)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 I had to add some functionality, but the timeToRead
signature now matches for both MDX and Remark.
1773739
to
a466767
Compare
I've rebased on master as well |
@@ -604,7 +605,9 @@ module.exports = ( | |||
timeToRead: { | |||
type: `Int`, | |||
resolve(markdownNode) { | |||
return getHTML(markdownNode).then(timeToRead) | |||
return getHTML(markdownNode).then(html => | |||
timeToRead(markdownNode, html, calcTimeToRead) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you rename the utils function, it is more easy to understand (i asked myself why timeToRead(markdownNode, html, calcTimeToRead)
as a different signature compared to the other timeToRead(words, html, rawMD*)
)
timeToRead(markdownNode, html, calcTimeToRead) | |
timeToReadCalculator(markdownNode, html, timeToRead) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a great point. I thought it was confusing too to be honest and didn't think about renaming the util function. Let me know if you like the new naming 👍
just now the function gets different TTR? in |
For the original code in the PR, the utility functions hadn't been created yet and all of that was mostly inline. The utility function still resides in the Remark transformer so I don't want to create a dependency on the Remark transformer in the MDX plugin. I could extract the utils to a shared package somewhere (optimal) or just copy and paste them to mdx (have to maintain in both places). Which one depends on what y'all would like to do really since I'm indifferent. Historically, MDX and Remark have had different TTRs (once the character handling code was added to Remark) so I left it as is. For a first pass at creating better time to read calculations the main focus of this PR is passing the responsibility to the user if they want to change it. |
yes you are right, that is not the scope of this PR |
@johnrichter tests are failing even after rebase. If we can get the tests pass I'm inclined to merge it. Please let me know whether you want to fix the tests. |
…o read a document.
Format per contribution guidelines.
c6f32ab
to
0284d48
Compare
Hi @pvdz, I'm working on getting those tests working today. Did a quick rebase on master again before I start working on getting the tests to pass. |
@pvdz Looks like rebasing was all that was needed 👍 |
Holy buckets, @johnrichter — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
@johnrichter Thank you for the additional effort 💜 Sorry it took this long, but happy we can merge it now :D |
You're welcome! Thanks for merging it in. I can't wait to use it when it gets released 😄 Also, I had no idea ya'll hand out swag for PRs 😍 😍 😍 |
@johnrichter we had to revert this change because of Out of memory issues and slow query builds. |
|
i have two ideas:
|
I've been wanting this for a while (since not everyone reads at 265 wpm) and had some time to work on it this evening.
Description
This PR makes
timeToRead
calculation user configurable. I went with the simple solution to start.More advanced potential solution
It would make sense to give the user more access to the markdown node for more flexible and precise calculation (e.g. code samples and complex graphs). I was thinking this could look like
It essentially gives access to all of the
markdownNode
field resolvers.wordCount
would now be based off ofmarkdownNode.rawMarkdownBody
instead of the sanitized HTML. It's more complicated, but gives the user everything they'd need to calculatetimeToRead
as precisely as they'd want.Related Issues
Related to #16760. It would allow users to calculate the value in the they want to vs Gatsby building and maintaining a custom one-size-fits-all solution.
gatsby-plugin-mdx
This plugin also has a
timeToRead
that can be updated with the same simple solution. It doesn't look on first glance that it could be updated to support the more advanced solution above though.