Skip to content

Commit 8885de2

Browse files
committed
fix: implement stripUrlExceptId function to clean URLs
1 parent 7278923 commit 8885de2

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/core/event/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isMobile, mobileBreakpoint } from '../util/env.js';
22
import * as dom from '../util/dom.js';
3+
import { stripUrlExceptId } from '../router/util.js';
34

45
/** @typedef {import('../Docsify.js').Constructor} Constructor */
56

@@ -474,6 +475,8 @@ export function Events(Base) {
474475
return;
475476
}
476477

478+
href = stripUrlExceptId(href);
479+
477480
const oldActive = dom.find(sidebar, 'li.active');
478481
const newActive = dom
479482
.find(

src/core/render/compiler/heading.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getAndRemoveDocsifyIgnoreConfig,
55
} from '../utils.js';
66
import { slugify } from '../slugify.js';
7+
import { stripUrlExceptId } from '../../router/util.js';
78

89
export const headingCompiler = ({ renderer, router, compiler }) =>
910
(renderer.heading = function ({ tokens, depth }) {
@@ -20,7 +21,7 @@ export const headingCompiler = ({ renderer, router, compiler }) =>
2021
nextToc.ignoreSubHeading = ignoreSubHeading;
2122
const slug = slugify(config.id || str);
2223
const url = router.toURL(router.getCurrentPath(), { id: slug });
23-
nextToc.slug = url;
24+
nextToc.slug = stripUrlExceptId(url);
2425
compiler.toc.push(nextToc);
2526

2627
// Note: tabindex="-1" allows programmatically focusing on heading

src/core/router/history/base.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ export class History {
8282
const local = currentRoute && path[0] === '#';
8383
const route = this.parse(replaceSlug(path));
8484

85-
if (route?.query?.s) {
86-
delete route.query.s;
87-
}
88-
8985
route.query = { ...route.query, ...params };
9086
path = route.path + stringifyQuery(route.query);
9187
path = path.replace(/\.md(\?)|\.md$/, '$1');

src/core/router/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ export function stringifyQuery(obj, ignores = []) {
4040
return qs.length ? `?${qs.join('&')}` : '';
4141
}
4242

43+
export function stripUrlExceptId(str) {
44+
const [path, queryString] = str.split('?');
45+
if (!queryString) {return str;}
46+
47+
const params = new URLSearchParams(queryString);
48+
const id = params.get('id');
49+
50+
if (id !== null) {
51+
return `${path}?id=${id}`;
52+
}
53+
54+
return path;
55+
}
56+
4357
export const isAbsolutePath = cached(path => {
4458
return /(:|(\/{2}))/g.test(path);
4559
});

0 commit comments

Comments
 (0)