ant-design/site/theme/template/utils.tsx
二货机器人 afedb4127f
docs: Site with single paging ()
* patch route

* use data for promise

* rename Article file

* Article in TS

* clean up

* use collect

* adjust css

* card it

* listing

* res

* hiring

* add footer

* english it

* rm others

* fix lint

* fix navigation

* add dropdown button

* Header with TS

* split input & logo

* navigation out

* use flex

* flex everything

* stretch

* hide search when narrow

* init category

* global rules

* more cat

* pages

* level 2 width

* level 3

* level 4

* skip before ready

* clean up

* unqiue shadow variable

* update eslintignore

* update ignore

* fix ssr

* fix location param

* replace images

* shake more

* fix top of navigation

* remove home

* reorder nav & adjust padding style

* narrow res style

* Update research-form-page.zh-CN.md ()

Co-authored-by: Golevka <huangsamfisher@163.com>

* rm skip

* narrow merge in

* reset p style

* update svg

* fix lint

* update ignore

* update lint ignore

* Update research-result-page.zh-CN.md ()

Co-authored-by: mihaideyu <zxy1010414746@163.com>

* Update research-result-page.zh-CN.md

* Update research-workbench.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update research-workbench.zh-CN.md

* Update research-list-page.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update research-list-page.zh-CN.md

* padding use 40px

* add responsive of ref

* fix p margin

* Update research-workbench.zh-CN.md

* Update buttons.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update resources.en-US.md ()

* Update research-navigation.zh-CN.md ()

* Update research-navigation.zh-CN.md

* Update research-navigation.zh-CN.md

* Update research-navigation.zh-CN.md

* Update research-empty.zh-CN.md ()

* Update research-empty.zh-CN.md

* Update research-empty.zh-CN.md

* Update research-empty.zh-CN.md

* Update research-empty.zh-CN.md

Co-authored-by: 二货机器人 <smith3816@gmail.com>

* Update research-exception-page.zh-CN.md ()

* Update research-exception-page.zh-CN.md

* Update research-exception-page.zh-CN.md

* Update research-message-and-feedback.zh-CN.md ()

* Update research-message-and-feedback.zh-CN.md ()

* Update research-message-and-feedback.zh-CN.md

* Update research-message-and-feedback.zh-CN.md

* Update research-message-and-feedback.zh-CN.md

* Update research-message-and-feedback.zh-CN.md

* replace background

* fix lines margin

* home padding

* patch minTilda doc

* Update resources.zh-CN.md ()

* clean up

* Update research-workbench.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update research-overview.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update research-result-page.zh-CN.md ()

Co-authored-by: mihaideyu <zxy1010414746@163.com>

* Update research-list-page.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* fix doc

* Update research-message-and-feedback.zh-CN.md ()

* adjust home nav width

* Update research-message-and-feedback.zh-CN.md ()

* Update research-form-page.zh-CN.md ()

Co-authored-by: Golevka <huangsamfisher@163.com>

* patch file

* clean up

* update shadow.zh-CN

update shadow.zh-CN

* add shadow table

* Update visual.zh-CN.md ()

Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com>

* Update docs/spec/shadow.zh-CN.md

Co-Authored-By: 偏右 <afc163@gmail.com>

* Update docs/spec/shadow.zh-CN.md

Co-Authored-By: 偏右 <afc163@gmail.com>

* Update docs/spec/shadow.zh-CN.md

Co-Authored-By: 偏右 <afc163@gmail.com>

* Update docs/spec/shadow.zh-CN.md

Co-Authored-By: 偏右 <afc163@gmail.com>

* Update docs/spec/shadow.zh-CN.md

Co-Authored-By: 偏右 <afc163@gmail.com>

* patch docs

* fix doc

* rm page suffix

* fix style

* mobile it

* Update resources.en-US.md ()

* chart res

* add qr code

* adjust qr

* Update visual.zh-CN.md ()

Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com>

* fix site qr

* mobile height of it

* book update

* 3.x site link

* isMobile is new cnotext

* Update research-form.zh-CN.md ()

Co-authored-by: Golevka <huangsamfisher@163.com>

* adjust form doc

* mobile use link

* adjust order

* flush

* fix ts

* Update research-navigation.zh-CN.md ()

* Update research-navigation.zh-CN.md ()

* rm useless lines

* add mobile icon

* Update research-exception.zh-CN.md ()

* Update research-empty.zh-CN.md ()

* Update research-workbench.zh-CN.md

* Update visual.zh-CN.md

* adjust mobile icon

* Update research-message-and-feedback.zh-CN.md ()

* Update research-list.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update research-workbench.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update visual.zh-CN.md ()

Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com>

* Update research-result.zh-CN.md ()

Co-authored-by: mihaideyu <zxy1010414746@163.com>

