Skip to content

Commit bf6c55f

Browse files
committed
Merge pull request #187 from gatsbyjs/new-file-types
Add support for json/yaml/toml pages
2 parents faba16c + 08eca67 commit bf6c55f

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

lib/isomorphic/create-routes.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,44 +82,40 @@ module.exports = (files, pagesReq) => {
8282
parentRoute.childRoutes.push(route)
8383
})
8484

85-
const markdownWrapper = require('wrappers/md')
86-
const htmlWrapper = require('wrappers/html')
85+
const staticFileTypes = [
86+
'md',
87+
'html',
88+
'json',
89+
'yaml',
90+
'toml',
91+
]
92+
const reactComponentFileTypes = [
93+
'js',
94+
'jsx',
95+
'cjsx',
96+
]
97+
const wrappers = {}
98+
staticFileTypes.forEach((type) => {
99+
try {
100+
wrappers[type] = require(`wrappers/${type}`)
101+
} catch (e) {
102+
// Ignore error
103+
}
104+
})
87105

88106
pages.forEach((p) => {
89107
const page = p
90-
// TODO add ways to load data for other file types.
91-
// Should be able to install a gatsby-toml plugin to add support
92-
// for TOML. Perhaps everything other than JSX and Markdown should be plugins.
93-
// Or even they are plugins but they have built-in 'blessed' plugins.
94108
let handler
95-
switch (page.file.ext) {
96-
case 'md':
97-
handler = markdownWrapper
98-
// TODO figure out if this is redundant as data already added in
99-
// glob-pages.
100-
page.data = pagesReq(`./${page.requirePath}`)
101-
break
102-
case 'html':
103-
handler = htmlWrapper
104-
break
105-
case 'jsx':
106-
handler = pagesReq(`./${page.requirePath}`)
107-
page.data = (() => {
108-
if (pagesReq(`./${page.requirePath}`).metadata) {
109-
return pagesReq(`./${page.requirePath}`).metadata()
110-
}
111-
})()
112-
break
113-
case 'cjsx':
114-
handler = pagesReq(`./${page.requirePath}`)
115-
page.data = (() => {
116-
if (pagesReq(`./${page.requirePath}`).metadata) {
117-
return pagesReq(`./${page.requirePath}`).metadata()
118-
}
119-
})()
120-
break
121-
default:
122-
handler = pagesReq(`./${page.requirePath}`)
109+
if (staticFileTypes.indexOf(page.file.ext) !== -1) {
110+
handler = wrappers[page.file.ext]
111+
page.data = pagesReq(`./${page.requirePath}`)
112+
} else if (reactComponentFileTypes.indexOf(page.file.ext) !== -1) {
113+
handler = pagesReq(`./${page.requirePath}`)
114+
page.data = (() => {
115+
if (pagesReq(`./${page.requirePath}`).metadata) {
116+
return pagesReq(`./${page.requirePath}`).metadata()
117+
}
118+
})()
123119
}
124120

125121
// Determine parent template for page.

lib/loaders/html-loader/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import htmlFrontMatter from 'html-frontmatter'
2+
import objectAssign from 'object-assign'
3+
4+
module.exports = function (content) {
5+
this.cacheable()
6+
const data = objectAssign({}, htmlFrontMatter(content), { body: content })
7+
this.value = data
8+
return `module.exports = ${JSON.stringify(data)}`
9+
}

lib/utils/glob-pages.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ module.exports = (directory, callback) => {
1515

1616
// Make this list easy to modify through the config?
1717
// Or just keep adding extensions...?
18-
const globQuery = `${directory}/pages/**/?(*.coffee|*.cjsx|*.jsx|*.js|*.md|*.html)`
18+
const fileTypesToGlob = [
19+
'coffee',
20+
'cjsx',
21+
'jsx',
22+
'js',
23+
'md',
24+
'html',
25+
'json',
26+
'yaml',
27+
'toml',
28+
]
29+
const fileGlobQuery = fileTypesToGlob.map((type) => `*.${type}`)
30+
const joinedFileQuery = fileGlobQuery.join('|')
31+
const globQuery = `${directory}/pages/**/?(${joinedFileQuery})`
1932
glob(globQuery, null, (err, pages) => {
2033
if (err) { return callback(err) }
2134

@@ -36,6 +49,9 @@ module.exports = (directory, callback) => {
3649
parsed.dirname = slash(parsed.dirname)
3750

3851
// Load data for each file type.
52+
// TODO use webpack-require to ensure data loaded
53+
// here (in node context) is consistent with what's loaded
54+
// in the browser.
3955
let data
4056
if (ext === 'md') {
4157
const rawData = frontMatter(fs.readFileSync(page, 'utf-8'))

lib/utils/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = (program, directory, stage, webpackPort = 1500, routes = []) =>
153153
})
154154
config.loader('html', {
155155
test: /\.html$/,
156-
loader: 'raw',
156+
loader: 'html',
157157
})
158158
config.loader('json', {
159159
test: /\.json$/,

0 commit comments

Comments
 (0)