index.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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/README_EN.md': '/README_EN.md',
  205. '/en/Preface.md': '/Preface.md',
  206. '/en/_sidebar.md': '/_sidebar_en.md',
  207. '/en/.*/_sidebar.md': '/_sidebar_en.md',
  208. '/en/(.*)': '/$1',
  209. // 默认中文侧边栏
  210. '/_sidebar.md': '/_sidebar.md',
  211. '/.*/_sidebar.md': '/_sidebar.md'
  212. },
  213. pagination: {
  214. previousText: '上一章节',
  215. nextText: '下一章节',
  216. },
  217. count: {
  218. countable: true,
  219. fontsize: '0.9em',
  220. color: 'rgb(90,90,90)',
  221. language: 'chinese'
  222. },
  223. // 多语言配置
  224. fallbackLanguages: ['en'],
  225. nameLink: {
  226. '/en/': '#/en/',
  227. '/': '#/'
  228. },
  229. // 使用钩子动态处理侧边栏
  230. plugins: [
  231. // --- 新增:暗黑模式开关插件 ---
  232. function(hook, vm) {
  233. hook.doneEach(function() {
  234. const sidebar = document.querySelector('.sidebar-nav');
  235. // 防止重复添加
  236. if (!sidebar || document.querySelector('.sidebar-toggle-btn')) return;
  237. const btn = document.createElement('div');
  238. btn.className = 'sidebar-toggle-btn';
  239. // 初始化状态
  240. const savedTheme = localStorage.getItem('theme-mode');
  241. if (savedTheme === 'dark') {
  242. document.body.classList.add('dark-mode');
  243. btn.textContent = '🌙 Switch to Light';
  244. } else {
  245. btn.textContent = '☀️ Switch to Dark';
  246. }
  247. // 核心:点击事件
  248. btn.onclick = function() {
  249. // 1. 切换类名
  250. document.body.classList.toggle('dark-mode');
  251. const isDark = document.body.classList.contains('dark-mode');
  252. // 2. 保存状态
  253. localStorage.setItem('theme-mode', isDark ? 'dark' : 'light');
  254. // 3. 更新按钮文字
  255. btn.textContent = isDark ? '🌙 Switch to Light' : '☀️ Switch to Dark';
  256. // 4. 关键:通知 Giscus 切换主题 (无需刷新)
  257. const iframe = document.querySelector('iframe.giscus-frame');
  258. if (iframe) {
  259. iframe.contentWindow.postMessage({
  260. giscus: {
  261. setConfig: {
  262. theme: isDark ? 'dark' : 'light'
  263. }
  264. }
  265. }, 'https://giscus.app');
  266. }
  267. };
  268. // 插入到侧边栏最顶部
  269. sidebar.insertBefore(btn, sidebar.firstChild);
  270. });
  271. },
  272. // --- 原有逻辑:语言偏好处理 ---
  273. function(hook, vm) {
  274. // 在每次路由变化时检查语言偏好
  275. hook.beforeEach(function(content) {
  276. const preferredLang = localStorage.getItem('preferredLanguage');
  277. const currentPath = vm.route.path;
  278. // 根据当前路径或语言偏好更新分页文本
  279. if (currentPath.includes('/en/') || preferredLang === 'en') {
  280. window.$docsify.pagination.previousText = 'Previous';
  281. window.$docsify.pagination.nextText = 'Next';
  282. } else {
  283. window.$docsify.pagination.previousText = '上一章节';
  284. window.$docsify.pagination.nextText = '下一章节';
  285. }
  286. return content;
  287. });
  288. // 在页面渲染后添加 Giscus 评论区
  289. hook.doneEach(function() {
  290. // 检查是否是章节页面(排除首页和前言)
  291. const currentPath = vm.route.path;
  292. const isChapterPage = currentPath.includes('chapter') ||
  293. currentPath.includes('Chapter') ||
  294. currentPath.includes('第') && currentPath.includes('章');
  295. if (!isChapterPage) {
  296. return; // 非章节页面不显示评论区
  297. }
  298. // 移除旧的评论区
  299. const oldGiscus = document.querySelector('.giscus-container');
  300. if (oldGiscus) {
  301. oldGiscus.remove();
  302. }
  303. // 创建评论区容器
  304. const giscusContainer = document.createElement('div');
  305. giscusContainer.className = 'giscus-container';
  306. // 获取语言设置
  307. const preferredLang = localStorage.getItem('preferredLanguage');
  308. const isEnglish = currentPath.includes('/en/') || preferredLang === 'en';
  309. // --- 获取当前主题状态 (新增逻辑) ---
  310. // 这里是为了在创建 Giscus 时就设置正确的初始主题
  311. const isDarkMode = document.body.classList.contains('dark-mode');
  312. // 创建可折叠的标题栏
  313. const toggleButton = document.createElement('div');
  314. toggleButton.className = 'giscus-toggle';
  315. const titleDiv = document.createElement('div');
  316. titleDiv.className = 'giscus-toggle-title';
  317. titleDiv.innerHTML = isEnglish
  318. ? '💬 Discussion & Questions'
  319. : '💬 讨论与提问';
  320. const hintText = document.createElement('p');
  321. hintText.className = 'giscus-hint';
  322. hintText.textContent = isEnglish
  323. ? 'Click to expand/collapse'
  324. : '点击展开/收起';
  325. const iconSpan = document.createElement('span');
  326. iconSpan.className = 'giscus-toggle-icon';
  327. iconSpan.textContent = '▼';
  328. const titleWrapper = document.createElement('div');
  329. titleWrapper.appendChild(titleDiv);
  330. titleWrapper.appendChild(hintText);
  331. toggleButton.appendChild(titleWrapper);
  332. toggleButton.appendChild(iconSpan);
  333. // 创建评论内容容器
  334. const contentDiv = document.createElement('div');
  335. contentDiv.className = 'giscus-content';
  336. // 创建 Giscus 脚本
  337. const giscusScript = document.createElement('script');
  338. giscusScript.src = 'https://giscus.app/client.js';
  339. giscusScript.setAttribute('data-repo', 'datawhalechina/hello-agents');
  340. giscusScript.setAttribute('data-repo-id', 'R_kgDOPrUECg');
  341. giscusScript.setAttribute('data-category', '💬 Exercises & Q&A');
  342. giscusScript.setAttribute('data-category-id', 'DIC_kwDOPrUECs4Cxfyu');
  343. giscusScript.setAttribute('data-mapping', 'pathname');
  344. giscusScript.setAttribute('data-strict', '0');
  345. giscusScript.setAttribute('data-reactions-enabled', '1');
  346. giscusScript.setAttribute('data-emit-metadata', '0');
  347. giscusScript.setAttribute('data-input-position', 'top');
  348. // --- 修改:强制跟随当前 dark-mode 状态 ---
  349. // 如果当前 body 有 dark-mode 类,则初始化为 'dark',否则 'light'
  350. giscusScript.setAttribute('data-theme', isDarkMode ? 'dark' : 'light');
  351. giscusScript.setAttribute('data-lang', isEnglish ? 'en' : 'zh-CN');
  352. giscusScript.setAttribute('data-loading', 'lazy');
  353. giscusScript.crossOrigin = 'anonymous';
  354. giscusScript.async = true;
  355. contentDiv.appendChild(giscusScript);
  356. // 组装评论区
  357. giscusContainer.appendChild(toggleButton);
  358. giscusContainer.appendChild(contentDiv);
  359. // 添加折叠/展开功能
  360. let isExpanded = false;
  361. toggleButton.addEventListener('click', function() {
  362. isExpanded = !isExpanded;
  363. if (isExpanded) {
  364. contentDiv.classList.add('expanded');
  365. iconSpan.classList.add('expanded');
  366. } else {
  367. contentDiv.classList.remove('expanded');
  368. iconSpan.classList.remove('expanded');
  369. }
  370. });
  371. // 将评论区添加到内容区域
  372. const article = document.querySelector('article.markdown-section');
  373. if (article) {
  374. article.appendChild(giscusContainer);
  375. }
  376. });
  377. }
  378. ]
  379. }
  380. </script>
  381. <!-- Put them above docsify.min.js -->
  382. <script src="//cdn.jsdelivr.net/npm/docsify@latest/lib/docsify.min.js"></script>
  383. <!-- code render-->
  384. <script src="//cdn.jsdelivr.net/npm/prismjs@latest/components/prism-bash.js"></script>
  385. <script src="//cdn.jsdelivr.net/npm/prismjs@latest/components/prism-python.js"></script>
  386. <script src="//cdn.jsdelivr.net/npm/docsify-pagination@latest/dist/docsify-pagination.min.js"></script>
  387. <script src="//cdn.jsdelivr.net/npm/docsify-copy-code"></script>
  388. <!-- 新增:图片放大插件 (官方) -->
  389. <script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/zoom-image.min.js"></script>
  390. <script src="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js"></script>
  391. <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css" />
  392. <script src="https://cdn.jsdelivr.net/npm/marked@3"></script>
  393. <!-- CDN files for docsify-katex -->
  394. <script src="//cdn.jsdelivr.net/npm/docsify-katex@latest/dist/docsify-katex.js"></script>
  395. <!-- 字数统计 -->
  396. <script src="//unpkg.com/docsify-count/dist/countable.js"></script>
  397. <!-- 语言切换脚本 (保留原样) -->
  398. <script>
  399. // 章节文件名映射
  400. const chapterMapping = {
  401. // 中文 -> 英文
  402. 'zh2en': {
  403. 'README.md': 'README_EN.md',
  404. '前言.md': 'Preface.md',
  405. '第一章 初识智能体.md': 'Chapter1-Introduction-to-Agents.md',
  406. '第二章 智能体发展史.md': 'Chapter2-History-of-Agents.md',
  407. '第三章 大语言模型基础.md': 'Chapter3-Fundamentals-of-Large-Language-Models.md',
  408. '第四章 智能体经典范式构建.md': 'Chapter4-Building-Classic-Agent-Paradigms.md',
  409. '第五章 基于低代码平台的智能体搭建.md': 'Chapter5-Building-Agents-with-Low-Code-Platforms.md',
  410. '第六章 框架开发实践.md': 'Chapter6-Framework-Development-Practice.md',
  411. '第七章 构建你的Agent框架.md': 'Chapter7-Building-Your-Agent-Framework.md',
  412. '第八章 记忆与检索.md': 'Chapter8-Memory-and-Retrieval.md',
  413. '第九章 上下文工程.md': 'Chapter9-Context-Engineering.md',
  414. '第十章 智能体通信协议.md': 'Chapter10-Agent-Communication-Protocols.md',
  415. '第十一章 Agentic-RL.md': 'Chapter11-Agentic-RL.md',
  416. '第十二章 智能体性能评估.md': 'Chapter12-Agent-Performance-Evaluation.md',
  417. '第十三章 智能旅行助手.md': 'Chapter13-Intelligent-Travel-Assistant.md',
  418. '第十四章 自动化深度研究智能体.md': 'Chapter14-Automated-Deep-Research-Agent.md',
  419. '第十五章 构建赛博小镇.md': 'Chapter15-Building-Cyber-Town.md',
  420. '第十六章 毕业设计.md': 'Chapter16-Graduation-Project.md'
  421. },
  422. // 英文 -> 中文
  423. 'en2zh': {
  424. 'README_EN.md': 'README.md',
  425. 'Preface.md': '前言.md',
  426. 'Chapter1-Introduction-to-Agents.md': '第一章 初识智能体.md',
  427. 'Chapter2-History-of-Agents.md': '第二章 智能体发展史.md',
  428. 'Chapter3-Fundamentals-of-Large-Language-Models.md': '第三章 大语言模型基础.md',
  429. 'Chapter4-Building-Classic-Agent-Paradigms.md': '第四章 智能体经典范式构建.md',
  430. 'Chapter5-Building-Agents-with-Low-Code-Platforms.md': '第五章 基于低代码平台的智能体搭建.md',
  431. 'Chapter6-Framework-Development-Practice.md': '第六章 框架开发实践.md',
  432. 'Chapter7-Building-Your-Agent-Framework.md': '第七章 构建你的Agent框架.md',
  433. 'Chapter8-Memory-and-Retrieval.md': '第八章 记忆与检索.md',
  434. 'Chapter9-Context-Engineering.md': '第九章 上下文工程.md',
  435. 'Chapter10-Agent-Communication-Protocols.md': '第十章 智能体通信协议.md',
  436. 'Chapter11-Agentic-RL.md': '第十一章 Agentic-RL.md',
  437. 'Chapter12-Agent-Performance-Evaluation.md': '第十二章 智能体性能评估.md',
  438. 'Chapter13-Intelligent-Travel-Assistant.md': '第十三章 智能旅行助手.md',
  439. 'Chapter14-Automated-Deep-Research-Agent.md': '第十四章 自动化深度研究智能体.md',
  440. 'Chapter15-Building-Cyber-Town.md': '第十五章 构建赛博小镇.md',
  441. 'Chapter16-Graduation-Project.md': '第十六章 毕业设计.md'
  442. }
  443. };
  444. function switchLanguage() {
  445. const currentHash = window.location.hash;
  446. const langBtn = document.getElementById('langBtn');
  447. // 检测当前语言
  448. if (currentHash.includes('/en/')) {
  449. // 从英文切换到中文
  450. localStorage.setItem('preferredLanguage', 'zh');
  451. let newHash = currentHash.replace('#/', '').replace('en/', '');
  452. // 提取文件名
  453. const parts = newHash.split('/');
  454. const filename = parts[parts.length - 1];
  455. // 查找对应的中文文件名
  456. if (chapterMapping.en2zh[filename]) {
  457. parts[parts.length - 1] = chapterMapping.en2zh[filename];
  458. newHash = '#/' + parts.join('/');
  459. } else {
  460. newHash = '#/';
  461. }
  462. window.location.hash = newHash;
  463. langBtn.textContent = 'English';
  464. window.$docsify.pagination.previousText = '上一章节';
  465. window.$docsify.pagination.nextText = '下一章节';
  466. } else {
  467. // 从中文切换到英文
  468. localStorage.setItem('preferredLanguage', 'en');
  469. let path = currentHash.replace('#/', '');
  470. if (path === '' || path === '/') {
  471. // 首页
  472. window.location.hash = '#/en/README_EN.md';
  473. } else {
  474. // 提取文件名
  475. const parts = path.split('/');
  476. const filename = parts[parts.length - 1];
  477. // 查找对应的英文文件名
  478. if (chapterMapping.zh2en[filename]) {
  479. parts[parts.length - 1] = chapterMapping.zh2en[filename];
  480. window.location.hash = '#/en/' + parts.join('/');
  481. } else {
  482. window.location.hash = '#/en/README_EN.md';
  483. }
  484. }
  485. langBtn.textContent = '中文';
  486. window.$docsify.pagination.previousText = 'Previous';
  487. window.$docsify.pagination.nextText = 'Next';
  488. }
  489. // 重新加载页面以应用新的 sidebar
  490. window.location.reload();
  491. }
  492. // 页面加载时设置按钮文本和检查语言偏好
  493. window.addEventListener('load', function() {
  494. const currentHash = window.location.hash;
  495. const langBtn = document.getElementById('langBtn');
  496. const preferredLang = localStorage.getItem('preferredLanguage');
  497. // 只有在用户明确设置了语言偏好后,才自动切换
  498. // 首次访问(没有偏好设置)时,默认显示中文版
  499. if (preferredLang) {
  500. // 如果用户有语言偏好,且当前 URL 不匹配偏好,则自动切换
  501. if (preferredLang === 'en' && !currentHash.includes('/en/')) {
  502. // 用户偏好英文,但当前是中文页面
  503. let path = currentHash.replace('#/', '');
  504. if (path === '' || path === '/') {
  505. window.location.hash = '#/en/README_EN.md';
  506. } else {
  507. const parts = path.split('/');
  508. const filename = parts[parts.length - 1];
  509. if (chapterMapping.zh2en[filename]) {
  510. parts[parts.length - 1] = chapterMapping.zh2en[filename];
  511. window.location.hash = '#/en/' + parts.join('/');
  512. }
  513. }
  514. } else if (preferredLang === 'zh' && currentHash.includes('/en/')) {
  515. // 用户偏好中文,但当前是英文页面
  516. let newHash = currentHash.replace('#/', '').replace('en/', '');
  517. const parts = newHash.split('/');
  518. const filename = parts[parts.length - 1];
  519. if (chapterMapping.en2zh[filename]) {
  520. parts[parts.length - 1] = chapterMapping.en2zh[filename];
  521. window.location.hash = '#/' + parts.join('/');
  522. }
  523. }
  524. }
  525. // 设置按钮文本
  526. if (currentHash.includes('/en/')) {
  527. langBtn.textContent = '中文';
  528. } else {
  529. langBtn.textContent = 'English';
  530. }
  531. });
  532. </script>
  533. </body>
  534. </html>