2019-09-08 12:48:00 +08:00
|
|
|
import flattenDeep from 'lodash/flattenDeep';
|
|
|
|
import flatten from 'lodash/flatten';
|
|
|
|
|
2017-12-22 17:49:38 +08:00
|
|
|
export function getMenuItems(moduleData, locale, categoryOrder, typeOrder) {
|
2016-08-04 10:52:09 +08:00
|
|
|
const menuMeta = moduleData.map(item => item.meta);
|
2017-12-22 17:49:38 +08:00
|
|
|
const menuItems = [];
|
|
|
|
const sortFn = (a, b) => (a.order || 0) - (b.order || 0);
|
2018-12-07 16:17:45 +08:00
|
|
|
menuMeta.sort(sortFn).forEach(meta => {
|
2017-12-22 17:49:38 +08:00
|
|
|
if (!meta.category) {
|
|
|
|
menuItems.push(meta);
|
|
|
|
} else {
|
|
|
|
const category = meta.category[locale] || meta.category;
|
|
|
|
let group = menuItems.filter(i => i.title === category)[0];
|
|
|
|
if (!group) {
|
|
|
|
group = {
|
|
|
|
type: 'category',
|
|
|
|
title: category,
|
|
|
|
children: [],
|
|
|
|
order: categoryOrder[category],
|
|
|
|
};
|
|
|
|
menuItems.push(group);
|
|
|
|
}
|
|
|
|
if (meta.type) {
|
|
|
|
let type = group.children.filter(i => i.title === meta.type)[0];
|
|
|
|
if (!type) {
|
|
|
|
type = {
|
|
|
|
type: 'type',
|
|
|
|
title: meta.type,
|
|
|
|
children: [],
|
|
|
|
order: typeOrder[meta.type],
|
|
|
|
};
|
|
|
|
group.children.push(type);
|
|
|
|
}
|
|
|
|
type.children.push(meta);
|
|
|
|
} else {
|
|
|
|
group.children.push(meta);
|
|
|
|
}
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
|
|
|
});
|
2018-12-07 16:17:45 +08:00
|
|
|
return menuItems
|
|
|
|
.map(i => {
|
|
|
|
if (i.children) {
|
|
|
|
i.children = i.children.sort(sortFn);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
})
|
|
|
|
.sort(sortFn);
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
|
|
|
|
2016-12-09 13:02:16 +08:00
|
|
|
export function isZhCN(pathname) {
|
|
|
|
return /-cn\/?$/.test(pathname);
|
|
|
|
}
|
2016-10-18 12:04:09 +08:00
|
|
|
|
2016-12-09 13:02:16 +08:00
|
|
|
export function getLocalizedPathname(path, zhCN) {
|
|
|
|
const pathname = path.startsWith('/') ? path : `/${path}`;
|
2018-12-07 16:17:45 +08:00
|
|
|
if (!zhCN) {
|
|
|
|
// to enUS
|
2016-12-09 13:02:16 +08:00
|
|
|
return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
|
2018-06-22 21:05:13 +08:00
|
|
|
}
|
|
|
|
if (pathname === '/') {
|
2016-12-09 13:02:16 +08:00
|
|
|
return '/index-cn';
|
2018-06-22 21:05:13 +08:00
|
|
|
}
|
|
|
|
if (pathname.endsWith('/')) {
|
2016-12-09 13:02:16 +08:00
|
|
|
return pathname.replace(/\/$/, '-cn/');
|
|
|
|
}
|
|
|
|
return `${pathname}-cn`;
|
2016-10-18 11:18:25 +08:00
|
|
|
}
|
|
|
|
|
2017-07-10 22:17:52 +08:00
|
|
|
export function ping(callback) {
|
|
|
|
// eslint-disable-next-line
|
2018-12-07 16:17:45 +08:00
|
|
|
const url =
|
|
|
|
'https://private-a' +
|
|
|
|
'lipay' +
|
|
|
|
'objects.alip' +
|
|
|
|
'ay.com/alip' +
|
|
|
|
'ay-rmsdeploy-image/rmsportal/RKuAiriJqrUhyqW.png';
|
2016-05-27 11:48:08 +08:00
|
|
|
const img = new Image();
|
|
|
|
let done;
|
2018-12-07 16:17:45 +08:00
|
|
|
const finish = status => {
|
2016-05-27 11:48:08 +08:00
|
|
|
if (!done) {
|
|
|
|
done = true;
|
|
|
|
img.src = '';
|
|
|
|
callback(status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
img.onload = () => finish('responded');
|
|
|
|
img.onerror = () => finish('error');
|
|
|
|
img.src = url;
|
2016-07-17 15:25:12 +08:00
|
|
|
return setTimeout(() => finish('timeout'), 1500);
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
2017-02-21 13:36:17 +08:00
|
|
|
|
|
|
|
export function isLocalStorageNameSupported() {
|
|
|
|
const testKey = 'test';
|
|
|
|
const storage = window.localStorage;
|
|
|
|
try {
|
|
|
|
storage.setItem(testKey, '1');
|
|
|
|
storage.removeItem(testKey);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 17:31:48 +08:00
|
|
|
|
|
|
|
export function loadScript(src) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.type = 'text/javascript';
|
|
|
|
script.src = src;
|
|
|
|
script.onload = resolve;
|
|
|
|
script.onerror = reject;
|
|
|
|
document.head.appendChild(script);
|
|
|
|
});
|
|
|
|
}
|
2019-09-08 12:48:00 +08:00
|
|
|
|
|
|
|
export function getMetaDescription(jml) {
|
|
|
|
const COMMON_TAGS = ['h1', 'h2', 'h3', 'p', 'img', 'a', 'code', 'strong'];
|
|
|
|
if (!Array.isArray(jml)) return '';
|
|
|
|
const paragraph = flattenDeep(
|
|
|
|
jml
|
|
|
|
.filter(item => {
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
const [tag] = item;
|
|
|
|
return tag === 'p';
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
// ['p', ['code', 'aa'], 'bb'] => ['p', 'aabb']
|
|
|
|
.map(item => {
|
|
|
|
const [tag, ...others] = flatten(item);
|
2019-09-08 15:51:36 +08:00
|
|
|
const content = others
|
|
|
|
.filter(other => typeof other === 'string' && !COMMON_TAGS.includes(other))
|
|
|
|
.join('');
|
2019-09-08 12:48:00 +08:00
|
|
|
return [tag, content];
|
|
|
|
}),
|
|
|
|
).find(p => p && typeof p === 'string' && !COMMON_TAGS.includes(p));
|
|
|
|
return paragraph;
|
|
|
|
}
|