2020-03-29 19:54:28 +08:00
|
|
|
/* global hexo */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const url = require('url');
|
2020-11-24 20:06:08 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const yaml = require('js-yaml');
|
2020-03-29 19:54:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export theme config to js
|
|
|
|
*/
|
2020-04-18 17:33:11 +08:00
|
|
|
hexo.extend.helper.register('export_config', function () {
|
2020-11-24 20:06:08 +08:00
|
|
|
|
|
|
|
let { config, theme } = this;
|
|
|
|
|
|
|
|
// ------ export language to js ------
|
|
|
|
const languageDir = path.join(__dirname, '../../languages');
|
|
|
|
let file = fs.readdirSync(languageDir).find(v => v === `${config.language}.yml`);
|
|
|
|
file = languageDir + '/' + (file ? file : 'en.yml');
|
|
|
|
let languageContent = fs.readFileSync(file, 'utf8');
|
|
|
|
try {
|
|
|
|
languageContent = yaml.safeLoad(languageContent);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
// ---------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let hexo_config = {
|
2020-10-21 19:36:38 +08:00
|
|
|
hostname: url.parse(config.url).hostname || config.url,
|
2020-11-24 20:06:08 +08:00
|
|
|
root: config.root
|
2020-10-21 19:36:38 +08:00
|
|
|
};
|
|
|
|
if (config.search) {
|
2020-11-24 20:06:08 +08:00
|
|
|
hexo_config.path = config.search.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
let theme_config = {
|
|
|
|
toc: theme.toc,
|
|
|
|
left_side_width: theme.left_side_width,
|
|
|
|
local_search: theme.local_search,
|
|
|
|
side_tools: theme.side_tools,
|
|
|
|
version: theme.version,
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-10-21 19:36:38 +08:00
|
|
|
return `<script id="hexo-configurations">
|
2020-11-20 12:02:22 +08:00
|
|
|
let KEEP = window.KEEP || {};
|
2020-11-24 20:06:08 +08:00
|
|
|
KEEP.hexo_config = ${JSON.stringify(hexo_config)};
|
|
|
|
KEEP.theme_config = ${JSON.stringify(theme_config)};
|
|
|
|
KEEP.language = ${JSON.stringify(languageContent)};
|
2020-03-29 19:54:28 +08:00
|
|
|
</script>`;
|
|
|
|
});
|