@@ -86,10 +86,10 @@ export default {
8686
8787 if (docPath) {
8888 // 如果URL中有文档路径,加载该文档
89- this .loadMarkdown (docPath)
89+ this .loadMarkdown (docPath, true )
9090 } else {
9191 // 否则加载默认文档
92- this .loadMarkdown (this .menuItems [0 ].path )
92+ this .loadMarkdown (this .menuItems [0 ].path , true )
9393 }
9494
9595 // 添加事件监听器来处理浏览器的前进/后退按钮操作
@@ -98,14 +98,14 @@ export default {
9898 const newDocPath = newUrlParams .get (' doc' )
9999
100100 if (newDocPath) {
101- this .loadMarkdown (newDocPath)
101+ this .loadMarkdown (newDocPath, true )
102102 } else {
103- this .loadMarkdown (this .menuItems [0 ].path )
103+ this .loadMarkdown (this .menuItems [0 ].path , true )
104104 }
105105 })
106106 },
107107 methods: {
108- async loadMarkdown (path ) {
108+ async loadMarkdown (path , isInitialLoad = false ) {
109109 try {
110110 const response = await axios .get (path)
111111 this .content = response .data
@@ -114,7 +114,18 @@ export default {
114114 // 更新URL查询参数,不刷新页面
115115 const url = new URL (window .location )
116116 url .searchParams .set (' doc' , path)
117- window .history .pushState ({}, ' ' , url)
117+
118+ // 只有在非初始加载时才更新历史记录
119+ // 这样可以避免刷新页面时重复添加历史记录
120+ if (! isInitialLoad) {
121+ window .history .pushState ({}, ' ' , url)
122+ } else {
123+ // 在初始加载时,只替换当前的历史记录
124+ window .history .replaceState ({}, ' ' , url)
125+ }
126+
127+ // 自动展开包含当前文档的菜单组
128+ this .expandMenuForPath (path)
118129
119130 // 移动端点击菜单项后自动关闭菜单
120131 if (window .innerWidth <= 768 ) {
@@ -127,10 +138,26 @@ export default {
127138 // 如果路径无效且不是默认路径,尝试加载默认文档
128139 if (path !== this .menuItems [0 ].path ) {
129140 console .log (' 尝试加载默认文档' )
130- this .loadMarkdown (this .menuItems [0 ].path )
141+ this .loadMarkdown (this .menuItems [0 ].path , isInitialLoad )
131142 }
132143 }
133144 },
145+
146+ // 查找并展开包含当前文档的菜单组
147+ expandMenuForPath (path ) {
148+ // 重置已打开的菜单组
149+ // this.openGroups = []
150+
151+ // 查找包含当前路径的菜单组
152+ this .menuItems .forEach ((item , index ) => {
153+ if (item .children ) {
154+ const hasPath = item .children .some (child => child .path === path)
155+ if (hasPath && ! this .openGroups .includes (index)) {
156+ this .openGroups .push (index)
157+ }
158+ }
159+ })
160+ },
134161 toggleMenu () {
135162 this .menuOpen = ! this .menuOpen
136163 },
0 commit comments