You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
}
}
```
Copy file name to clipboardExpand all lines: packages/gatsby-transformer-remark/src/__tests__/__snapshots__/extend-node.js.snap
+221Lines changed: 221 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,13 @@
3
3
exports[`Excerpt is generated correctly from schema correctly loads a default excerpt 1`] =`
4
4
Object {
5
5
"excerpt": "",
6
+
"excerptAst": Object {
7
+
"children": Array [],
8
+
"data": Object {
9
+
"quirksMode": false,
10
+
},
11
+
"type": "root",
12
+
},
6
13
"frontmatter": Object {
7
14
"title": "my little pony",
8
15
},
@@ -12,6 +19,17 @@ Object {
12
19
exports[`Excerpt is generated correctly from schema correctly loads an excerpt 1`] =`
13
20
Object {
14
21
"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
+
},
15
33
"frontmatter": Object {
16
34
"title": "my little pony",
17
35
},
@@ -21,6 +39,17 @@ Object {
21
39
exports[`Excerpt is generated correctly from schema correctly prunes length to default value 1`] =`
22
40
Object {
23
41
"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
+
},
24
53
"frontmatter": Object {
25
54
"title": "my little pony",
26
55
},
@@ -30,6 +59,17 @@ Object {
30
59
exports[`Excerpt is generated correctly from schema correctly prunes length to provided parameter 1`] =`
31
60
Object {
32
61
"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
+
},
33
73
"frontmatter": Object {
34
74
"title": "my little pony",
35
75
},
@@ -39,6 +79,17 @@ Object {
39
79
exports[`Excerpt is generated correctly from schema correctly prunes length to provided parameter with truncate 1`] =`
40
80
Object {
41
81
"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
+
},
42
93
"frontmatter": Object {
43
94
"title": "my little pony",
44
95
},
@@ -49,6 +100,30 @@ exports[`Excerpt is generated correctly from schema correctly uses excerpt separ
49
100
Object {
50
101
"excerpt": "Where oh where is my little pony?
51
102
",
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
+
},
52
127
"frontmatter": Object {
53
128
"title": "my little pony",
54
129
},
@@ -58,6 +133,76 @@ Object {
58
133
exports[`Excerpt is generated correctly from schema given an html format, it correctly maps nested markdown to html 1`] =`
0 commit comments