-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
8f7624c
commit 414bd0f
Showing
10 changed files
with
366 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/gatsby-node.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// noop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/gatsby-transformer-toml/src/__tests__/__snapshots__/gatsby-node.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |