index.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello-Agents</title>
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  7. <meta name="description" content="Description">
  8. <meta name="viewport"
  9. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  10. <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@latest/lib/themes/vue.css">
  11. <style>
  12. /* --- 1. 原有样式 (保留) --- */
  13. /* 语言切换按钮样式 */
  14. .lang-switch {
  15. position: fixed;
  16. top: 20px;
  17. right: 80px;
  18. z-index: 999;
  19. }
  20. .lang-switch button {
  21. background: #42b983;
  22. color: white;
  23. border: none;
  24. padding: 8px 16px;
  25. border-radius: 4px;
  26. cursor: pointer;
  27. font-size: 14px;
  28. transition: background 0.3s;
  29. }
  30. .lang-switch button:hover {
  31. background: #33a06f;
  32. }
  33. /* Giscus 评论区样式 */
  34. .giscus-container {
  35. margin-top: 60px;
  36. padding-top: 40px;
  37. border-top: 1px solid #eee;
  38. }
  39. /* 可折叠评论区标题 */
  40. .giscus-toggle {
  41. display: flex;
  42. align-items: center;
  43. justify-content: space-between;
  44. cursor: pointer;
  45. padding: 15px 20px;
  46. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  47. border-radius: 8px;
  48. margin-bottom: 20px;
  49. transition: all 0.3s ease;
  50. box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);
  51. }
  52. .giscus-toggle:hover {
  53. transform: translateY(-2px);
  54. box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  55. }
  56. .giscus-toggle-title {
  57. font-size: 1.3em;
  58. font-weight: 600;
  59. color: white;
  60. margin: 0;
  61. display: flex;
  62. align-items: center;
  63. gap: 10px;
  64. }
  65. .giscus-toggle-icon {
  66. font-size: 1.2em;
  67. transition: transform 0.3s ease;
  68. color: white;
  69. }
  70. .giscus-toggle-icon.expanded {
  71. transform: rotate(180deg);
  72. }
  73. .giscus-content {
  74. max-height: 0;
  75. overflow: hidden;
  76. transition: max-height 0.4s ease-out, opacity 0.3s ease;
  77. opacity: 0;
  78. }
  79. .giscus-content.expanded {
  80. max-height: 2000px;
  81. opacity: 1;
  82. transition: max-height 0.5s ease-in, opacity 0.4s ease;
  83. }
  84. .giscus-hint {
  85. font-size: 0.9em;
  86. color: rgba(255, 255, 255, 0.9);
  87. margin: 0;
  88. }
  89. /* --- 2. 新增:暗黑模式样式 (Dark Mode Styles) --- */
  90. :root {
  91. --dark-bg: #1a1a1a;
  92. --dark-text: #c4c4c4;
  93. --dark-sidebar: #141414;
  94. --dark-code-bg: #2b2b2b;
  95. --dark-border: #333;
  96. --theme-color: #42b983;
  97. }
  98. /* 基础反色 */
  99. body.dark-mode {
  100. background-color: var(--dark-bg);
  101. color: var(--dark-text);
  102. }
  103. /* 侧边栏适配 */
  104. body.dark-mode .sidebar {
  105. background-color: var(--dark-sidebar);
  106. border-right: 1px solid var(--dark-border);
  107. color: var(--dark-text);
  108. }
  109. body.dark-mode .sidebar-nav li a {
  110. color: #999;
  111. }
  112. body.dark-mode .sidebar-nav li.active > a {
  113. color: var(--theme-color);
  114. border-right: 2px solid var(--theme-color);
  115. }
  116. /* 代码块适配 */
  117. body.dark-mode pre {
  118. background-color: var(--dark-code-bg) !important;
  119. }
  120. body.dark-mode code {
  121. background-color: var(--dark-code-bg) !important;
  122. color: #e0e0e0 !important;
  123. }
  124. body.dark-mode .markdown-section code {
  125. color: #f08d49;
  126. background-color: rgba(255,255,255,0.1);
  127. }
  128. /* 标题和引用适配 */
  129. body.dark-mode h1, body.dark-mode h2, body.dark-mode h3, body.dark-mode h4 {
  130. color: #e0e0e0;
  131. }
  132. body.dark-mode blockquote {
  133. color: #999;
  134. background: rgba(255,255,255,0.05);
  135. border-left-color: var(--theme-color);
  136. }
  137. /* 表格适配 */
  138. body.dark-mode .markdown-section tr:nth-child(2n) {
  139. background-color: rgba(255,255,255,0.03);
  140. }
  141. body.dark-mode .markdown-section td,
  142. body.dark-mode .markdown-section th {
  143. border-color: var(--dark-border);
  144. }
  145. /* Mermaid 图表适配 (颜色反转) */
  146. body.dark-mode .mermaid {
  147. filter: invert(1) hue-rotate(180deg);
  148. }
  149. /* Giscus 容器边框适配 */
  150. body.dark-mode .giscus-container {
  151. border-top: 1px solid var(--dark-border);
  152. }
  153. /* --- 3. 新增:侧边栏开关按钮样式 --- */
  154. .sidebar-toggle-btn {
  155. cursor: pointer;
  156. display: block;
  157. text-align: center;
  158. padding: 10px 0;
  159. margin: 0 15px 10px 15px;
  160. font-weight: bold;
  161. font-size: 14px;
  162. border-radius: 4px;
  163. background-color: rgba(0,0,0,0.05);
  164. color: #505d6b;
  165. border: 1px solid rgba(0,0,0,0.05);
  166. transition: all 0.3s;
  167. }
  168. body.dark-mode .sidebar-toggle-btn {
  169. background-color: rgba(255,255,255,0.1);
  170. color: #ccc;
  171. border: 1px solid #444;
  172. }
  173. .sidebar-toggle-btn:hover {
  174. background-color: var(--theme-color);
  175. color: white;
  176. }
  177. /* 深色模式下修正 docsify 自带 .sidebar-toggle (左下角汉堡) 的浅色背景 */
  178. /* vue.css 默认写死 background-color: hsla(0,0%,100%,.8),并在小屏折叠态
  179. (body.close .sidebar-toggle) 下回写同色,这里一并覆盖 */
  180. body.dark-mode .sidebar-toggle,
  181. body.dark-mode.close .sidebar-toggle {
  182. background-color: transparent;
  183. }
  184. </style>
  185. </head>
  186. <body>
  187. <!-- 语言切换按钮 -->
  188. <div class="lang-switch">
  189. <button id="langBtn" onclick="switchLanguage()">English</button>
  190. </div>
  191. <div id="app"></div>
  192. <script src="//cdn.jsdelivr.net/npm/mermaid@8.0.0-rc.8/dist/mermaid.min.js"></script>
  193. <script>
  194. window.$docsify = {
  195. name: 'Hello-Agents',
  196. repo: 'https://github.com/datawhalechina/Hello-Agents',
  197. loadSidebar: true,
  198. auto2top: true,
  199. subMaxLevel: 3,
  200. relativePath: false,
  201. alias: {
  202. // 英文路径映射
  203. '/en/README.md': '/README_EN.md',
  204. '/en/_sidebar.md': '/_sidebar_en.md',
  205. '/en/chapter(\\d+)/Chapter(.*)': '/chapter$1/Chapter$2',
  206. // 默认中文侧边栏
  207. '/_sidebar.md': '/_sidebar.md'
  208. },
  209. pagination: {
  210. previousText: '上一章节',
  211. nextText: '下一章节',
  212. },
  213. count: {
  214. countable: true,
  215. fontsize: '0.9em',
  216. color: 'rgb(90,90,90)',
  217. language: 'chinese'
  218. },
  219. // 多语言配置
  220. fallbackLanguages: ['en'],
  221. nameLink: {
  222. '/en/': '#/en/',
  223. '/': '#/'
  224. },
  225. // 使用钩子动态处理侧边栏
  226. plugins: [
  227. // --- 新增:暗黑模式开关插件 ---
  228. function(hook, vm) {
  229. hook.doneEach(function() {
  230. const sidebar = document.querySelector('.sidebar-nav');
  231. // 防止重复添加
  232. if (!sidebar || document.querySelector('.sidebar-toggle-btn')) return;
  233. const btn = document.createElement('div');
  234. btn.className = 'sidebar-toggle-btn';
  235. // 初始化状态
  236. const savedTheme = localStorage.getItem('theme-mode');
  237. if (savedTheme === 'dark') {
  238. document.body.classList.add('dark-mode');
  239. btn.textContent = '🌙 Switch to Light';
  240. } else {
  241. btn.textContent = '☀️ Switch to Dark';
  242. }
  243. // 核心:点击事件
  244. btn.onclick = function() {
  245. // 1. 切换类名
  246. document.body.classList.toggle('dark-mode');
  247. const isDark = document.body.classList.contains('dark-mode');
  248. // 2. 保存状态
  249. localStorage.setItem('theme-mode', isDark ? 'dark' : 'light');
  250. // 3. 更新按钮文字
  251. btn.textContent = isDark ? '🌙 Switch to Light' : '☀️ Switch to Dark';
  252. // 4. 关键:通知 Giscus 切换主题 (无需刷新)
  253. const iframe = document.querySelector('iframe.giscus-frame');
  254. if (iframe) {
  255. iframe.contentWindow.postMessage({
  256. giscus: {
  257. setConfig: {
  258. theme: isDark ? 'dark' : 'light'
  259. }
  260. }
  261. }, 'https://giscus.app');
  262. }
  263. };
  264. // 插入到侧边栏最顶部
  265. sidebar.insertBefore(btn, sidebar.firstChild);
  266. });
  267. },
  268. // --- 原有逻辑:语言偏好处理 ---
  269. function(hook, vm) {
  270. // 在每次路由变化时检查语言偏好
  271. hook.beforeEach(function(content) {
  272. const preferredLang = localStorage.getItem('preferredLanguage');
  273. const currentPath = vm.route.path;
  274. // 根据当前路径或语言偏好更新分页文本
  275. if (currentPath.includes('/en/') || preferredLang === 'en') {
  276. window.$docsify.pagination.previousText = 'Previous';
  277. window.$docsify.pagination.nextText = 'Next';
  278. } else {
  279. window.$docsify.pagination.previousText = '上一章节';
  280. window.$docsify.pagination.nextText = '下一章节';
  281. }
  282. return content;
  283. });
  284. // 在页面渲染后添加 Giscus 评论区
  285. hook.doneEach(function() {
  286. // 检查是否是章节页面(排除首页和前言)
  287. const currentPath = vm.route.path;
  288. const isChapterPage = currentPath.includes('chapter') ||
  289. currentPath.includes('Chapter') ||
  290. currentPath.includes('第') && currentPath.includes('章');
  291. if (!isChapterPage) {
  292. return; // 非章节页面不显示评论区
  293. }
  294. // 移除旧的评论区
  295. const oldGiscus = document.querySelector('.giscus-container');
  296. if (oldGiscus) {
  297. oldGiscus.remove();
  298. }
  299. // 创建评论区容器
  300. const giscusContainer = document.createElement('div');
  301. giscusContainer.className = 'giscus-container';
  302. // 获取语言设置
  303. const preferredLang = localStorage.getItem('preferredLanguage');
  304. const isEnglish = currentPath.includes('/en/') || preferredLang === 'en';
  305. // --- 获取当前主题状态 (新增逻辑) ---
  306. // 这里是为了在创建 Giscus 时就设置正确的初始主题
  307. const isDarkMode = document.body.classList.contains('dark-mode');
  308. // 创建可折叠的标题栏
  309. const toggleButton = document.createElement('div');
  310. toggleButton.className = 'giscus-toggle';
  311. const titleDiv = document.createElement('div');
  312. titleDiv.className = 'giscus-toggle-title';
  313. titleDiv.innerHTML = isEnglish
  314. ? '💬 Discussion & Questions'
  315. : '💬 讨论与提问';
  316. const hintText = document.createElement('p');
  317. hintText.className = 'giscus-hint';
  318. hintText.textContent = isEnglish
  319. ? 'Click to expand/collapse'
  320. : '点击展开/收起';
  321. const iconSpan = document.createElement('span');
  322. iconSpan.className = 'giscus-toggle-icon';
  323. iconSpan.textContent = '▼';
  324. const titleWrapper = document.createElement('div');
  325. titleWrapper.appendChild(titleDiv);
  326. titleWrapper.appendChild(hintText);
  327. toggleButton.appendChild(titleWrapper);
  328. toggleButton.appendChild(iconSpan);
  329. // 创建评论内容容器
  330. const contentDiv = document.createElement('div');
  331. contentDiv.className = 'giscus-content';
  332. // 创建 Giscus 脚本
  333. const giscusScript = document.createElement('script');
  334. giscusScript.src = 'https://giscus.app/client.js';
  335. giscusScript.setAttribute('data-repo', 'datawhalechina/hello-agents');
  336. giscusScript.setAttribute('data-repo-id', 'R_kgDOPrUECg');
  337. giscusScript.setAttribute('data-category', '💬 Exercises & Q&A');
  338. giscusScript.setAttribute('data-category-id', 'DIC_kwDOPrUECs4Cxfyu');
  339. giscusScript.setAttribute('data-mapping', 'pathname');
  340. giscusScript.setAttribute('data-strict', '0');
  341. giscusScript.setAttribute('data-reactions-enabled', '1');
  342. giscusScript.setAttribute('data-emit-metadata', '0');
  343. giscusScript.setAttribute('data-input-position', 'top');
  344. // --- 修改:强制跟随当前 dark-mode 状态 ---
  345. // 如果当前 body 有 dark-mode 类,则初始化为 'dark',否则 'light'
  346. giscusScript.setAttribute('data-theme', isDarkMode ? 'dark' : 'light');
  347. giscusScript.setAttribute('data-lang', isEnglish ? 'en' : 'zh-CN');
  348. giscusScript.setAttribute('data-loading', 'lazy');
  349. giscusScript.crossOrigin = 'anonymous';
  350. giscusScript.async = true;
  351. contentDiv.appendChild(giscusScript);
  352. // 组装评论区
  353. giscusContainer.appendChild(toggleButton);
  354. giscusContainer.appendChild(contentDiv);
  355. // 添加折叠/展开功能
  356. let isExpanded = false;
  357. toggleButton.addEventListener('click', function() {
  358. isExpanded = !isExpanded;
  359. if (isExpanded) {
  360. contentDiv.classList.add('expanded');
  361. iconSpan.classList.add('expanded');
  362. } else {
  363. contentDiv.classList.remove('expanded');
  364. iconSpan.classList.remove('expanded');
  365. }
  366. });
  367. // 将评论区添加到内容区域
  368. const article = document.querySelector('article.markdown-section');
  369. if (article) {
  370. article.appendChild(giscusContainer);
  371. }
  372. });
  373. }
  374. ]
  375. }
  376. </script>
  377. <!-- Put them above docsify.min.js -->
  378. <script src="//cdn.jsdelivr.net/npm/docsify@latest/lib/docsify.min.js"></script>
  379. <!-- code render-->
  380. <script src="//cdn.jsdelivr.net/npm/prismjs@latest/components/prism-bash.js"></script>
  381. <script src="//cdn.jsdelivr.net/npm/prismjs@latest/components/prism-python.js"></script>
  382. <script src="//cdn.jsdelivr.net/npm/docsify-pagination@latest/dist/docsify-pagination.min.js"></script>
  383. <script src="//cdn.jsdelivr.net/npm/docsify-copy-code"></script>
  384. <!-- 新增:图片放大插件 (官方) -->
  385. <script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/zoom-image.min.js"></script>
  386. <script src="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js"></script>
  387. <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css" />
  388. <script src="https://cdn.jsdelivr.net/npm/marked@3"></script>
  389. <!-- CDN files for docsify-katex -->
  390. <script src="//cdn.jsdelivr.net/npm/docsify-katex@latest/dist/docsify-katex.js"></script>
  391. <!-- 字数统计 -->
  392. <script src="//unpkg.com/docsify-count/dist/countable.js"></script>
  393. <!-- 语言切换脚本 (保留原样) -->
  394. <script>
  395. // 章节文件名映射
  396. const chapterMapping = {
  397. // 中文 -> 英文
  398. 'zh2en': {
  399. 'README.md': 'README_EN.md',
  400. '前言.md': 'Preface.md',
  401. '第一章 初识智能体.md': 'Chapter1-Introduction-to-Agents.md',
  402. '第二章 智能体发展史.md': 'Chapter2-History-of-Agents.md',
  403. '第三章 大语言模型基础.md': 'Chapter3-Fundamentals-of-Large-Language-Models.md',
  404. '第四章 智能体经典范式构建.md': 'Chapter4-Building-Classic-Agent-Paradigms.md',
  405. '第五章 基于低代码平台的智能体搭建.md': 'Chapter5-Building-Agents-with-Low-Code-Platforms.md',
  406. '第六章 框架开发实践.md': 'Chapter6-Framework-Development-Practice.md',
  407. '第七章 构建你的Agent框架.md': 'Chapter7-Building-Your-Agent-Framework.md',
  408. '第八章 记忆与检索.md': 'Chapter8-Memory-and-Retrieval.md',
  409. '第九章 上下文工程.md': 'Chapter9-Context-Engineering.md',
  410. '第十章 智能体通信协议.md': 'Chapter10-Agent-Communication-Protocols.md',
  411. '第十一章 Agentic-RL.md': 'Chapter11-Agentic-RL.md',
  412. '第十二章 智能体性能评估.md': 'Chapter12-Agent-Performance-Evaluation.md',
  413. '第十三章 智能旅行助手.md': 'Chapter13-Intelligent-Travel-Assistant.md',
  414. '第十四章 自动化深度研究智能体.md': 'Chapter14-Automated-Deep-Research-Agent.md',
  415. '第十五章 构建赛博小镇.md': 'Chapter15-Building-Cyber-Town.md',
  416. '第十六章 毕业设计.md': 'Chapter16-Graduation-Project.md'
  417. },
  418. // 英文 -> 中文
  419. 'en2zh': {
  420. 'README_EN.md': 'README.md',
  421. 'Preface.md': '前言.md',
  422. 'Chapter1-Introduction-to-Agents.md': '第一章 初识智能体.md',
  423. 'Chapter2-History-of-Agents.md': '第二章 智能体发展史.md',
  424. 'Chapter3-Fundamentals-of-Large-Language-Models.md': '第三章 大语言模型基础.md',
  425. 'Chapter4-Building-Classic-Agent-Paradigms.md': '第四章 智能体经典范式构建.md',
  426. 'Chapter5-Building-Agents-with-Low-Code-Platforms.md': '第五章 基于低代码平台的智能体搭建.md',
  427. 'Chapter6-Framework-Development-Practice.md': '第六章 框架开发实践.md',
  428. 'Chapter7-Building-Your-Agent-Framework.md': '第七章 构建你的Agent框架.md',
  429. 'Chapter8-Memory-and-Retrieval.md': '第八章 记忆与检索.md',
  430. 'Chapter9-Context-Engineering.md': '第九章 上下文工程.md',
  431. 'Chapter10-Agent-Communication-Protocols.md': '第十章 智能体通信协议.md',
  432. 'Chapter11-Agentic-RL.md': '第十一章 Agentic-RL.md',
  433. 'Chapter12-Agent-Performance-Evaluation.md': '第十二章 智能体性能评估.md',
  434. 'Chapter13-Intelligent-Travel-Assistant.md': '第十三章 智能旅行助手.md',
  435. 'Chapter14-Automated-Deep-Research-Agent.md': '第十四章 自动化深度研究智能体.md',
  436. 'Chapter15-Building-Cyber-Town.md': '第十五章 构建赛博小镇.md',
  437. 'Chapter16-Graduation-Project.md': '第十六章 毕业设计.md'
  438. }
  439. };
  440. function switchLanguage() {
  441. const currentHash = window.location.hash;
  442. const langBtn = document.getElementById('langBtn');
  443. // 检测当前语言
  444. if (currentHash.includes('/en/')) {
  445. // 从英文切换到中文
  446. localStorage.setItem('preferredLanguage', 'zh');
  447. let newHash = currentHash.replace('#/', '').replace('en/', '');
  448. // 提取文件名
  449. const parts = newHash.split('/');
  450. const filename = parts[parts.length - 1];
  451. // 查找对应的中文文件名
  452. if (chapterMapping.en2zh[filename]) {
  453. parts[parts.length - 1] = chapterMapping.en2zh[filename];
  454. newHash = '#/' + parts.join('/');
  455. } else {
  456. newHash = '#/';
  457. }
  458. window.location.hash = newHash;
  459. langBtn.textContent = 'English';
  460. window.$docsify.pagination.previousText = '上一章节';
  461. window.$docsify.pagination.nextText = '下一章节';
  462. } else {
  463. // 从中文切换到英文
  464. localStorage.setItem('preferredLanguage', 'en');
  465. let path = currentHash.replace('#/', '');
  466. if (path === '' || path === '/') {
  467. // 首页
  468. window.location.hash = '#/en/README_EN.md';
  469. } else {
  470. // 提取文件名
  471. const parts = path.split('/');
  472. const filename = parts[parts.length - 1];
  473. // 查找对应的英文文件名
  474. if (chapterMapping.zh2en[filename]) {
  475. parts[parts.length - 1] = chapterMapping.zh2en[filename];
  476. window.location.hash = '#/en/' + parts.join('/');
  477. } else {
  478. window.location.hash = '#/en/README_EN.md';
  479. }
  480. }
  481. langBtn.textContent = '中文';
  482. window.$docsify.pagination.previousText = 'Previous';
  483. window.$docsify.pagination.nextText = 'Next';
  484. }
  485. // 重新加载页面以应用新的 sidebar
  486. window.location.reload();
  487. }
  488. // 页面加载时设置按钮文本和检查语言偏好
  489. window.addEventListener('load', function() {
  490. const currentHash = window.location.hash;
  491. const langBtn = document.getElementById('langBtn');
  492. const preferredLang = localStorage.getItem('preferredLanguage');
  493. // 只有在用户明确设置了语言偏好后,才自动切换
  494. // 首次访问(没有偏好设置)时,默认显示中文版
  495. if (preferredLang) {
  496. // 如果用户有语言偏好,且当前 URL 不匹配偏好,则自动切换
  497. if (preferredLang === 'en' && !currentHash.includes('/en/')) {
  498. // 用户偏好英文,但当前是中文页面
  499. let path = currentHash.replace('#/', '');
  500. if (path === '' || path === '/') {
  501. window.location.hash = '#/en/README_EN.md';
  502. } else {
  503. const parts = path.split('/');
  504. const filename = parts[parts.length - 1];
  505. if (chapterMapping.zh2en[filename]) {
  506. parts[parts.length - 1] = chapterMapping.zh2en[filename];
  507. window.location.hash = '#/en/' + parts.join('/');
  508. }
  509. }
  510. } else if (preferredLang === 'zh' && currentHash.includes('/en/')) {
  511. // 用户偏好中文,但当前是英文页面
  512. let newHash = currentHash.replace('#/', '').replace('en/', '');
  513. const parts = newHash.split('/');
  514. const filename = parts[parts.length - 1];
  515. if (chapterMapping.en2zh[filename]) {
  516. parts[parts.length - 1] = chapterMapping.en2zh[filename];
  517. window.location.hash = '#/' + parts.join('/');
  518. }
  519. }
  520. }
  521. // 设置按钮文本
  522. if (currentHash.includes('/en/')) {
  523. langBtn.textContent = '中文';
  524. } else {
  525. langBtn.textContent = 'English';
  526. }
  527. });
  528. </script>
  529. </body>
  530. </html>