Index handling fixup. Locale Index. Deepmerge properties.

This commit is contained in:
Valentin Boettcher 2017-09-21 11:00:39 +02:00
parent 4eeb07df57
commit 2fe05657c7
2 changed files with 18 additions and 21 deletions

View file

@ -1,4 +1,5 @@
'use strict';
const deepmerge = require('deepmerge');
var extname = require('path').extname;
@ -69,7 +70,8 @@ Multilang.prototype.getPlugin = function () {
}
return function (files, ms, done) {
ms.metadata().locales = self.locales;
ms.metadata().locales = self.locales.reduce((locObj, locale) =>
Object.assign(locObj, {[locale]: []}), {});
ms.metadata().defaultLocale = self.default;
for (var file in files) {
@ -82,7 +84,7 @@ Multilang.prototype.getPlugin = function () {
// This lets to have base some generic properties
// applied only in the 'default' locale, e.g.: template
if (base !== file) {
self.merge(files[base], files[file]);
files[file] = deepmerge(files[base], files[file]);
}
} else {
files[file].locale = self.default;
@ -94,32 +96,33 @@ Multilang.prototype.getPlugin = function () {
self.locales.forEach(function (locale) {
if (locale != files[file].locale) {
files[file].altFiles[locale] = files[self.getAltFilename(file, files[file].locale, locale)];
} else {
files[file].altFiles[files[file].locale] = files[file];
}
});
// Bind lang()
files[file].lang = lang.bind(files[file]);
// Ad file to locale index.
ms.metadata().locales[files[file].locale].push(files[file]);
}
// Index handling
// Default locale will go in 'index.html'
// Other index-es in '/:locale/index.html'
for (file in files) {
if (/^index/.test(file)) {
var ext = extname(file);
if (files[file].locale == self.default) {
for (file of Object.keys(files)) {
if (files[file].index) {
var name = file.split('/').pop().replace(/_.*(?=\.)/, '');
if (files[file].locale === self.default) {
files[file].path = '';
files['index'+ ext] = files[file];
files[name] = Object.assign({},files[file]);
} else {
files[file].path = files[file].locale +'/';
files[files[file].locale +'/index'+ ext] = files[file];
files[files[file].locale + '/' + name] = files[file];
}
// Remove old entry
delete files[file];
if(name !== file)
delete files[file];
}
}
@ -127,12 +130,4 @@ Multilang.prototype.getPlugin = function () {
};
};
Multilang.prototype.merge = function (src, dest) {
for (var key in src) {
if (!dest.hasOwnProperty(key)) {
dest[key] = src[key];
}
}
};
module.exports = Multilang;

View file

@ -22,7 +22,9 @@
"bugs": {
"url": "https://github.com/doup/metalsmith-multi-language/issues"
},
"dependencies": {},
"dependencies": {
"deepmerge": "^1.5.1"
},
"devDependencies": {
"chai": "^3.0.0",
"metalsmith": "^1.7.0",