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

Add new plugin to handle csv files #1496

Merged
merged 2 commits into from
Jul 19, 2017
Merged
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
8 changes: 8 additions & 0 deletions examples/using-csv/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"browser": true
},
"globals": {
"graphql": false
}
}
3 changes: 3 additions & 0 deletions examples/using-csv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public
.cache
node_modules
3 changes: 3 additions & 0 deletions examples/using-csv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# using-csv

https://using-csv.gatsbyjs.org
22 changes: 22 additions & 0 deletions examples/using-csv/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
siteMetadata: {
title: `gatsby-example-using-csv`,
description: `Blazing-fast React.js static site generator`,
},
plugins: [
`gatsby-transformer-csv`,
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `UA-93349937-2`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/data`,
name: `data`,
},
},
],
}
19 changes: 19 additions & 0 deletions examples/using-csv/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "using-csv",
"private": true,
"description": "Gatsby example site using using-csv",
"author": "Sonal Saldanha <sonal.saldanha@gmail.com>",
"dependencies": {
"gatsby": "latest",
"gatsby-link": "latest",
"gatsby-plugin-google-analytics": "latest",
"gatsby-source-filesystem": "latest",
"gatsby-transformer-csv": "latest"
},
"license": "MIT",
"main": "n/a",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build"
}
}
27 changes: 27 additions & 0 deletions examples/using-csv/src/data/letters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
letter,value
A,65
B,66
C,67
D,68
E,69
F,70
G,71
H,72
I,73
J,74
K,75
L,76
M,77
N,78
O,79
P,80
Q,81
R,82
S,83
T,84
U,85
V,86
W,87
X,88
Y,89
Z,90
44 changes: 44 additions & 0 deletions examples/using-csv/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from "react"

class IndexComponent extends React.Component {
render() {
const data = this.props.data.allLettersCsv.edges
return (
<div>
<table>
<thead>
<tr>
<th>Letter</th>
<th>ASCII Value</th>
</tr>
</thead>
<tbody>
{data.map((row,i) => (<tr key={`${row.node.value} ${i}`}>
<td>
{row.node.letter}
</td>
<td>
{row.node.value}
</td>
</tr>))}
</tbody>
</table>
</div>
)
}
}

export default IndexComponent

export const IndexQuery = graphql`
query IndexQuery {
allLettersCsv {
edges {
node {
letter
value
}
}
}
}
`
98 changes: 97 additions & 1 deletion packages/gatsby-transformer-csv/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
# gatsby-transformer-csv

Stub README
Parses CSV files into JSON arrays.

## Install

`npm install --save gatsby-transformer-csv`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
`gatsby-transformer-csv`,
]
```
Above is the minimal configuration required to begin working. Additional customization
of the parsing process is possible using the parameters listed in
[csvtojson](https://github.com/Keyang/node-csvtojson#parameters).

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-csv`,
options: {
noheader: true
}
}
]
```

## Parsing algorithm

Each row is converted into a node with CSV headers as the keys.

So if your project has a `letters.csv` with
```
letter,value
a,65
b,66
c,67
```
the following three nodes would be created.

```javascript
[
{ letter: 'a', value: 65, type: 'LettersCsv' },
{ letter: 'b', value: 66, type: 'LettersCsv' },
{ letter: 'c', value: 67, type: 'LettersCsv' },
]
```

## How to query

You'd be able to query your letters like:

```graphql
{
allLettersCsv {
edges {
node {
letter
value
}
}
}
}
```

Which would return:

```javascript
{
allLettersCsv: {
edges: [
{
node: {
letter: 'a'
value: 65
}
},
{
node: {
letter: 'b'
value: 66
}
},
{
node: {
letter: 'c'
value: 67
}
}
]
}
}
```

15 changes: 11 additions & 4 deletions packages/gatsby-transformer-csv/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
{
"name": "gatsby-transformer-csv",
"version": "1.0.1",
"description": "Stub description for gatsby-transformer-csv",
"description": "Gatsby transformer plugin for CSV files",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__"
},
"keywords": [
"gatsby"
"gatsby",
"csv",
"gatsby-plugin"
],
"author": "Kyle Mathews &lt;mathews.kyle@gmail.com&gt;",
"author": "Sonal Saldanha <sonal.saldanha@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1"
"babel-cli": "^6.24.1",
"json2csv":"^3.7"
},
"dependencies": {
"bluebird": "^3.5.0",
"csvtojson": "^1.1"
}
}
Empty file.
Loading