Skip to content

Commit

Permalink
feat(gatsby-transformer-remark): add excerptAst to be exported as a G…
Browse files Browse the repository at this point in the history
…raphQL field (#11237)

## 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
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 ([esamattis/underscore.string#418).
+By default, `excerpt` and `excerptAst` use `underscore.string/prune` which doesn't handle non-latin characters ([esamattis/underscore.string#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)
   }
 }
```
  • Loading branch information
chitoku-k authored and DSchau committed Feb 25, 2019
1 parent 52cb180 commit e59d4ca
Show file tree
Hide file tree
Showing 4 changed files with 552 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ const visit = require(`unist-util-visit`)
const { id } = require(`./constants`)

module.exports = function remarkPlugin({ cache, markdownAST }) {
visit(markdownAST, `html`, async node => {
if (node.value.match(id)) {
const value = await cache.get(id)
node.value = node.value.replace(/%SUBCACHE_VALUE%/, value)
}
const promises = []

visit(markdownAST, `html`, node => {
promises.push(
(async () => {
if (node.value.match(id)) {
const value = await cache.get(id)
node.value = node.value.replace(/%SUBCACHE_VALUE%/, value)
}
})()
)
})

return Promise.all(promises)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
exports[`Excerpt is generated correctly from schema correctly loads a default excerpt 1`] = `
Object {
"excerpt": "",
"excerptAst": Object {
"children": Array [],
"data": Object {
"quirksMode": false,
},
"type": "root",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -12,6 +19,17 @@ Object {
exports[`Excerpt is generated correctly from schema correctly loads an excerpt 1`] = `
Object {
"excerpt": "Where oh where is my little pony?",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is my little pony?",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -21,6 +39,17 @@ Object {
exports[`Excerpt is generated correctly from schema correctly prunes length to default value 1`] = `
Object {
"excerpt": "Where oh where is my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla…",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla…",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -30,6 +59,17 @@ Object {
exports[`Excerpt is generated correctly from schema correctly prunes length to provided parameter 1`] = `
Object {
"excerpt": "Where oh where is my little pony? Lorem ipsum…",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is my little pony? Lorem ipsum…",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -39,6 +79,17 @@ Object {
exports[`Excerpt is generated correctly from schema correctly prunes length to provided parameter with truncate 1`] = `
Object {
"excerpt": "Where oh where is my little pony? Lorem ipsum dol…",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is my little pony? Lorem ipsum dol…",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -49,6 +100,30 @@ exports[`Excerpt is generated correctly from schema correctly uses excerpt separ
Object {
"excerpt": "Where oh where is my little pony?
",
"excerptAst": Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is my little pony?",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
Object {
"type": "text",
"value": "
",
},
],
"data": Object {
"quirksMode": false,
},
"type": "root",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -58,6 +133,76 @@ 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 href=\\"nick.com\\"><em>where</em></a> <strong><em>is</em></strong> <img src=\\"pony.png\\" alt=\\"that pony\\">?</p>",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh ",
},
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "where",
},
],
"properties": Object {},
"tagName": "em",
"type": "element",
},
],
"properties": Object {
"href": "nick.com",
},
"tagName": "a",
"type": "element",
},
Object {
"type": "text",
"value": " ",
},
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "is",
},
],
"properties": Object {},
"tagName": "em",
"type": "element",
},
],
"properties": Object {},
"tagName": "strong",
"type": "element",
},
Object {
"type": "text",
"value": " ",
},
Object {
"children": Array [],
"properties": Object {
"alt": "that pony",
"src": "pony.png",
},
"tagName": "img",
"type": "element",
},
Object {
"type": "text",
"value": "?",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -67,6 +212,17 @@ Object {
exports[`Excerpt is generated correctly from schema given an html format, it prunes large excerpts 1`] = `
Object {
"excerpt": "<p>Where oh where is that pony? Is he in the stable…</p>",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is that pony? Is he in the stable…",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -77,6 +233,45 @@ exports[`Excerpt is generated correctly from schema given an html format, it res
Object {
"excerpt": "<p>Where oh where is that <em>pony</em>? Is he in the stable or by the stream?</p>
",
"excerptAst": Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "Where oh where is that ",
},
Object {
"children": Array [
Object {
"type": "text",
"value": "pony",
},
],
"properties": Object {},
"tagName": "em",
"type": "element",
},
Object {
"type": "text",
"value": "? Is he in the stable or by the stream?",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
Object {
"type": "text",
"value": "
",
},
],
"data": Object {
"quirksMode": false,
},
"type": "root",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand All @@ -86,6 +281,32 @@ Object {
exports[`Excerpt is generated correctly from schema given raw html in the text body, this html is not escaped 1`] = `
Object {
"excerpt": "<p>Where is my <code>pony</code> named leo?</p>",
"excerptAst": Object {
"children": Array [
Object {
"type": "text",
"value": "Where is my ",
},
Object {
"children": Array [
Object {
"type": "text",
"value": "pony",
},
],
"properties": Object {},
"tagName": "code",
"type": "element",
},
Object {
"type": "text",
"value": " named leo?",
},
],
"properties": Object {},
"tagName": "p",
"type": "element",
},
"frontmatter": Object {
"title": "my little pony",
},
Expand Down
Loading

0 comments on commit e59d4ca

Please sign in to comment.