Skip to content

Commit 8b1cc13

Browse files
committed
支持版本选择
1 parent fa11cb5 commit 8b1cc13

4 files changed

Lines changed: 175 additions & 3 deletions

File tree

docs/en/_templates/versions.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{# 独立版本选择器组件,不依赖模板继承 #}
2+
<div class="version-selector-wrapper">
3+
<select id="version-select" class="version-select">
4+
<option value="/en/latest/" selected>master</option>
5+
<option value="/en/stable/">v1.0.0</option>
6+
</select>
7+
</div>
8+
9+
<script>
10+
document.addEventListener('DOMContentLoaded', function() {
11+
// 获取logo和搜索框元素
12+
const logo = document.querySelector('.wy-side-nav-search .logo');
13+
const searchBox = document.querySelector('.wy-side-nav-search .wy-form');
14+
15+
if (logo && searchBox) {
16+
const selector = document.querySelector('.version-selector-wrapper');
17+
// 在logo之后、搜索框之前插入
18+
searchBox.parentNode.insertBefore(selector, searchBox);
19+
}
20+
21+
// 设置当前选中项
22+
const path = window.location.pathname;
23+
const select = document.getElementById('version-select');
24+
if (path.includes('/master/')) {
25+
select.value = '/en/master/';
26+
} else if (path.includes('/v1.0.0/')) {
27+
select.value = '/en/v1.0.0/';
28+
}
29+
30+
// 切换版本
31+
select.addEventListener('change', function() {
32+
if (this.value) {
33+
window.location.href = this.value;
34+
}
35+
});
36+
});
37+
</script>
38+
39+
<style>
40+
.version-selector-wrapper {
41+
padding: 10px 15px;
42+
background: #f8f8f8;
43+
border-bottom: 1px solid #e7e7e7;
44+
}
45+
46+
.version-select {
47+
width: auto;
48+
min-width: 120px;
49+
max-width: 180px;
50+
margin: 0 auto;
51+
padding: 6px 12px;
52+
border-radius: 4px;
53+
border: 1px solid #d1d1d1;
54+
background: white;
55+
color: #333;
56+
font-size: 13px;
57+
display: block;
58+
}
59+
60+
/* 响应式调整 */
61+
@media screen and (max-width: 768px) {
62+
.version-selector-wrapper {
63+
padding: 8px 10px;
64+
}
65+
}
66+
</style>

docs/en/conf.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import sphinx_rtd_theme
3+
14
# Configuration file for the Sphinx documentation builder.
25
#
36
# For the full list of built-in configuration values, see the documentation:
@@ -29,7 +32,28 @@
2932
'sphinx_sitemap',
3033
]
3134

32-
templates_path = ['_templates']
35+
# 确保主题模板路径优先
36+
templates_path = [
37+
'_templates',
38+
os.path.join(os.path.dirname(sphinx_rtd_theme.__file__), 'layout.html'),
39+
os.path.join(os.path.dirname(sphinx_rtd_theme.__file__), 'includes')
40+
]
41+
html_context = {
42+
# GitHub 仓库设置(必需)
43+
"display_github": True, # 启用 GitHub 链接
44+
"github_user": "tuya", # 组织/用户名
45+
"github_repo": "TuyaOpen", # 仓库名
46+
"github_version": "master", # 默认分支(如 main/master)
47+
48+
# 页面路径配置(自动生成编辑链接)
49+
"conf_py_path": "/docs/en/", # 配置文件的仓库相对路径
50+
51+
# 版本选择器配置
52+
"versions": [
53+
{"name": "master", "url": "/en/master/"},
54+
{"name": "v1.0.0", "url": "/en/v1.0.0/"}
55+
]
56+
}
3357
exclude_patterns = []
3458
source_suffix = ['.rst', '.md']
3559

docs/zh/_templates/versions.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{# 独立版本选择器组件,不依赖模板继承 #}
2+
<div class="version-selector-wrapper">
3+
<select id="version-select" class="version-select">
4+
<option value="/en/latest/" selected>master</option>
5+
<option value="/en/stable/">release</option>
6+
</select>
7+
</div>
8+
9+
<script>
10+
document.addEventListener('DOMContentLoaded', function() {
11+
// 获取logo和搜索框元素
12+
const logo = document.querySelector('.wy-side-nav-search .logo');
13+
const searchBox = document.querySelector('.wy-side-nav-search .wy-form');
14+
15+
if (logo && searchBox) {
16+
const selector = document.querySelector('.version-selector-wrapper');
17+
// 在logo之后、搜索框之前插入
18+
searchBox.parentNode.insertBefore(selector, searchBox);
19+
}
20+
21+
// 设置当前选中项
22+
const path = window.location.pathname;
23+
const select = document.getElementById('version-select');
24+
if (path.includes('/latest/')) {
25+
select.value = '/en/latest/';
26+
} else if (path.includes('/stable/')) {
27+
select.value = '/en/stable/';
28+
}
29+
30+
// 切换版本
31+
select.addEventListener('change', function() {
32+
if (this.value) {
33+
window.location.href = this.value;
34+
}
35+
});
36+
});
37+
</script>
38+
39+
<style>
40+
.version-selector-wrapper {
41+
padding: 10px 15px;
42+
background: #f8f8f8;
43+
border-bottom: 1px solid #e7e7e7;
44+
}
45+
46+
.version-select {
47+
width: auto;
48+
min-width: 120px;
49+
max-width: 180px;
50+
margin: 0 auto;
51+
padding: 6px 12px;
52+
border-radius: 4px;
53+
border: 1px solid #d1d1d1;
54+
background: white;
55+
color: #333;
56+
font-size: 13px;
57+
display: block;
58+
}
59+
60+
/* 响应式调整 */
61+
@media screen and (max-width: 768px) {
62+
.version-selector-wrapper {
63+
padding: 8px 10px;
64+
}
65+
}
66+
</style>

docs/zh/conf.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@
3030
]
3131

3232
templates_path = ['_templates']
33+
html_context = {
34+
# GitHub 仓库设置(必需)
35+
"display_github": True, # 启用 GitHub 链接
36+
"github_user": "tuya", # 组织/用户名
37+
"github_repo": "TuyaOpen", # 仓库名
38+
"github_version": "master", # 默认分支(如 main/master)
39+
40+
# 页面路径配置(自动生成编辑链接)
41+
"conf_py_path": "/docs/zh/", # 配置文件的仓库相对路径
42+
43+
# 版本选择器配置
44+
"versions": [
45+
{"name": "master", "url": "/zh/latest/"},
46+
{"name": "release", "url": "/zh/stable/"}
47+
]
48+
}
3349
exclude_patterns = []
3450
source_suffix = ['.rst', '.md']
3551

@@ -42,6 +58,7 @@
4258

4359
def setup(app):
4460
app.add_css_file('css/custom.css')
61+
# 版本选择器不需要额外CSS/JS,已在模板中内联
4562

4663
# -- Options for HTML output -------------------------------------------------
4764
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
@@ -81,5 +98,4 @@ def setup(app):
8198
}
8299

83100
highlight_language = 'c' # 默认高亮C语言代码
84-
primary_domain = 'c' # 主文档域为C语言
85-
101+
primary_domain = 'c' # 主文档域为C语言

0 commit comments

Comments
 (0)