fix(links): prevent panic when page title equals path hint in RewriteWikiLinks#1164
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a panic in MarkdownRefactorEngine.RewriteWikiLinks that could occur when a rule’s OldTitle matches the normalized OldPath hint, causing overlapping replacements and an invalid slice operation during replacement application.
Changes:
- Skip generating the path-hint rewrite when the title rewrite already covers the same target (case-insensitive).
- Add an overlap-deduplication pass over sorted replacements to prevent slice-bounds panics.
- Add a regression test reproducing the panic scenario and asserting correct rewrite output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/links/link_refactor.go | Prevents overlapping rewrite replacements in RewriteWikiLinks via rule-level suppression and post-sort deduplication. |
| internal/links/link_refactor_wikilinks_test.go | Adds a regression test covering the OldTitle == OldPath hint overlap/panic case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…hint Root cause: when OldTitle matched the normalized path hint (e.g. "intro" == "intro"), both the case-insensitive title regex and the path-hint regex matched the same byte span. With sort.Slice (unstable), the two replacements could land in arbitrary order, and the second one produced a backwards slice index [18:10] → panic in applyReplacements. Fix: - Use sort.SliceStable so the title rewrite (inserted first) always wins - Add a dedup pass that drops any replacement whose start falls inside the previous replacement's end range - Remove the titleCoversHint pre-filter (too many edge cases); dedup guard handles all overlap cases uniformly - Add overlap guard inside applyReplacements itself as a last-resort safety net Performance (bulk rename): - Extract CompiledWikiLinkRewrites + CompileWikiLinkRewrites so regexes are compiled once per operation (N) instead of once per page (M×N) - RewriteWikiLinks becomes a thin wrapper; callers doing bulk renames use RewriteWikiLinksPrecompiled Refactor: - Add collectExcludedRanges helper; RewriteWikiLinksPrecompiled and FindWikiLinksForPath call it instead of discarding the candidates return value of collectCandidatesAndExcludedRanges - Add debug log before bulk page loop: pages=N rules=M Regression tests added for both panic scenario and path-only move with same title.
perber
force-pushed
the
fix/wikilink-rename-panic
branch
from
June 16, 2026 13:15
6504672 to
3260c28
Compare
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When OldTitle and the normalised OldPath hint are the same string (e.g. title "intro", path "/intro"), both the title rewrite and the path-hint rewrite generated a regex that matched the identical byte range. applyReplacements then tried to slice content[End:Start] (e.g. [18:10]) and panicked.
Fix: skip the path-hint rewrite when the title rewrite already covers it (strings.EqualFold comparison), and add a defensive overlap-dedup pass after sorting replacements. Regression test added.