Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit baf4000
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 23:36:17 2017 +0400

    Adds TOML transformer to plugins.

commit bd14359
Merge: 4c336a9 8f7624c
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 23:33:41 2017 +0400

    Merge branch 'master' into toml-plugin

commit 4c336a9
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 23:15:40 2017 +0400

    Adds requested corrections.

commit baec91e
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 23:05:58 2017 +0400

    Adds documentation.

commit 4645096
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 22:47:02 2017 +0400

    Adds tests for the new plugin.

commit 1275245
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 21:19:29 2017 +0400

    Adds the intial version of gatsby-transformer-toml.

commit 0c51cd4
Author: Ruben Harutyunyan <harutyunyan.ruben@gmail.com>
Date:   Thu Jul 6 03:32:21 2017 +0400

    gatsby-transformer-toml project init.

commit 9f2b7e8
Author: Kyle Mathews <mathews.kyle@gmail.com>
Date:   Thu Jul 6 11:54:14 2017 -0700

    Add missing extension
  • Loading branch information
KyleAMathews committed Jul 6, 2017
1 parent 8f7624c commit 414bd0f
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
## [1.0.0] - 2017-07-06

### Added
* Adds Material Blog starter to the list of starters. #1344 @Vagr9K committed with KyleAMathews 4 days ago
* Adds Material Blog starter to the list of starters. #1344 @Vagr9K committed with KyleAMathews 4 days ago
* Continuation: WIP update home page with new design #1355 @fk
* Wordpress source plugin and example site #1321 @sebastienfi
* [v1.0] Documentation improvements. #1370 @Vagr9K
* 1.0.0 announcement blog post #1379 @KyleAMathews
* Adds gatsby-transformer-toml to the core. ##1382 @Vagr9K

### Fixed
* Update README to make it clearer about deploying to gh pages #1343 @jsfeb26
Expand Down
1 change: 1 addition & 0 deletions docs/docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ you can place the files in a `src` subfolder and build them to the plugin folder
* [gatsby-transformer-remark](/docs/packages/gatsby-transformer-remark/)
* [gatsby-transformer-sharp](/docs/packages/gatsby-transformer-sharp/)
* [gatsby-transformer-yaml](/docs/packages/gatsby-transformer-yaml/)
* [gatsby-transformer-toml](/docs/packages/gatsby-transformer-toml/)

## Official components

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-transformer-toml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/gatsby-node.js
34 changes: 34 additions & 0 deletions packages/gatsby-transformer-toml/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
72 changes: 72 additions & 0 deletions packages/gatsby-transformer-toml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# gatsby-transformer-toml

Parses TOML files.

## Install

`npm install --save gatsby-transformer-toml`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
`gatsby-transformer-toml`,
]
```

## Parsing algorithm

This plugin is using NPM [toml](https://www.npmjs.com/package/toml) package to parse TOML documents.
As long as your TOML is valid, you shouldn't have any issues.

Live demo of TOML to JSON conversion using [toml](https://www.npmjs.com/package/toml) is [here](http://binarymuse.github.io/toml-node/).

If you have `user.toml` in you project, with contents like this:

```toml
userName = "Random User"
userAvatar = "https://api.adorable.io/avatars/150/test.png"
userDescription = "Lorem..."
[userLink]
label='Website'
url='//mywebsite.example.local'
icon='fa fa-link'
```

Then you'll be able to query your data using:

```graphql
query MyQuery {
userToml {
userName
userAvatar
userDescription
userLink {
label
url
icon
}
}
}

```

And the result will be:

```javascript
{
"data": {
"userToml": {
"userName": "Random User",
"userAvatar": "https://api.adorable.io/avatars/150/test.png",
"userDescription": "Lorem...",
"userLink": {
"label": "Website",
"url": "//mywebsite.example.local",
"icon": "fa fa-link"
}
}
}
}
```
1 change: 1 addition & 0 deletions packages/gatsby-transformer-toml/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
23 changes: 23 additions & 0 deletions packages/gatsby-transformer-toml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "gatsby-transformer-toml",
"version": "1.0.0-beta.7",
"description": "Gatsby transformer plugin for toml",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__"
},
"keywords": [
"gatsby",
"gatsby-plugin",
"toml"
],
"author": "Ruben Harutyunyan <vagr9k@gmail.com>",
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.0",
"toml": "^2.3.2"
},
"devDependencies": {
"babel-cli": "^6.24.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Process TOML nodes correctly Correctly creates nodes from TOML test file 1`] = `
Array [
Array [
Object {
"children": Array [],
"id": "whatever >>> TOML",
"internal": Object {
"contentDigest": "9ce7919cb3f607a0542bac4aa46136b6",
"type": "",
},
"parent": "whatever",
"the": Object {
"hard": Object {
"another_test_string": " Same thing, but with a string #",
"bit#": Object {
"multi_line_array": Array [
"]",
],
"what?": "You don't think some user won't do that?",
},
"harder_test_string": " And when \\"'s are in the string, along with # \\"",
"test_array": Array [
"] ",
" # ",
],
"test_array2": Array [
"Test #11 ]proved that",
"Experiment #9 was a success",
],
},
"test_string": "You'll hate me after this - #",
},
},
],
]
`;

