Skip to content

Render fragment-only hyperlinks as plain text instead of dead links - #2265

Open
abhays02 wants to merge 1 commit into
microsoft:mainfrom
abhays02:fix/2125-docx-toc-anchors
Open

Render fragment-only hyperlinks as plain text instead of dead links#2265
abhays02 wants to merge 1 commit into
microsoft:mainfrom
abhays02:fix/2125-docx-toc-anchors

Conversation

@abhays02

@abhays02 abhays02 commented Aug 2, 2026

Copy link
Copy Markdown

Internal DOCX hyperlinks such as TOC entries and cross-references are converted to Word bookmark anchors like [Executive Summary](#_Toc12345). These anchors never resolve in the Markdown output: nothing in the conversion pipeline preserves heading ids (see _CustomMarkdownify.convert_hn), so a fragment-only href can never point at anything, no matter which converter produced the source HTML (DOCX, PPTX, or raw HTML). The result is a block of dead links.

This changes _CustomMarkdownify.convert_a in _markdownify.py to detect hrefs that are fragment-only (no scheme, no netloc, no path — just #...) and render the link text plain instead of wrapping it in a non-functional Markdown link. Hrefs with a path plus a fragment (e.g. page.html#section) are unaffected.

Fixes #2125.

Verified with a regression test (test_fragment_only_links_render_as_plain_text in tests/test_module_misc.py) that:

  • fails against the old code ([Executive Summary](#_Toc12345) present in output)
  • passes against the fix
  • confirms normal external links and path+fragment links are unaffected

Full existing test suite: 335 passed, 4 skipped, 2 failed. Both failures (test_markitdown_remote, test_speech_transcription) are pre-existing and unrelated — they fail identically with this change reverted, due to network access to arxiv.org and a speech-recognition API being blocked in the sandbox this was verified in.

@abhays02

abhays02 commented Aug 2, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@abhays02
abhays02 marked this pull request as ready for review August 2, 2026 07:45
Internal anchors like Word TOC/cross-reference bookmarks (#_Toc12345)
never resolve in the Markdown output, since heading ids are not
preserved anywhere in the conversion pipeline. Fixes microsoft#2125.
@abhays02
abhays02 force-pushed the fix/2125-docx-toc-anchors branch from 4467de9 to 709982f Compare August 2, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOCX: internal TOC / cross-reference hyperlinks emit dead [text](#_Toc…) anchors

1 participant