Skip to content

Commit

Permalink
fix(gatsby-transformer-remark): fix excerpt generation - strip excess…
Browse files Browse the repository at this point in the history
…ive white spaces, extract alt from images (#12878)
  • Loading branch information
macklinu authored and wardpeet committed Apr 25, 2019
1 parent f115994 commit ceb0d72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,25 @@ Where oh [*where*](nick.com) **_is_** ![that pony](pony.png)?`,
}
)

bootstrapTest(
`excerpt does have missing words and extra spaces`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh [*where*](nick.com) **_is_** ![that pony](pony.png)?`,
`excerpt
frontmatter {
title
}
`,
node => {
expect(node.excerpt).toMatch(`Where oh where is that pony?`)
},
{}
)

bootstrapTest(
`given raw html in the text body, this html is not escaped`,
`---
Expand Down
10 changes: 7 additions & 3 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,21 @@ module.exports = (
}

const text = await getAST(markdownNode).then(ast => {
const excerptNodes = []
let excerptNodes = []
visit(ast, node => {
if (node.type === `text` || node.type === `inlineCode`) {
excerptNodes.push(node.value)
}
if (node.type === `image`) {
excerptNodes.push(node.alt)
}
return
})

if (!truncate) {
return prune(excerptNodes.join(` `), pruneLength, `…`)
return prune(excerptNodes.join(``), pruneLength, `…`)
}
return _.truncate(excerptNodes.join(` `), {
return _.truncate(excerptNodes.join(``), {
length: pruneLength,
omission: `…`,
})
Expand Down

0 comments on commit ceb0d72

Please # to comment.