exports[`Process TOML nodes correctly Correctly creates nodes from TOML test file 2`] = `
Array [
Array [
Object {
"child": Object {
"children": Array [],
"id": "whatever >>> TOML",
"internal": Object {
"contentDigest": "9ce7919cb3f607a0542bac4aa46136b6",
"type": "",
},
"parent": "whatever",
"the": Object {
"hard": Object {
"another_test_string": " Same thing, but with a string #",
"bit#": Object {
"multi_line_array": Array [
"]",
],
"what?": "You don't think some user won't do that?",
},
"harder_test_string": " And when \\"'s are in the string, along with # \\"",
"test_array": Array [
"] ",
" # ",
],
"test_array2": Array [
"Test #11 ]proved that",
"Experiment #9 was a success",
],
},
"test_string": "You'll hate me after this - #",
},
},
"parent": Object {
"children": Array [],
"content": "
[the]
test_string = \\"You'll hate me after this - #\\"
[the.hard]
test_array = [ \\"] \\", \\" # \\"] # ]
test_array2 = [ \\"Test #11 ]proved that\\", \\"Experiment #9 was a success\\" ]
another_test_string = \\" Same thing, but with a string #\\"
harder_test_string = \\" And when \\\\\\"'s are in the string, along with # \\\\\\"\\"
# Things will get harder
[the.hard.\\"bit#\\"]
\\"what?\\" = \\"You don't think some user won't do that?\\"
multi_line_array = [
\\"]\\",
# ] Oh yes I did
]
",
"extension": "toml",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
},
"name": "test",
"parent": "SOURCE",
},
},
],
]
`;
82 changes: 82 additions & 0 deletions packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const Promise = require(`bluebird`)
const _ = require(`lodash`)

const { onCreateNode } = require(`../gatsby-node`)

describe(`Process TOML nodes correctly`, () => {
const node = {
id: `whatever`,
parent: `SOURCE`,
children: [],
extension: `toml`,
internal: {
contentDigest: `whatever`,
},
name: `test`,
}

// Provide fake functions
const loadNodeContent = node => Promise.resolve(node.content)

it(`Correctly creates nodes from TOML test file`, async () => {
// Unfortunately due to TOML limitations no JSON -> TOML convertors exist,
// which means that we are stuck with JS template literals.
node.content = `
[the]
test_string = "You'll hate me after this - #"
[the.hard]
test_array = [ "] ", " # "] # ]
test_array2 = [ "Test #11 ]proved that", "Experiment #9 was a success" ]
another_test_string = " Same thing, but with a string #"
harder_test_string = " And when \\"'s are in the string, along with # \\""
# Things will get harder
[the.hard."bit#"]
"what?" = "You don't think some user won't do that?"
multi_line_array = [
"]",
# ] Oh yes I did
]
`

const createNode = jest.fn()
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(createParentChildLink).toHaveBeenCalledTimes(1)
})
})

it(`If the object has an id, it uses that as the id instead of the auto-generated one`, async () => {
node.content = `
id = 'foo'
blue = true
funny = 'yup'
`

const createNode = jest.fn()
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls[0][0].id).toEqual(`foo`)
})
})

// Since TOML transformer doesn't generate sub-objects from arrays,
// but directly uses the object, 'id' uniqueness tests between sub-objects
// are omitted.
})
45 changes: 45 additions & 0 deletions packages/gatsby-transformer-toml/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const toml = require(`toml`)
const _ = require(`lodash`)
const crypto = require(`crypto`)

async function onCreateNode({ node, boundActionCreators, loadNodeContent }) {
const { createNode, createParentChildLink } = boundActionCreators
// Filter out non-toml content
// Currently TOML files are considered 'application/octet-stream' in 'mime-db'
// Hence the extension test instead of mediaType test
if (node.extension !== `toml`) {
return
}
// Load TOML contents
const content = await loadNodeContent(node)
// Parse
const parsedContent = toml.parse(content)

// This version suffers from:
// 1) More TOML files -> more types
// 2) Different files with the same name creating conflicts
const parsedContentStr = JSON.stringify(parsedContent)
const contentDigest = crypto
.createHash(`md5`)
.update(parsedContentStr)
.digest(`hex`)

const newNode = {
...parsedContent,
id: parsedContent.id ? parsedContent.id : `${node.id} >>> TOML`,
children: [],
parent: node.id,
internal: {
contentDigest,

// Use the relative filepath as "type"
type: _.upperFirst(_.camelCase(node.relativePath)),
},
}

createNode(newNode)
createParentChildLink({ parent: node, child: newNode })
return
}

exports.onCreateNode = onCreateNode

0 comments on commit 414bd0f

Please sign in to comment.