Skip to content

Support graymatter data in asciidoc files #33403

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/using-asciidoc/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ module.exports = {
path: `${__dirname}/src`,
},
},
`gatsby-transformer-asciidoc`,
{
resolve: `gatsby-transformer-asciidoc`,
options: {
attributes: {
'skip-front-matter': true
}
}
},
{
resolve: `gatsby-plugin-typography`,
options: {
Expand Down
16 changes: 16 additions & 0 deletions examples/using-asciidoc/src/pages/front-matter.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
:layout: post
source: halkeye
---
= Testing front matter
Doc Writer <[email protected]>

An introduction to http://asciidoc.org[AsciiDoc].

== First Section

* item 1
* item 2

[source,ruby]
puts "Hello, World!"
18 changes: 18 additions & 0 deletions examples/using-asciidoc/src/templates/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ class Article extends React.Component {
</tbody>
</table>
)}
{this.props.data.asciidoc.frontmatter && (
<table>
<thead>
<tr>
<td colSpan="2">Frontmatter</td>
</tr>
</thead>
<tbody>
<tr>
<th>frontmatter.source</th>
<td>{this.props.data.asciidoc.frontmatter.source}</td>
</tr>
</tbody>
</table>
)}
<div
dangerouslySetInnerHTML={{ __html: this.props.data.asciidoc.html }}
/>
Expand Down Expand Up @@ -98,6 +113,9 @@ export const pageQuery = graphql`
authorInitials
email
}
frontmatter {
source
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none of the example asciidoctor files have frontmatter yet right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh shoot i forgot to git add, which explained why ci failed

}
}
}
`
16 changes: 16 additions & 0 deletions packages/gatsby-transformer-asciidoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ In the asciidoc file you can insert your image just by using:
- In case a `pathPrefix` is set it will altered the images location.
- In case you want to be able to override the defined imagesdir inside of your asciidoc file you have to end the path with a `@` (e.g. `/images@`).

## Support front/graymatter

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-asciidoc`,
options: {
attributes: {
"skip-front-matter": true,
},
},
},
]
```

## How to query

A sample GraphQL query to get AsciiDoc nodes:
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-transformer-asciidoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"dependencies": {
"@babel/runtime": "^7.15.4",
"asciidoctor": "^2.2.5"
"asciidoctor": "^2.2.5",
"gray-matter": "^4.0.2"
},
"devDependencies": {
"@babel/cli": "^7.15.4",
Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby-transformer-asciidoc/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const asciidoc = require(`asciidoctor`)()
const grayMatter = require(`gray-matter`)
const _ = require(`lodash`)

function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) {
Expand Down Expand Up @@ -97,6 +98,12 @@ async function onCreateNode(
pageAttributes,
}

if (doc.getAttribute(`front-matter`)) {
asciiNode.frontmatter = grayMatter(
`---\n` + doc.getAttribute(`front-matter`)
).data
}

asciiNode.internal.contentDigest = createContentDigest(asciiNode)

createNode(asciiNode)
Expand Down