* Update research-form.zh-CN.md ()

* Update research-message-and-feedback.zh-CN.md ()

* Update research-list.zh-CN.md ()

Co-authored-by: yingxirz <inseeing@gmail.com>

* Update research-result.zh-CN.md ()

Co-authored-by: mihaideyu <zxy1010414746@163.com>

* update ref color

* update zip file

* fix mobile icon

* rm skip dark

* fix cat

* update zip link

* update zip link

Co-authored-by: Golevka <huangsamfisher@163.com>
Co-authored-by: mihaideyu <zxy1010414746@163.com>
Co-authored-by: yingxirz <inseeing@gmail.com>
Co-authored-by: 偏右 <afc163@gmail.com>
Co-authored-by: myeunhyuk <39618466+myeunhyuk@users.noreply.github.com>
Co-authored-by: minTilda <31951290+minTilda@users.noreply.github.com>
Co-authored-by: ziyuan-174774 <61226089+ziyuan-174774@users.noreply.github.com>
Co-authored-by: 竹尔 <54707870+AntDesigners@users.noreply.github.com>
Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com>
2020-02-27 10:53:30 +08:00

203 lines
4.7 KiB
TypeScript

import flattenDeep from 'lodash/flattenDeep';
import flatten from 'lodash/flatten';
interface Meta {
skip?: boolean;
category?: any;
type?: any;
title?: any;
order?: number;
children?: Meta[];
}
interface ModuleDataItem {
meta: Meta;
}
interface Orders {
[key: string]: number;
}
export function getMenuItems(
moduleData: ModuleDataItem[],
locale: string,
categoryOrder: Orders,
typeOrder: Orders,
) {
const menuMeta = moduleData.map(item => item.meta).filter(meta => !meta.skip);
const menuItems: Meta[] = [];
const sortFn = (a: Meta, b: Meta) => (a.order || 0) - (b.order || 0);
menuMeta.sort(sortFn).forEach(meta => {
// Format
if (meta.category) {
meta.category = meta.category[locale] || meta.category;
}
if (meta.type) {
meta.type = meta.type[locale] || meta.type;
}
if (meta.title) {
meta.title = meta.title[locale] || meta.title;
}
if (!meta.category) {
menuItems.push(meta);
return;
}
// Component
if (meta.category === 'Components' && meta.type) {
let type = menuItems.find(i => i.title === meta.type);
if (!type) {
type = {
type: 'type',
title: meta.type,
children: [],
order: typeOrder[meta.type],
};
menuItems.push(type);
}
type.children = type.children || [];
type.children.push(meta);
return;
}
let group = menuItems.find(i => i.title === meta.category);
if (!group) {
group = {
type: 'category',
title: meta.category,
children: [],
order: categoryOrder[meta.category],
};
menuItems.push(group);
}
group.children = group.children || [];
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 = type.children || [];
type.children.push(meta);
} else {
group.children.push(meta);
}
});
function nestSort(list: Meta[]): Meta[] {
return list.sort(sortFn).map(item => {
if (item.children) {
return {
...item,
children: nestSort(item.children),
};
}
return item;
});
}
return nestSort(menuItems);
}
export function isZhCN(pathname: string) {
return /-cn\/?$/.test(pathname);
}
export function getLocalizedPathname(path: string, zhCN?: boolean) {
const pathname = path.startsWith('/') ? path : `/${path}`;
if (!zhCN) {
// to enUS
return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
}
if (pathname === '/') {
return '/index-cn';
}
if (pathname.endsWith('/')) {
return pathname.replace(/\/$/, '-cn/');
}
return `${pathname}-cn`;
}
export function ping(callback: (status: string) => void) {
// eslint-disable-next-line
const url =
'https://private-a' +
'lipay' +
'objects.alip' +
'ay.com/alip' +
'ay-rmsdeploy-image/rmsportal/RKuAiriJqrUhyqW.png';
const img = new Image();
let done: boolean;
const finish = (status: string) => {
if (!done) {
done = true;
img.src = '';
callback(status);
}
};
img.onload = () => finish('responded');
img.onerror = () => finish('error');
img.src = url;
return setTimeout(() => finish('timeout'), 1500);
}
export function isLocalStorageNameSupported() {
const testKey = 'test';
const storage = window.localStorage;
try {
storage.setItem(testKey, '1');
storage.removeItem(testKey);
return true;
} catch (error) {
return false;
}
}
export function loadScript(src: string) {
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);
});
}
export function getMetaDescription(jml?: any[] | null) {
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);
const content = others
.filter(other => typeof other === 'string' && !COMMON_TAGS.includes(other))
.join('');
return [tag, content];
}),
).find(p => p && typeof p === 'string' && !COMMON_TAGS.includes(p));
return paragraph;
}