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
|
|
|
|
2022-09-27 17:52:45 +08:00
|
|
|
const { config, theme } = this;
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2021-01-07 15:15:39 +08:00
|
|
|
// ------------------------ export language to js ------------------------
|
2020-11-24 20:06:08 +08:00
|
|
|
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 {
|
2021-02-24 21:34:54 +08:00
|
|
|
languageContent = yaml.load(languageContent);
|
2020-11-24 20:06:08 +08:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2021-01-07 15:15:39 +08:00
|
|
|
// -----------------------------------------------------------------------
|
2020-11-24 20:06:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
let hexo_config = {
|
2020-10-21 19:36:38 +08:00
|
|
|
hostname: url.parse(config.url).hostname || config.url,
|
2021-01-15 16:57:20 +08:00
|
|
|
root: config.root,
|
|
|
|
language: config.language
|
2020-10-21 19:36:38 +08:00
|
|
|
};
|
2020-12-30 18:11:46 +08:00
|
|
|
|
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 = {
|
2022-09-28 23:53:03 +08:00
|
|
|
toc: theme.toc || {},
|
|
|
|
style: theme.style || {},
|
|
|
|
local_search: theme.local_search || {},
|
|
|
|
code_copy: theme.code_copy || {},
|
|
|
|
code_block_tools: theme.code_block_tools || {},
|
|
|
|
side_tools: theme.side_tools || {},
|
|
|
|
pjax: theme.pjax || {},
|
|
|
|
lazyload: theme.lazyload || {},
|
2022-09-27 17:52:45 +08:00
|
|
|
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)};
|
2020-12-30 18:11:46 +08:00
|
|
|
KEEP.language_ago = ${JSON.stringify(languageContent['ago'])};
|
2022-09-29 16:42:09 +08:00
|
|
|
KEEP.language_code_block = ${JSON.stringify(languageContent['code_block'])};
|
2020-03-29 19:54:28 +08:00
|
|
|
</script>`;
|
|
|
|
});
|