Skip to content
This repository was archived by the owner on Sep 15, 2023. It is now read-only.

Commit 7ac50dd

Browse files
authored
Merge pull request #585 from olets/yaml
support yaml data source in html task
2 parents ebeec7d + 2d61470 commit 7ac50dd

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

gulpfile.js/tasks/html.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ if(!TASK_CONFIG.html) return
22

33
const browserSync = require('browser-sync')
44
const data = require('gulp-data')
5+
const fs = require('fs')
56
const gulp = require('gulp')
67
const gulpif = require('gulp-if')
78
const handleErrors = require('../lib/handleErrors')
8-
const projectPath = require('../lib/projectPath')
99
const htmlmin = require('gulp-htmlmin')
1010
const nunjucksRender = require('gulp-nunjucks-render')
11-
const fs = require('fs')
11+
const projectPath = require('../lib/projectPath')
12+
const yaml = require('js-yaml')
1213

1314
const htmlTask = function() {
1415

@@ -21,7 +22,16 @@ const htmlTask = function() {
2122

2223
const dataFunction = TASK_CONFIG.html.dataFunction || function(file) {
2324
const dataPath = projectPath(PATH_CONFIG.src, PATH_CONFIG.html.src, TASK_CONFIG.html.dataFile)
24-
return JSON.parse(fs.readFileSync(dataPath, 'utf8'))
25+
const dataExtension = dataPath.split('.').pop()
26+
let data
27+
28+
if (dataExtension === 'yaml' || dataExtension === 'yml') {
29+
data = yaml.safeLoad(fs.readFileSync(dataPath, 'utf8'))
30+
} else {
31+
data = JSON.parse(fs.readFileSync(dataPath, 'utf8'))
32+
}
33+
34+
return data
2535
}
2636

2737
const nunjucksRenderPath = [ projectPath(PATH_CONFIG.src, PATH_CONFIG.html.src) ]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"gulp-sizereport": "1.2.1",
5555
"gulp-sourcemaps": "^2.6.5",
5656
"gulp-svgstore": "7.0.1",
57+
"js-yaml": "^3.13.1",
5758
"lodash": "^4.17.11",
5859
"merge-stream": "^1.0.1",
5960
"minimist": "^1.2.0",

0 commit comments

Comments
 (0)