Skip to content

Commit 94e075c

Browse files
stevesCopilot
andauthored
Remove query strings when normalizing links for dead link checks (#60815)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: steves <54561+steves@users.noreply.github.com>
1 parent 07b1bd6 commit 94e075c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/links/lib/extract-links.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,17 @@ export function getRelativePath(filePath: string): string {
288288
/**
289289
* Normalize a link path for comparison with pageMap
290290
*
291+
* - Removes query strings
291292
* - Removes trailing slashes
292293
* - Removes anchor fragments
293294
* - Ensures leading slash
294295
*/
295296
export function normalizeLinkPath(href: string): string {
297+
// Remove query string
298+
let normalized = href.split('?')[0]
299+
296300
// Remove anchor
297-
let normalized = href.split('#')[0]
301+
normalized = normalized.split('#')[0]
298302

299303
// Remove trailing slash
300304
if (normalized.endsWith('/') && normalized.length > 1) {

src/links/tests/extract-links.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,22 @@ describe('normalizeLinkPath', () => {
243243
'/en/enterprise-server@3.10/admin/overview',
244244
)
245245
})
246+
247+
test('removes query string', () => {
248+
expect(normalizeLinkPath('/actions/guides?tab=cli')).toBe('/actions/guides')
249+
})
250+
251+
test('removes query string before anchor fragment', () => {
252+
expect(normalizeLinkPath('/actions/guides?tab=cli#section')).toBe('/actions/guides')
253+
})
254+
255+
test('removes query string with trailing slash', () => {
256+
expect(normalizeLinkPath('/actions/guides/?tab=cli')).toBe('/actions/guides')
257+
})
258+
259+
test('handles path with only a query string (no anchor)', () => {
260+
expect(normalizeLinkPath('/repositories/overview?version=3')).toBe('/repositories/overview')
261+
})
246262
})
247263

248264
describe('checkInternalLink', () => {

0 commit comments

Comments
 (0)