2020-03-29 19:54:28 +08:00
|
|
|
/* global hexo */
|
2022-09-29 21:59:40 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
'use strict'
|
2020-03-29 19:54:28 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
const url = require('url')
|
|
|
|
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 () {
|
2022-09-30 11:40:30 +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 ------------------------
|
2022-09-30 11:40:30 +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')
|
2020-11-24 20:06:08 +08:00
|
|
|
try {
|
2022-09-30 11:40:30 +08:00
|
|
|
languageContent = yaml.load(languageContent)
|
2022-09-29 21:59:40 +08:00
|
|
|
} catch (err) {
|
2022-09-30 11:40:30 +08:00
|
|
|
console.log(err)
|
2020-11-24 20:06:08 +08:00
|
|
|
}
|
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
|
2022-09-30 11:40:30 +08:00
|
|
|
}
|
2020-12-30 18:11:46 +08:00
|
|
|
|
2020-10-21 19:36:38 +08:00
|
|
|
if (config.search) {
|
2022-09-30 11:40:30 +08:00
|
|
|
hexo_config.path = config.search.path
|
2020-11-24 20:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-29 21:59:40 +08:00
|
|
|
version: require('../../package.json').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'])};
|
2022-09-30 11:40:30 +08:00
|
|
|
</script>`
|
|
|
|
})
|