Skip to content

Commit c627760

Browse files
committed
feat(site): support site search
1 parent dc9482d commit c627760

File tree

9 files changed

+669
-613
lines changed

9 files changed

+669
-613
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
"rollup-plugin-styles": "^3.14.1",
150150
"rollup-plugin-terser": "^7.0.2",
151151
"rollup-plugin-typescript2": "^0.31.2",
152+
"semver": "^7.7.1",
152153
"tdesign-icons-view": "^0.3.4",
153154
"tdesign-site-components": "^0.15.6",
154155
"typescript": "^4.5.3",

site/docs/getting-started.en-US.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: React for Mobile
3+
description: TDesign Mobile React is a UI component library for React and Mobile application.
4+
spline: explain
5+
---
6+
7+
## Installation
8+
9+
### npm
10+
11+
```bash
12+
npm i tdesign-mobile-react
13+
```

site/docs/overview.en-US.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ spline: explain
116116
</a>
117117
</div>
118118
</section>
119+
119120
<h3>Form<em class="tag">14</em></h3>
120121
<section class="image-group">
121122
<div class="image-wrapper">
@@ -158,8 +159,8 @@ spline: explain
158159
<img class="__light__" src="https://tdesign.gtimg.com/site/mobile/doc-form.png" />
159160
<img class="__dark__" src="https://tdesign.gtimg.com/site/mobile/doc-form-dark.png" />
160161
<p class="name">Form</p>
161-
</a> -->
162-
</div>
162+
</a>
163+
</div>-->
163164
<div class="image-wrapper">
164165
<a class="item" href="./components/input-en">
165166
<img class="__light__" src="https://tdesign.gtimg.com/site/mobile/doc-input.png" />

site/plugin-tdoc/md-to-react.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,17 @@ const DEFAULT_TABS = [
164164
{ tab: 'design', name: '指南' },
165165
];
166166

