-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (75 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use-strict';
var fs = require('hexo-fs');
var frontMatter = require('hexo-front-matter');
const { load } = require('js-yaml');
const helper = require('./lib/helpers');
function loadConfig() {
if (!hexo.config.hasOwnProperty('i18n_post_generator')) {
hexo.log.info('[i18n post generator]: i18n_post config not found, exiting');
return null;
}
var config = hexo.config.i18n_post_generator;
return config;
}
function postGenerator(locals) {
let config = loadConfig();
let allPosts = locals.posts;
let defaultLang = hexo.config.language;
let identifier = config.identifier || 'slug';
if (Array.isArray(defaultLang)) {
defaultLang = defaultLang[0];
hexo.log.debug(`[i18n post generator]: default language: ${defaultLang}`);
} else if (typeof defaultLang === 'undefined') {
hexo.log.warn('[i18n post generator]: default language not defiend, using `en` as default language');
}
let postDict = helper.getPostDict(allPosts, defaultLang, identifier);
// print postDict
for (let id in postDict) {
let message = `[i18n post generator]: id: ${id}`;
for (let lang in postDict[id]) {
message += `, ${lang}`;
}
hexo.log.info(message);
}
let postList = [];
for (let id in postDict) {
let defaultPath = null;
if (typeof postDict[id][defaultLang] === 'undefined') {
hexo.log.warn(`[i18n post generator]: default language ${defaultLang} not found in post ${id}`);
} else {
defaultPath = postDict[id][defaultLang].path;
}
let message = "";
for (let lang in postDict[id]) {
if (lang === defaultLang) {
postList.push({
path: `${defaultPath}`,
data: postDict[id][lang].content
});
message = `[i18n post generator]: generating post: ${defaultPath}, available languages: ${lang} |`;
continue
}
if (defaultPath === null) {
defaultPath = postDict[id][lang].path;
message = `[i18n post generator]: generating post: ${defaultPath}, available languages: `;
}
message += ` ${lang} |`;
let post = postDict[id][lang];
if (config.group_by === 'post') {
post.path = `${defaultPath}/${lang}`;
} else if (config.group_by === 'lang') {
post.path = `${lang}/${defaultPath}`;
} else {
hexo.log.warn(`[i18n post generator]: unknown group_by option: ${config.group_by}, using default: post`);
post.path = `${defaultPath}/${lang}`;
}
postList.push({
path: `${post.path}`,
data: postDict[id][lang].content
});
}
hexo.log.info(message);
}
return postList;
}
hexo.extend.generator.register('i18n_post_generator', postGenerator);