Skip to content
Permalink
Browse files

auto convert lang code to improve compatibility

  • Loading branch information...
ikeq committed Jun 28, 2019
1 parent c6984ae commit 7d1411d2f0b8654075c4681d46a39b745c28f6b3
@@ -15,10 +15,8 @@ assignees: ''
### 不好的 Title 包括但不限于以下形式:
- 提一个问题
- 添加插件
- 你好,请问能……吗?
- 主题很赞,遇到了一些小问题
- 发现一个自定义 URL 生成问题
以上形式的 Issue,不保证回复,也可能被直接 delete ~
@@ -29,7 +27,13 @@ assignees: ''
- 代码块使用``` ```时样式可能会出现错误
- 无法自定义 URL
Title 最好一句话描述,不要出现逗号。
### 关于 Issue:
请务必描述清楚你的问题,最好能够提供以下信息:
- 主题版本
- 配置
- markdown 代码(如果有关)
最后:
@@ -1,7 +1,7 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 30
daysUntilStale: 10
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
daysUntilClose: 1
# Issues with these labels will never be considered stale
exemptLabels:
- bug
@@ -63,7 +63,7 @@ permalink: post/:title/
theme: inside
```

3\. See [inside/_config.yml](_config.yml) for theme configuration.
3\. Read [documentation] to configure the theme.

## Browser Support

@@ -91,3 +91,4 @@ Licensed under [MIT](LICENSE).
[livere]: https://livere.com
[releases]: https://github.com/ike-c/hexo-theme-inside/releases
[travis]: https://travis-ci.org/ike-c/hexo-theme-inside
[documentation]: https://blog.oniuo.com/theme-inside
@@ -58,13 +58,11 @@
2\. 配置 `HEXO/_config.yml` 如下:

```yml
language: zh-Hans
# language: zh-Hant
permalink: post/:title/
theme: inside
```

3\. 主题配置见 [inside/_config.yml](_config.yml),详细说明见[这里](https://blog.oniuo.com/post/inside-theme-configuration)
3\. 主题配置请参阅 [documentation]

## 浏览器支持

@@ -92,3 +90,4 @@ Licensed under [MIT](LICENSE).
[livere]: https://livere.com
[releases]: https://github.com/ike-c/hexo-theme-inside/releases
[travis]: https://travis-ci.org/ike-c/hexo-theme-inside
[documentation]: https://blog.oniuo.com/theme-inside
@@ -0,0 +1,51 @@
menu:
archives: Archives
categories: Categories
tags: Tags

title:
archives: Archive
categories: Category
tags: Tag
search: Search

footer:
powered: Powered by %s
theme: Theme

reward:
paypal: Paypal
wechat: WeChat
alipay:
bitcoin: Bitcoin

comments:
load: Load %s
load_faild: Faild to load %s, click to retry
livere: Livere
disqus: Disqus

post:
copyright:
author: Author
link: Link
license: Copyright
published: Published
updated: Updated

page:
tags:
zero: No tags
one: 1 tag in total
other: "%d tags in total"

notfound:
direct_failed: Failed to direct to %s
empty: Nothing here.

search:
placeholder: Search
hits:
zero: No result found for ":query"
one: 1 result found in :time ms
other: ":hits results found in :time ms"
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="<%= config.language || 'en' %>">
<html lang="<%= config.language %>">
<head>
<meta charset="utf-8">
<title><%= title %></title>
@@ -13,18 +13,18 @@
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<% } -%>
<%- open_graph({ image: page.thumbnail || theme.profile.avatar }) %>
<% if (theme.seo.structured_data) { %><%- structured_data(page) %><% } -%>
<% if (theme.seo.structured_data) { %><%- structured_data(page) %>
<% } -%>
<% if (config.feed && config.feed.path) { -%>
<link rel="alternate" type="application/atom+xml" title="<%= config.title %>" href="<%= url_for(config.feed.path) %>">
<% } -%>
<% if (theme.appearance.font && theme.appearance.font.url) { -%><%- css(theme.appearance.font.url) -%><% } %>
<%- theme.styles %>
<% if (theme.runtime.themeHash) { %><%- css('theme.' + theme.runtime.themeHash) %><% } %>
<%- css('theme.' + theme.runtime.themeHash) %>
<%- js('config.' + theme.runtime.configHash) %>
<% if (theme.ga) { %><%- ga(theme.ga) %><% } -%>
</head>
<body lang="<%= config.language || 'en' %>">
<body lang="<%= config.language %>">
<is-d2a57d></is-d2a57d>
<%- theme.scripts -%>
<%- theme.scripts %>
</body>
</html>
@@ -5,12 +5,12 @@ const utils = require('./utils');
const pkg = require('../package.json');
const configSchema = require('./configSchema.json');
const resources = require('../source/_resources.json');
const external_regex = /^(\w+:)?\/\/\w+\.\w+/;

module.exports = function (hexo) {
hexo.on('generateBefore', function () {
const site = hexo.config;
const theme = hexo.theme.config;
const __ = this.theme.i18n.__();
const email = theme.profile && theme.profile.email || site.email || '';
const feed = site.feed ? urlFor.call(this, site.feed.path) : '';
const result = utils.parseConfig(configSchema, theme, {
@@ -21,7 +21,12 @@ module.exports = function (hexo) {
$title: site.title,
$description: site.description
});
const urlFn = result.static_prefix ? a => `${result.static_prefix}/${a}` : urlFor.bind(this);
const urlFn = result.static_prefix ?
a => external_regex.test(a) ? a : `${result.static_prefix}/${a}` :
urlFor.bind(this);

site.language = utils.localeId(site.language);
const __ = this.theme.i18n.__(site.language);

if (!result.data_prefix) result.data_prefix = result.data_dir;

@@ -43,11 +48,16 @@ module.exports = function (hexo) {
}

result.plugins = [
// plugins comes first to ensure that their libs is ready when executing dynamic code.
...(result.plugins || []),
...resources.styles,
...resources.scripts,
resources.locales[site.languages || 'en'],
...(result.plugins || [])
resources.locales[site.language]
];

if (theme.appearance.font && theme.appearance.font.url)
result.plugins.unshift({ tag: 'link', href: theme.appearance.font.url });

{
const plugins = { $t: [] };
const scripts = [];
@@ -72,7 +82,6 @@ module.exports = function (hexo) {
const tag = plugin.split('?')[0].endsWith('.css') ? 'link' : 'script';
plugin = tag === 'link' ? { tag, href: plugin } : { tag, src: plugin }
}
if (plugin.tag === 'link' && !plugin.rel) plugin.rel = 'stylesheet';
if (plugin.src) plugin.src = urlFn(plugin.src);
if (plugin.href) plugin.href = urlFn(plugin.href);

@@ -24,7 +24,7 @@ module.exports = function (data) {
if (copyright === undefined)
copyright = theme.copyright;
if (dateHelper === undefined)
dateHelper = date.bind({ page: { lang: localeId(config.language) }, config })
dateHelper = date.bind({ page: { lang: localeId(config.language, true) }, config })
if (uriReplacer === undefined)
uriReplacer = theme.assets ?
(() => {
@@ -1,17 +1,23 @@
let locale, configTitle, titleFn;

module.exports = function (locals) {
const __ = this.theme.i18n.__(),
{ page, config } = locals,
if (titleFn === undefined) {
locale = this.theme.i18n.get(locals.config.language);
configTitle = locals.config.title;
titleFn = {
archives: p => __('title.archives'),
categories: p => __('title.categories') + (p.name ? ` : ${p.name}` : ''),
tags: p => __('title.tags') + (p.name ? ` : ${p.name}` : ''),
archives: p => locale['title.archives'],
categories: p => locale['title.categories'] + (p.name ? ` : ${p.name}` : ''),
tags: p => locale['title.tags'] + (p.name ? ` : ${p.name}` : ''),
page: p => p.title,
posts: p => p.title,
post: p => p.title,
},
title = titleFn[page.type] ? titleFn[page.type](page) : '';
};
}

const { page } = locals;
const title = titleFn[page.type] ? titleFn[page.type](page) : '';

locals.title = title ? `${title} - ${config.title}` : config.title;
locals.title = title ? `${title} - ${configTitle}` : configTitle;

return locals;
}
@@ -14,7 +14,7 @@ module.exports = function (locals) {
},
color: theme.appearance.accent_color,
hash: theme.runtime.hash,
locale: this.theme.i18n.get()
locale: this.theme.i18n.get(site.language)
}
);

@@ -4,7 +4,7 @@ module.exports = function (id) {
if (typeof cache === 'undefined') {
cache = id ? [
`<script async src="//www.googletagmanager.com/gtag/js?id=${id}"></script>`,
`<script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments)};gtag('js', new Date());gtag('config', '${id}');</script>`
`<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)};gtag('js',new Date());gtag('config','${id}');</script>`
].join('\n') : '';
}

@@ -8,7 +8,7 @@ let org = null;
module.exports = function (page) {
const config = this.config;
const theme = this.theme;
const lang = config.language || 'en';
const lang = config.language;
const datum = [];

if (!avatar) avatar = url.resolve(config.url, this.url_for(theme.profile.avatar));
@@ -1,7 +1,8 @@
const { localeId } = require('../utils');
const Pattern = require('hexo-util').Pattern;

module.exports = function (hexo) {
const lang = hexo.config.language || 'en';
const lang = localeId(hexo.config.language);

return {
process: function (file) {
@@ -74,27 +74,6 @@ exports.getPagePath = function (source) {
return paths;
}

/**
* Get file name without hash suffixed
*
* @param {string[]} dir
* @param {string} ext
* @param {string[]} names
* @returns {string[]}
*/
exports.getAssetsName = function (dir, ext, names) {
const out = [];

names.forEach(name => {
dir.forEach(filename => {
new RegExp(`^${name}.*\.${ext}$`).test(filename) && out.push(filename);
})
});

return out;
}


exports.Pagination = class {
/**
* @param {any} config
@@ -554,13 +533,31 @@ const localeMap = {
'zh-cn': 'zh-Hans',
'zh-hk': 'zh-Hant',
'zh-tw': 'zh-Hant',
'en': 'en',
'ja': 'ja',
};
const oldLocaleMap = {
'zh-Hans': 'zh-cn',
'zh-Hant': 'zh-hk'
'zh-Hant': 'zh-hk',
'en': 'en',
'ja': 'ja',
};
exports.localeId = function (id, toNew) {
return (toNew ? localeMap : oldLocaleMap)[id] || id;

/**
* Convert language code to ISO 639-1
*
* @param {string | string[]} ids
* @param {boolean} toOld reverse
* @returns {string}
*/
exports.localeId = function (ids, toOld) {
const id = ids ? typeof ids === 'string' ? ids : ids[0] : '';
const lowerCasedId = id ? id.toLowerCase() : '';

if (toOld)
return localeMap[lowerCasedId] !== undefined ? id : oldLocaleMap[id] || oldLocaleMap.en;

return oldLocaleMap[id] !== undefined ? id : localeMap[lowerCasedId] || localeMap.en;
}

function jsParser() {
@@ -643,16 +640,34 @@ exports.published = function (p) {
* @returns {string}
*/
exports.tag = function (vTag) {
const { tag, code, ...attrs } = vTag;
let { tag, code, ...attrs } = vTag;

if (code) return `<${tag}>${code}</${tag}>`;
if (tag === 'link') {
if (!attrs.href) return '';
if (!attrs.rel) attrs.rel = 'stylesheet';
}
else if (tag === 'style') {
if (!code) return '';
}
else if (tag === 'script') {
if (attrs.src) code = '';
else if (code) {
if (attrs.type === undefined) {
code = exports.parseJs(`(function(){${code}})();`);
if (!code) return '';
}
}
else return '';
}

let attrString = '';
for (let k in attrs) {
const v = attrs[k]
if (v === true) attrString += ' ' + k;
else attrString += ` ${k}="${v}"`;
}
if (tag === 'script') return `<script${attrString}></script>`;

if (tag === 'link') return `<link${attrString}>`;

return `<${tag + attrString}>${code || ''}</${tag}>`;
}
@@ -26,8 +26,8 @@
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"hexo": "^3.8.0",
"jasmine": "^3.3.1",
"uglify-js": "^3.4.9"
"hexo": "^3.9.0",
"jasmine": "^3.4.0",
"uglify-js": "^3.6.0"
}
}

0 comments on commit 7d1411d

Please sign in to comment.
You can’t perform that action at this time.