Skip to content

Commit

Permalink
Revert "feat(gatsby-transformer-remark, gatsby-plugin-mdx) Make timeT… (
Browse files Browse the repository at this point in the history
#23726)

Revert "feat(gatsby-transformer-remark, gatsby-plugin-mdx) Make timeToRead configurable (#19763)" 

This reverts commit 8586ca3.

Fixes #23614
Fixes #23714
  • Loading branch information
wardpeet authored May 4, 2020
1 parent 075e2da commit 87f82bc
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 98 deletions.
22 changes: 0 additions & 22 deletions packages/gatsby-plugin-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ scope, and more.
| [`rehypePlugins`](#rehype-plugins) | `[]` | Specify rehype plugins |
| [`mediaTypes`](#media-types) | `["text/markdown", "text/x-markdown"]` | Determine which media types are processed by MDX |
| [`shouldBlockNodeFromTransformation`](#shouldblocknodefromtransformation) | `(node) => false` | Disable MDX transformation for nodes where this function returns true |
| [`timeToRead`](#time-to-read) | `wordCount => wordCount / 265` | Calculate `timeToRead` from the word count, html, and raw MDX content |

#### Extensions

Expand Down Expand Up @@ -457,27 +456,6 @@ module.exports = {
}
```

#### Time to read

Calculating the time to read a Markdown document based on the word
count or Markdown node. This is useful for customizing the time to
read a document based on a faster or slower words per minute reading
rate or by custom heuristics based on the markdown node.

```js
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-mdx`,
options: {
timeToRead: (wordCount, html, rawMDX) => wordCount / 42,
},
},
],
}
```

### Components

MDX and `gatsby-plugin-mdx` use components for different things like rendering
Expand Down
63 changes: 27 additions & 36 deletions packages/gatsby-plugin-mdx/gatsby/source-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,6 @@ module.exports = (
...helpers,
})

async function getHTML(mdxNode) {
if (mdxNode.html) {
return Promise.resolve(mdxNode.html)
}
const { body } = await processMDX({ node: mdxNode })
try {
if (!mdxHTMLLoader) {
mdxHTMLLoader = loader({ reporter, cache, store })
}
const html = await mdxHTMLLoader.load({ ...mdxNode, body })
return html
} catch (e) {
reporter.error(
`gatsby-plugin-mdx: Error querying the \`html\` field.
This field is intended for use with RSS feed generation.
If you're trying to use it in application-level code, try querying for \`Mdx.body\` instead.
Original error:
${e}`
)
return undefined
}
}

// New Code // Schema
const MdxType = schema.buildObjectType({
name: `Mdx`,
Expand Down Expand Up @@ -225,7 +202,26 @@ module.exports = (
html: {
type: `String`,
async resolve(mdxNode) {
return await getHTML(mdxNode)
if (mdxNode.html) {
return Promise.resolve(mdxNode.html)
}
const { body } = await processMDX({ node: mdxNode })
try {
if (!mdxHTMLLoader) {
mdxHTMLLoader = loader({ reporter, cache, store })
}
const html = await mdxHTMLLoader.load({ ...mdxNode, body })
return html
} catch (e) {
reporter.error(
`gatsby-plugin-mdx: Error querying the \`html\` field.
This field is intended for use with RSS feed generation.
If you're trying to use it in application-level code, try querying for \`Mdx.body\` instead.
Original error:
${e}`
)
return undefined
}
},
},
mdxAST: {
Expand Down Expand Up @@ -253,20 +249,15 @@ module.exports = (
timeToRead: {
type: `Int`,
async resolve(mdxNode) {
const html = await getHTML(mdxNode)
const { mdast, body } = await processMDX({ node: mdxNode })
const { mdast } = await processMDX({ node: mdxNode })
const { words } = await getCounts({ mdast })
const { timeToRead } = pluginOptions
let timeToRead = 0
const avgWPM = 265
const timeToReadInMinutes = Math.max(
1,
Math.round(
_.isFunction(timeToRead)
? timeToRead(words, html, body)
: words / avgWPM
)
)
return timeToReadInMinutes
timeToRead = Math.round(words / avgWPM)
if (timeToRead === 0) {
timeToRead = 1
}
return timeToRead
},
},
wordCount: {
Expand Down
2 changes: 0 additions & 2 deletions packages/gatsby-transformer-remark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ plugins: [
pedantic: true,
// GitHub Flavored Markdown mode (default: true)
gfm: true,
// Calculate timeToRead in minutes using word count, sanitized html, and raw Markdown content. (default: wordCount / 265)
timeToRead: (wordCount, html, rawMD) => wordCount / 42,
// Plugins configs
plugins: [],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,15 +683,6 @@ Object {
}
`;
exports[`Wordcount and timeToRead are generated correctly from schema correctly uses a custom value for timeToRead 1`] = `
Object {
"frontmatter": Object {
"title": "my little pony",
},
"timeToRead": 9,
}
`;
exports[`Wordcount and timeToRead are generated correctly from schema correctly uses a default value for timeToRead 1`] = `
Object {
"frontmatter": Object {
Expand Down
14 changes: 0 additions & 14 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,20 +916,6 @@ date: "2017-09-18T23:19:51.246Z"
expect(node.timeToRead).toBe(1)
}
)

bootstrapTest(
`correctly uses a custom value for timeToRead`,
`${content}\nWhere oh where is my little pony?\n`,
`timeToRead
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.timeToRead).toBe(9)
},
{ pluginOptions: { timeToRead: wordCount => wordCount / 1 } }
)
})

describe(`Table of contents is generated correctly from schema`, () => {
Expand Down
7 changes: 2 additions & 5 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
findLastTextNode,
} = require(`./hast-processing`)
const codeHandler = require(`./code-handler`)
const { calculateTimeToRead } = require(`./utils/time-to-read`)
const { timeToRead } = require(`./utils/time-to-read`)

let fileNodes
let pluginsCacheStr = ``
Expand Down Expand Up @@ -112,7 +112,6 @@ module.exports = (
heading: null,
maxDepth: 6,
},
timeToRead = null,
} = pluginOptions
const tocOptions = tableOfContents
const remarkOptions = {
Expand Down Expand Up @@ -618,9 +617,7 @@ module.exports = (
timeToRead: {
type: `Int`,
resolve(markdownNode) {
return getHTML(markdownNode).then(html =>
calculateTimeToRead(markdownNode, html, timeToRead)
)
return getHTML(markdownNode).then(timeToRead)
},
},
tableOfContents: {
Expand Down
17 changes: 7 additions & 10 deletions packages/gatsby-transformer-remark/src/utils/time-to-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function isCjChar(char) {
return cjRanges.some(([from, to]) => charCode >= from && charCode < to)
}

export const calculateTimeToRead = (mdNode, html, calcTimeToRead) => {
export const timeToRead = html => {
let timeToRead = 0
const pureText = sanitizeHTML(html, { allowTags: [] })
const avgWPM = 265

Expand All @@ -58,13 +59,9 @@ export const calculateTimeToRead = (mdNode, html, calcTimeToRead) => {
// on average one word consists of 2 characters in both Chinese and Japanese
const wordCount = _.words(latinChars.join(``)).length + cjChars.length * 0.56

const timeToReadInMinutes = Math.max(
1,
Math.round(
_.isFunction(calcTimeToRead)
? calcTimeToRead(wordCount, pureText, mdNode.rawMarkdownBody)
: wordCount / avgWPM
)
)
return timeToReadInMinutes
timeToRead = Math.round(wordCount / avgWPM)
if (timeToRead === 0) {
timeToRead = 1
}
return timeToRead
}

0 comments on commit 87f82bc

Please sign in to comment.