Skip to content

feat: add Insert Link to Header in File command for markdown#306561

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:feat/insert-header-link-command
Open

feat: add Insert Link to Header in File command for markdown#306561
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:feat/insert-header-link-command

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Feature — adds a new command to the markdown-language-features extension.

What is the current behavior?

When writing markdown links to headers (e.g., [text](#header-name)), users must type the slugified header name with dashes instead of spaces in the suggest widget. There is no way to search for headers using their natural text with spaces.

Closes #174999

What is the new behavior?

Adds a new Markdown: Insert Link to Header in File command (markdown.editor.insertHeaderLink) that:

  1. Retrieves all headers from the current document via vscode.executeDocumentSymbolProvider
  2. Shows a quick pick with all headers listed by their natural text (e.g., "A header with spaces in the title")
  3. Displays the corresponding slug in the description (e.g., a-header-with-spaces-in-the-title)
  4. Uses VS Code's fuzzy matching so users can search by natural text with spaces
  5. Inserts a properly formatted markdown link [Header Text](#slug) at the cursor position
  6. If text is selected, uses the selection as the link text instead

This follows the approach suggested by @mjbvz in the issue: a dedicated command with a quick pick UI, similar to how Markdown: Insert Link to File in Workspace works.

Implementation details

  • New InsertHeaderLink class in insertResource.ts, following the same Command pattern as InsertLinkFromWorkspace and InsertImageFromWorkspace
  • Uses the existing ISlugifier (GitHub-compatible) to generate correct slugs, including handling of duplicate headers
  • Recursively flattens nested DocumentSymbol hierarchy to collect all headers
  • Inserts via SnippetString with a placeholder for the link text when no text is selected

Additional context

The command is available in the Command Palette as "Markdown: Insert Link to Header in File" whenever a markdown file is active in the editor.

Add a new 'Markdown: Insert Link to Header in File' command that shows
a quick pick with all headers in the current document, allowing users
to search by natural header text (with spaces) and insert a properly
slugified markdown link.

This addresses the issue where searching for headers in the suggest
widget requires using dashes instead of spaces. The new command provides
a dedicated quick pick UI for header link insertion, similar to the
existing 'Insert Link to File in Workspace' command.
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.

Allow me to search for markdown headers without manually inserting - delimiters

2 participants