Skip to content
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): add excerptAst to be exported as a GraphQL field #11237

Merged
merged 7 commits into from
Feb 25, 2019
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