[ BUG ] TOC links break when a path with a space is one of several inputs

Open Beginner friendly
#100 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
cli

Research direction

The path prefix is added in internal/core/toc/renderer.go, in Renderer.Render. Read that function and the existing renderer tests first. Encode spaces in the link prefix as %20, then confirm inputs without spaces and error messages retain their current output; the reproduction in the issue gives the expected TOC.

Written by the indexing model from the issue text.

Description

bug

Describe the bug

With several inputs, every TOC entry links to <input>#<anchor>, with the input as given
on the command line. When the input has a space, the link is not valid Markdown, and
GitHub renders the entry as plain text. The names of GitHub wiki pages can have spaces.
The Bash version had the same bug (ekalinin/github-markdown-toc#76) and fixed it in
ekalinin/github-markdown-toc#190.

To Reproduce

mkdir "test directory"
printf '# Title one\n\n## Section\n' > "test directory/a.md"
printf '# Title two\n' > "test directory/b.md"
gh-md-toc --hide-footer "test directory/a.md" "test directory/b.md"

Actual:

* [Title one](test directory/a.md#title-one)
  * [Section](test directory/a.md#section)

* [Title two](test directory/b.md#title-two)

The GitHub Markdown API renders the first entry as
<li>[Title one](test directory/a.md#title-one)</li>, without a link.

Expected behavior

As in the Bash version, every space in the link prefix is replaced with %20, and no
other character changes:

* [Title one](test%20directory/a.md#title-one)
  * [Section](test%20directory/a.md#section)

* [Title two](test%20directory/b.md#title-two)

Inputs without a space give the same output as now. Messages that show the input, such
as errors, keep it as given.

Environment (please complete the following information):

  • OS: macOS (darwin/arm64)
  • Version: 2.0.1, current master (dd680f7)

Additional context

  • The prefix is added in Renderer.Render (internal/core/toc/renderer.go):
    link = path + link.
  • With --insert, local files get bare anchors, so the TOC written into them is not
    affected.
Dominant language
Go
Stars
525
Forks
89
Avg merge
6m
Merged PRs (30d)
1

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ekalinin/github-markdown-toc.go

All issues in ekalinin/github-markdown-toc.go

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.