167+
const DEFAULT_EN_TABS = [
168+
{ tab: 'demo', name: 'DEMO' },
169+
{ tab: 'api', name: 'API' },
170+
{ tab: 'design', name: 'Guideline' },
171+
];
172+
167173
// 解析 markdown 内容
168174
function customRender({ source, file, md }) {
169175
let { content, data } = matter(source);
170176
// console.log('data', data);
177+
const isEn = file.endsWith('en-US.md');
171178

172179
// md top data
173180
const pageData = {
@@ -177,15 +184,15 @@ function customRender({ source, file, md }) {
177184
description: '',
178185
isComponent: false,
179186
tdDocHeader: true,
180-
tdDocTabs: DEFAULT_TABS,
187+
tdDocTabs: !isEn ? DEFAULT_TABS : DEFAULT_EN_TABS,
181188
apiFlag: /#+\s*API/i,
182189
docClass: '',
183190
lastUpdated: Math.round(fs.statSync(file).mtimeMs),
184191
...data,
185192
};
186193

187194
// md filename
188-
const reg = file.match(/src\/[\w-]+\/([\w-]+)\.md/);
195+
const reg = file.match(/([\w-]+)\.?([\w-]+)?\.md/);
189196
const componentName = reg && reg[1];
190197

191198
// split md

site/plugin-tdoc/transforms.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,46 @@ let demoCodesImports = {};
99
export default {
1010
before({ source, file }) {
1111
const resourceDir = path.dirname(file);
12-
const reg = file.match(/src\/([\w-]+)\/([\w-]+)\.md/);
13-
const name = reg && reg[1];
12+
const reg = file.match(/([\w-]+)\.?([\w-]+)?\.md/);
13+
const fileName = reg && reg[0];
14+
const componentName = reg && reg[1];
15+
const localeName = reg && reg[2];
1416
demoCodesImports = {};
1517

16-
// 统一换成 iwiki 文档内容
17-
if (name && source.includes(':: BASE_DOC ::')) {
18-
const docPath = path.resolve(__dirname, `../../src/_common/docs/mobile/api/${name}.md`);
19-
if (fs.existsSync(docPath)) {
20-
const baseDoc = fs.readFileSync(docPath, 'utf-8');
21-
source = source.replace(':: BASE_DOC ::', baseDoc);
18+
// 统一换成 common 文档内容
19+
if (fileName && source.includes(':: BASE_DOC ::')) {
20+
const localeDocPath = path.resolve(__dirname, `../../src/_common/docs/mobile/api/${fileName}`);
21+
const defaultDocPath = path.resolve(__dirname, `../../src/_common/docs/mobile/api/${componentName}.md`);
22+
23+
let baseDoc = '';
24+
if (fs.existsSync(localeDocPath)) {
25+
// 优先载入语言版本
26+
baseDoc = fs.readFileSync(localeDocPath, 'utf-8');
27+
} else if (fs.existsSync(defaultDocPath)) {
28+
// 回退中文默认版本
29+
baseDoc = fs.readFileSync(defaultDocPath, 'utf-8');
2230
} else {
23-
console.error(`未找到 ${docPath} 文件`);
31+
console.error(`未找到 ${defaultDocPath} 文件`);
2432
}
33+
34+
source = source.replace(':: BASE_DOC ::', baseDoc);
2535
}
2636

2737
// 替换成对应 demo 文件
2838
source = source.replace(/\{\{\s+(.+)\s+\}\}/g, (demoStr, demoFileName) => {
29-
const tsDemoPath = path.resolve(resourceDir, `./_example/${demoFileName}.tsx`);
30-
if (fs.existsSync(tsDemoPath)) {
31-
return `\n::: demo _example/${demoFileName} ${name}\n:::\n`;
39+
const defaultDemoPath = path.resolve(resourceDir, `./_example/${demoFileName}.tsx`);
40+
const localeDemoPath = path.resolve(resourceDir, `../_example/${demoFileName}/${localeName}.tsx`);
41+
42+
if (fs.existsSync(localeDemoPath)) {
43+
return `\n::: demo _example/.${localeName} ${componentName}\n:::\n`;
3244
}
3345

34-
const demoPath = path.resolve(resourceDir, `./_example/${demoFileName}.jsx`);
35-
if (!fs.existsSync(demoPath)) {
36-
console.log('\x1B[36m%s\x1B[0m', `${name} 组件需要实现 _example/${demoFileName}.tsx 示例!`);
46+
if (!fs.existsSync(defaultDemoPath)) {
47+
console.log('\x1B[36m%s\x1B[0m', `${componentName} 组件需要实现 _example/${demoFileName}.tsx 示例!`);
3748
return '\n<h3>DEMO (🚧建设中)...</h3>';
3849
}
3950

40-
return `\n::: demo _example/${demoFileName} ${name}\n:::\n`;
51+
return `\n::: demo _example/${demoFileName} ${componentName}\n:::\n`;
4152
});
4253

4354
source.replace(/:::\s*demo\s+([\\/.\w-]+)/g, (demoStr, relativeDemoPath) => {
@@ -48,6 +59,7 @@ export default {
4859

4960
return source;
5061
},
62+
5163
render({ source, file, md }) {
5264
const demoCodesDefsStr = Object.keys(demoCodesImports)
5365
.map((key) => demoCodesImports[key])

site/web/App.jsx

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import React, { useEffect, useRef, useState, lazy, Suspense } from 'react';
22
import { BrowserRouter, Routes, Navigate, Route, useLocation, useNavigate, Outlet } from 'react-router-dom';
3+
import semver from 'semver';
34
import siteConfig from './site.config';
45
import { getRoute, filterVersions } from './utils';
56
import packageJson from '@/package.json';
67

78
const LazyDemo = lazy(() => import('./Demo'));
89

9-
const { docs: routerList } = JSON.parse(JSON.stringify(siteConfig).replace(/component:.+/g, ''));
10+
const { docs, enDocs } = JSON.parse(JSON.stringify(siteConfig).replace(/component:.+/g, ''));
11+
12+
const docsMap = {
13+
zh: docs,
14+
en: enDocs,
15+
};
1016

1117
const registryUrl =
1218
'https://service-edbzjd6y-1257786608.hk.apigw.tencentcs.com/release/npm/versions/tdesign-mobile-react';
1319
const currentVersion = packageJson.version.replace(/\./g, '_');
1420

15-
const docRoutes = getRoute(siteConfig.docs, []);
21+
// eslint-disable-next-line import/no-named-as-default-member
22+
const docRoutes = [...getRoute(siteConfig.docs, []), ...getRoute(siteConfig.enDocs, [])];
1623
const renderRouter = docRoutes.map((nav, i) => {
1724
const LazyCom = lazy(nav.component);
1825

@@ -36,37 +43,36 @@ function Components() {
3643
const tdHeaderRef = useRef();
3744
const tdDocAsideRef = useRef();
3845
const tdDocContentRef = useRef();
46+
const tdSelectRef = useRef();
3947
const tdDocSearch = useRef();
4048

41-
const [versionOptions, setVersionOptions] = useState([]);
4249
const [version] = useState(currentVersion);
4350

44-
function changeVersion(version) {
45-
if (version === currentVersion) return;
46-
const historyUrl = `//${version}-tdesign-mobile-react.surge.sh`;
47-
window.open(historyUrl, '_blank');
48-
}
49-
5051
function initHistoryVersions() {
5152
fetch(registryUrl)
5253
.then((res) => res.json())
5354
.then((res) => {
5455
const options = [];
55-
const versions = filterVersions(Object.keys(res.versions).filter((v) => !v.includes('alpha')));
56+
const versions = filterVersions(Object.keys(res.versions));
5657

5758
versions.forEach((v) => {
5859
const nums = v.split('.');
60+
if (nums[0] === '0' && nums[1] < 21) return false;
5961

6062
options.unshift({ label: v, value: v.replace(/\./g, '_') });
6163
});
62-
setVersionOptions(options);
64+
65+
tdSelectRef.current.options = options.sort((a, b) => (semver.gt(a.label, b.label) ? -1 : 1));
6366
});
6467
}
6568

6669
useEffect(() => {
6770
tdHeaderRef.current.framework = 'react';
68-
// tdDocSearch.current.docsearchInfo = { indexName: 'tdesign_doc_mobile_react' };
69-
tdDocAsideRef.current.routerList = routerList;
71+
tdDocSearch.current.docsearchInfo = { indexName: 'tdesign_doc_react_mobile' };
72+
73+
const isEn = window.location.pathname.endsWith('en');
74+
75+
tdDocAsideRef.current.routerList = isEn ? docsMap.en : docsMap.zh;
7076
tdDocAsideRef.current.onchange = ({ detail }) => {
7177
if (location.pathname === detail) return;
7278
tdDocContentRef.current.pageStatus = 'hidden';
@@ -83,20 +89,25 @@ function Components() {
8389
});
8490
};
8591

86-
// initHistoryVersions();
92+
tdSelectRef.current.onchange = ({ detail }) => {
93+
const { value: version } = detail;
94+
if (version === currentVersion) return;
95+
96+
const historyUrl = `https://${version}-tdesign-mobile-react.surge.sh`;
97+
window.open(historyUrl, '_blank');
98+
tdSelectRef.current.value = currentVersion;
99+
};
100+
101+
initHistoryVersions();
87102
}, [location, navigate]);
88103

89104
return (
90105
<td-doc-layout>
91106
<td-header ref={tdHeaderRef} slot="header" platform="mobile">
92-
{/* <td-doc-search slot="search" ref={tdDocSearch} /> */}
107+
<td-doc-search slot="search" ref={tdDocSearch} />
93108
</td-header>
94109
<td-doc-aside ref={tdDocAsideRef} title="React for Mobile">
95-
{/* {versionOptions.length ? (
96-
<div slot="extra">
97-
<Select popupProps={{ zIndex: 800 }} value={version} options={versionOptions} onChange={changeVersion} />
98-
</div>
99-
) : null} */}
110+
<td-select ref={tdSelectRef} value={version} slot="extra"></td-select>
100111
</td-doc-aside>
101112

102113
<td-doc-content ref={tdDocContentRef} platform="mobile">
@@ -111,8 +122,8 @@ function App() {
111122
return (
112123
<BrowserRouter>
113124
<Routes>
114-
<Route exact path="/" element={<Navigate replace to="/mobile-react/getting-started" />} />
115-
<Route exact path="/mobile-react" element={<Navigate replace to="/mobile-react/getting-started" />} />
125+
<Route exact path="/" element={<Navigate replace to="/mobile-react/overview" />} />
126+
<Route exact path="/mobile-react" element={<Navigate replace to="/mobile-react/overview" />} />
116127
<Route
117128
path="/mobile-react/demos/*"
118129
element={
@@ -124,7 +135,7 @@ function App() {
124135
<Route path="/mobile-react/*" element={<Components />}>
125136
{renderRouter}
126137
</Route>
127-
<Route path="*" element={<Navigate replace to="/mobile-react/getting-started" />} />
138+
<Route path="*" element={<Navigate replace to="/mobile-react/overview" />} />
128139
</Routes>
129140
</BrowserRouter>
130141
);

site/web/main.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3+
import { registerLocaleChange } from 'tdesign-site-components';
34
import App from './App';
45

56
import '../style/web/index.less';
67

7-
import 'tdesign-site-components';
88
import 'tdesign-site-components/lib/styles/style.css';
99
import 'tdesign-site-components/lib/styles/prism-theme.less';
1010
import 'tdesign-site-components/lib/styles/prism-theme-dark.less';
1111

1212
// import icons webcomponents
1313
import 'tdesign-icons-view';
1414

15+
registerLocaleChange();
1516
ReactDOM.render(
1617
<React.StrictMode>
1718
<App />

0 commit comments

Comments
 (0)