21
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gulp-data + json で title, description, keyword を一括管理する

Posted at

ディレクトリ構造は以下の前提での設定。

gulpfile.js
package.json
/dist
/src
 └ /_jade
   └ /_data
     └ setting.json
 └ /aaa
   └ index.html
 └ /bbb
   └ index.html

gulpfile.js
var data        = require('gulp-data');

var DEST = './dist';
var SRC = './src';
var SRC_HTML = ['./src/**/*.jade', '!./src/**/_jade/*.jade', '!./src/**/_*.jade'];

gulp.task('jade', function() {
  return gulp.src(SRC_HTML)
  .pipe(data(function(file) {
    var c, filename;
    if (file.path.length !== 0) {
      c = file.path.split('\\').join('/');
      filename = c.split('src/')[1].replace('.jade', '');
      return setJson(filename);
    }
  }))
  .pipe(jade({
    pretty: true,
    basedir: './src/_jade/',
    doctype: 'html'
  }))
  .pipe(gulp.dest(DEST));
});

// .jade のファイル名を keyに、jsonの項目を各ファイルにセットする
var setJson = function(filename) {
  var setting = require('./src/_jade/_data/setting.json');
  if (setting[filename]) {
    return setting[filename];
  } else {
    return setting.defalut;
  }
};

jade側では変数を #{title}, #{description}, #{keywords} この形で入れておく。

index.jade
doctype html
html(lang="ja")
  head
    meta(charset="UTF-8")
    title #{title}
    meta(name="description" content="#{description}")
    meta(name="keywords" content="#{keywords}")

setting.json にはファイルパスと各値を記述。
こちらに記述がないファイルには default で設定した値が入る。

setting.json
{
"default": { "title":"ページタイトル", "description":"ディスクリプション", "keywords":"キーワード"},
"aaa/index": { "title":"AAA|サイト名", "description":"AAAディスクリプション", "keywords":"AAA,AAA,AAA"},
"bbb/index": { "title":"BBB|サイト名", "description":"BBBディスクリプション", "keywords":"BBB,BBB"}
}
21
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?