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): Allow for multiple different remark sources #7512

Merged
merged 4 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into pr/7512
  • Loading branch information
wardpeet committed Mar 13, 2019
commit 6a263d258d339222882b555975bfcf54d1d09dd8
74 changes: 74 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,80 @@ This is [a reference]
)
})

describe(`Headings are generated correctly from schema`, () => {
bootstrapTest(
`returns value`,
`
# first title

## second title
`,
`headings {
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
value: `first title`,
depth: 1,
},
{
value: `second title`,
depth: 2,
},
])
}
)

bootstrapTest(
`returns value with inlineCode`,
`
# first title

## \`second title\`
`,
`headings {
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
value: `first title`,
depth: 1,
},
{
value: `second title`,
depth: 2,
},
])
}
)

bootstrapTest(
`returns value with mixed text`,
`
# An **important** heading with \`inline code\` and text
`,
`headings {
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
value: `An important heading with inline code and text`,
depth: 1,
},
])
}
)
})

describe(`Adding fields to the GraphQL schema`, () => {
it(`only adds fields when the GraphQL type matches the provided type`, async () => {
const getNode = jest.fn()
Expand Down
44 changes: 22 additions & 22 deletions packages/gatsby-transformer-remark/src/__tests__/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
expect(actions.createParentChildLink).toHaveBeenCalledTimes(1)
})
})
})

describe(`date formatting`, () => {
const getContent = date => `---
date: ${date}
---

yadda yadda
`

it(`coerces a date object into a date string`, async () => {
const date = `2019-01-01`
node.content = getContent(date)
const parsed = await onCreateNode({
node,
actions,
createNodeId,
loadNodeContent,
})

expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON())
})

it(`Filters nodes with the given filter function, if provided`, async () => {
const content = ``
Expand Down Expand Up @@ -143,28 +165,6 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
})
})

describe(`date formatting`, () => {
const getContent = date => `---
date: ${date}
---

yadda yadda
`

it(`coerces a date object into a date string`, async () => {
const date = `2019-01-01`
node.content = getContent(date)
const parsed = await onCreateNode({
node,
actions,
createNodeId,
loadNodeContent,
})

expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON())
})
})

describe(`process graphql correctly`, () => {
// given a set of nodes and a query, return the result of the query
async function queryResult(nodes, fragment, { types = [] } = {}) {
Expand Down
186 changes: 115 additions & 71 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const visit = require(`unist-util-visit`)
const toHAST = require(`mdast-util-to-hast`)
const hastToHTML = require(`hast-util-to-html`)
const mdastToToc = require(`mdast-util-toc`)
const mdastToString = require(`mdast-util-to-string`)
const unified = require(`unified`)
const parse = require(`remark-parse`)
const stringify = require(`remark-stringify`)
Expand Down Expand Up @@ -429,6 +430,91 @@ module.exports = (
}
}

async function getExcerptAst(
markdownNode,
{ pruneLength, truncate, excerptSeparator }
) {
const fullAST = await getHTMLAst(markdownNode)
if (excerptSeparator) {
return cloneTreeUntil(
fullAST,
({ nextNode }) =>
nextNode.type === `raw` && nextNode.value === excerptSeparator
)
}
if (!fullAST.children.length) {
return fullAST
}

const excerptAST = cloneTreeUntil(fullAST, ({ root }) => {
const totalExcerptSoFar = getConcatenatedValue(root)
return totalExcerptSoFar && totalExcerptSoFar.length > pruneLength
})
const unprunedExcerpt = getConcatenatedValue(excerptAST)
if (
!unprunedExcerpt ||
(pruneLength && unprunedExcerpt.length < pruneLength)
) {
return excerptAST
}

const lastTextNode = findLastTextNode(excerptAST)
const amountToPruneLastNode =
pruneLength - (unprunedExcerpt.length - lastTextNode.value.length)
if (!truncate) {
lastTextNode.value = prune(
lastTextNode.value,
amountToPruneLastNode,
`…`
)
} else {
lastTextNode.value = _.truncate(lastTextNode.value, {
length: pruneLength,
omission: `…`,
})
}
return excerptAST
}

async function getExcerpt(
markdownNode,
{ format, pruneLength, truncate, excerptSeparator }
) {
if (format === `html`) {
const excerptAST = await getExcerptAst(markdownNode, {
pruneLength,
truncate,
excerptSeparator,
})
const html = hastToHTML(excerptAST, {
allowDangerousHTML: true,
})
return html
}

if (markdownNode.excerpt) {
return markdownNode.excerpt
}

const text = await getAST(markdownNode).then(ast => {
const excerptNodes = []
visit(ast, node => {
if (node.type === `text` || node.type === `inlineCode`) {
excerptNodes.push(node.value)
}
return
})
if (!truncate) {
return prune(excerptNodes.join(` `), pruneLength, `…`)
}
return _.truncate(excerptNodes.join(` `), {
length: pruneLength,
omission: `…`,
})
})
return text
}

return resolve({
html: {
type: GraphQLString,
Expand Down Expand Up @@ -461,77 +547,35 @@ module.exports = (
defaultValue: `plain`,
},
},
async resolve(markdownNode, { format, pruneLength, truncate }) {
if (format === `html`) {
if (grayMatterOptions.excerpt_separator) {
const fullAST = await getHTMLAst(markdownNode)
const excerptAST = cloneTreeUntil(
fullAST,
({ nextNode }) =>
nextNode.type === `raw` &&
nextNode.value === grayMatterOptions.excerpt_separator
)
return hastToHTML(excerptAST, {
allowDangerousHTML: true,
})
}
const fullAST = await getHTMLAst(markdownNode)
if (!fullAST.children.length) {
return ``
}

const excerptAST = cloneTreeUntil(fullAST, ({ root }) => {
const totalExcerptSoFar = getConcatenatedValue(root)
return totalExcerptSoFar && totalExcerptSoFar.length > pruneLength
})
const unprunedExcerpt = getConcatenatedValue(excerptAST)
if (!unprunedExcerpt) {
return ``
}

if (pruneLength && unprunedExcerpt.length < pruneLength) {
return hastToHTML(excerptAST, {
allowDangerousHTML: true,
})
}

const lastTextNode = findLastTextNode(excerptAST)
const amountToPruneLastNode =
pruneLength - (unprunedExcerpt.length - lastTextNode.value.length)
if (!truncate) {
lastTextNode.value = prune(
lastTextNode.value,
amountToPruneLastNode,
`…`
)
} else {
lastTextNode.value = _.truncate(lastTextNode.value, {
length: pruneLength,
omission: `…`,
})
}
return hastToHTML(excerptAST, {
allowDangerousHTML: true,
})
}
if (markdownNode.excerpt) {
return Promise.resolve(markdownNode.excerpt)
}
return getAST(markdownNode).then(ast => {
const excerptNodes = []
visit(ast, node => {
if (node.type === `text` || node.type === `inlineCode`) {
excerptNodes.push(node.value)
}
return
})
if (!truncate) {
return prune(excerptNodes.join(` `), pruneLength, `…`)
}
return _.truncate(excerptNodes.join(` `), {
length: pruneLength,
omission: `…`,
})
resolve(markdownNode, { format, pruneLength, truncate }) {
return getExcerpt(markdownNode, {
format,
pruneLength,
truncate,
excerptSeparator: grayMatterOptions.excerpt_separator,
})
},
},
excerptAst: {
type: GraphQLJSON,
args: {
pruneLength: {
type: GraphQLInt,
defaultValue: 140,
},
truncate: {
type: GraphQLBoolean,
defaultValue: false,
},
},
resolve(markdownNode, { pruneLength, truncate }) {
return getExcerptAst(markdownNode, {
pruneLength,
truncate,
excerptSeparator: grayMatterOptions.excerpt_separator,
}).then(ast => {
const strippedAst = stripPosition(_.clone(ast), true)
return hastReparseRaw(strippedAst)
})
},
},
Expand Down
13 changes: 11 additions & 2 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ module.exports = async function onCreateNode(
const content = await loadNodeContent(node)

try {
const data = grayMatter(content, grayMatterOptions)
let data = grayMatter(content, grayMatterOptions)

const markdownNode = {
if (data.data) {
data.data = _.mapValues(data.data, value => {
if (_.isDate(value)) {
return value.toJSON()
}
return value
})
}

let markdownNode = {
id: createNodeId(`${node.id} >>> ${type}`),
children: [],
parent: node.id,
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.