Skip to content

Commit e59d4ca

Browse files
chitoku-kDSchau
authored andcommitted
feat(gatsby-transformer-remark): add excerptAst to be exported as a GraphQL 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 ([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) } } ```
1 parent 52cb180 commit e59d4ca

File tree

4 files changed

+552
-78
lines changed

4 files changed

+552
-78
lines changed

e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ const visit = require(`unist-util-visit`)
22
const { id } = require(`./constants`)
33

44
module.exports = function remarkPlugin({ cache, markdownAST }) {
5-
visit(markdownAST, `html`, async node => {
6-
if (node.value.match(id)) {
7-
const value = await cache.get(id)
8-
node.value = node.value.replace(/%SUBCACHE_VALUE%/, value)
9-
}
5+
const promises = []
6+
7+
visit(markdownAST, `html`, node => {
8+
promises.push(
9+
(async () => {
10+
if (node.value.match(id)) {
11+
const value = await cache.get(id)
12+
node.value = node.value.replace(/%SUBCACHE_VALUE%/, value)
13+
}
14+
})()
15+
)
1016
})
17+
18+
return Promise.all(promises)
1119
}

packages/gatsby-transformer-remark/src/__tests__/__snapshots__/extend-node.js.snap

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
exports[`Excerpt is generated correctly from schema correctly loads a default excerpt 1`] = `
44
Object {
55
"excerpt": "",
6+
"excerptAst": Object {
7+
"children": Array [],
8+
"data": Object {
9+
"quirksMode": false,
10+
},
11+
"type": "root",
12+
},
613
"frontmatter": Object {
714
"title": "my little pony",
815
},
@@ -12,6 +19,17 @@ Object {
1219
exports[`Excerpt is generated correctly from schema correctly loads an excerpt 1`] = `
1320
Object {
1421
"excerpt": "Where oh where is my little pony?",
22+
"excerptAst": Object {
23+
"children": Array [
24+
Object {
25+
"type": "text",
26+
"value": "Where oh where is my little pony?",
27+
},
28+
],
29+
"properties": Object {},
30+
"tagName": "p",
31+
"type": "element",
32+
},
1533
"frontmatter": Object {
1634
"title": "my little pony",
1735
},
@@ -21,6 +39,17 @@ Object {
2139
exports[`Excerpt is generated correctly from schema correctly prunes length to default value 1`] = `
2240
Object {
2341
"excerpt": "Where oh where is my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla…",
42+
"excerptAst": Object {
43+
"children": Array [
44+
Object {
45+
"type": "text",
46+
"value": "Where oh where is my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla…",
47+
},
48+
],
49+
"properties": Object {},
50+
"tagName": "p",
51+
"type": "element",
52+
},
2453
"frontmatter": Object {
2554
"title": "my little pony",
2655
},
@@ -30,6 +59,17 @@ Object {
3059
exports[`Excerpt is generated correctly from schema correctly prunes length to provided parameter 1`] = `
3160
Object {
3261
"excerpt": "Where oh where is my little pony? Lorem ipsum…",
62+
"excerptAst": Object {
63+
"children": Array [
64+
Object {
65+
"type": "text",
66+
"value": "Where oh where is my little pony? Lorem ipsum…",
67+
},
68+
],
69+
"properties": Object {},
70+
"tagName": "p",
71+
"type": "element",
72+
},
3373
"frontmatter": Object {
3474
"title": "my little pony",
3575
},
@@ -39,6 +79,17 @@ Object {
3979
exports[`Excerpt is generated correctly from schema correctly prunes length to provided parameter with truncate 1`] = `
4080
Object {
4181
"excerpt": "Where oh where is my little pony? Lorem ipsum dol…",
82+
"excerptAst": Object {
83+
"children": Array [
84+
Object {
85+
"type": "text",
86+
"value": "Where oh where is my little pony? Lorem ipsum dol…",
87+
},
88+
],
89+
"properties": Object {},
90+
"tagName": "p",
91+
"type": "element",
92+
},
4293
"frontmatter": Object {
4394
"title": "my little pony",
4495
},
@@ -49,6 +100,30 @@ exports[`Excerpt is generated correctly from schema correctly uses excerpt separ
49100
Object {
50101
"excerpt": "Where oh where is my little pony?
51102
",
103+
"excerptAst": Object {
104+
"children": Array [
105+
Object {
106+
"children": Array [
107+
Object {
108+
"type": "text",
109+
"value": "Where oh where is my little pony?",
110+
},
111+
],
112+
"properties": Object {},
113+
"tagName": "p",
114+
"type": "element",
115+
},
116+
Object {
117+
"type": "text",
118+
"value": "
119+
",
120+
},
121+
],
122+
"data": Object {
123+
"quirksMode": false,
124+
},
125+
"type": "root",
126+
},
52127
"frontmatter": Object {
53128
"title": "my little pony",
54129
},
@@ -58,6 +133,76 @@ Object {
58133
exports[`Excerpt is generated correctly from schema given an html format, it correctly maps nested markdown to html 1`] = `
59134
Object {
60135
"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>",
136+
"excerptAst": Object {
137+
"children": Array [
138+
Object {
139+
"type": "text",
140+
"value": "Where oh ",
141+
},
142+
Object {
143+
"children": Array [
144+
Object {
145+
"children": Array [
146+
Object {
147+
"type": "text",
148+
"value": "where",
149+
},
150+
],
151+
"properties": Object {},
152+
"tagName": "em",
153+
"type": "element",
154+
},
155+
],
156+
"properties": Object {
157+
"href": "nick.com",
158+
},
159+
"tagName": "a",
160+
"type": "element",
161+
},
162+
Object {
163+
"type": "text",
164+
"value": " ",
165+
},
166+
Object {
167+
"children": Array [
168+
Object {
169+
"children": Array [
170+
Object {
171+
"type": "text",
172+
"value": "is",
173+
},
174+
],
175+
"properties": Object {},
176+
"tagName": "em",
177+
"type": "element",
178+
},
179+
],
180+
"properties": Object {},
181+
"tagName": "strong",
182+
"type": "element",
183+
},
184+
Object {
185+
"type": "text",
186+
"value": " ",
187+
},
188+
Object {
189+
"children": Array [],
190+
"properties": Object {
191+
"alt": "that pony",
192+
"src": "pony.png",
193+
},
194+
"tagName": "img",
195+
"type": "element",
196+
},
197+
Object {
198+
"type": "text",
199+
"value": "?",
200+
},
201+
],
202+
"properties": Object {},
203+
"tagName": "p",
204+
"type": "element",
205+
},
61206
"frontmatter": Object {
62207
"title": "my little pony",
63208
},
@@ -67,6 +212,17 @@ Object {
67212
exports[`Excerpt is generated correctly from schema given an html format, it prunes large excerpts 1`] = `
68213
Object {
69214
"excerpt": "<p>Where oh where is that pony? Is he in the stable…</p>",
215+
"excerptAst": Object {
216+
"children": Array [
217+
Object {
218+
"type": "text",
219+
"value": "Where oh where is that pony? Is he in the stable…",
220+
},
221+
],
222+
"properties": Object {},
223+
"tagName": "p",
224+
"type": "element",
225+
},
70226
"frontmatter": Object {
71227
"title": "my little pony",
72228
},
@@ -77,6 +233,45 @@ exports[`Excerpt is generated correctly from schema given an html format, it res
77233
Object {
78234
"excerpt": "<p>Where oh where is that <em>pony</em>? Is he in the stable or by the stream?</p>
79235
",
236+
"excerptAst": Object {
237+
"children": Array [
238+
Object {
239+
"children": Array [
240+
Object {
241+
"type": "text",
242+
"value": "Where oh where is that ",
243+
},
244+
Object {
245+
"children": Array [
246+
Object {
247+
"type": "text",
248+
"value": "pony",
249+
},
250+
],
251+
"properties": Object {},
252+
"tagName": "em",
253+
"type": "element",
254+
},
255+
Object {
256+
"type": "text",
257+
"value": "? Is he in the stable or by the stream?",
258+
},
259+
],
260+
"properties": Object {},
261+
"tagName": "p",
262+
"type": "element",
263+
},
264+
Object {
265+
"type": "text",
266+
"value": "
267+
",
268+
},
269+
],
270+
"data": Object {
271+
"quirksMode": false,
272+
},
273+
"type": "root",
274+
},
80275
"frontmatter": Object {
81276
"title": "my little pony",
82277
},
@@ -86,6 +281,32 @@ Object {
86281
exports[`Excerpt is generated correctly from schema given raw html in the text body, this html is not escaped 1`] = `
87282
Object {
88283
"excerpt": "<p>Where is my <code>pony</code> named leo?</p>",
284+
"excerptAst": Object {
285+
"children": Array [
286+
Object {
287+
"type": "text",
288+
"value": "Where is my ",
289+
},
290+
Object {
291+
"children": Array [
292+
Object {
293+
"type": "text",
294+
"value": "pony",
295+
},
296+
],
297+
"properties": Object {},
298+
"tagName": "code",
299+
"type": "element",
300+
},
301+
Object {
302+
"type": "text",
303+
"value": " named leo?",
304+
},
305+
],
306+
"properties": Object {},
307+
"tagName": "p",
308+
"type": "element",
309+
},
89310
"frontmatter": Object {
90311
"title": "my little pony",
91312
},

0 commit comments

Comments
 (0)