Skip to content

Conversation

@Amitverma0509
Copy link

@Amitverma0509 Amitverma0509 commented Oct 10, 2025

Sumary

  • This PR improves the markdown package by fixing potential runtime issues and enhancing maintainability of the Markdown-to-HTML conversion logic.

Changes Made

1.Fixed nil map panic in IDGenerator
Added an internal ensureInit() method to safely initialize g.used before access.

Prevents runtime error:

panic: assignment to entry in nil map

Added Has() method to IDGenerator

Improved ToHTML() robustness

We want to ensure high quality of the packages. Make sure that you've checked the boxes below before sending a pull request.

Not every repository (project) will require every option, but most projects should. Check the Contribution Guidelines for details.

  • The repo documentation has a pkg.go.dev link.
  • The repo documentation has a coverage service link.
  • The repo documentation has a goreportcard link.
  • The repo has a version-numbered release and a go.mod file.
  • The repo has a continuous integration process that automatically runs tests that must pass before new pull requests are merged.
  • Continuous integration is used to attempt to catch issues prior to releasing this package to end-users.

Please provide some links to your package to ease the review

  • forge link (github.com, gitlab.com, etc):
  • pkg.go.dev:
  • goreportcard.com:
  • coverage service link (codecov, coveralls, etc.):

Pull Request content

  • The package has been added to the list in alphabetical order.
  • The package has an appropriate description with correct grammar.
  • [] As far as I know, the package has not been listed here before.

Category quality

Note that new categories can be added only when there are 3 packages or more.

Packages added a long time ago might not meet the current guidelines anymore. It would be very helpful if you could check 3-5 packages above and below your submission to ensure that they also still meet the Quality Standards.

Please delete one of the following lines:

  • The packages around my addition still meet the Quality Standards.
  • I removed the following packages around my addition: (please give a short reason for each removal)

Thanks for your PR, you're awesome! 😎

Summary by CodeRabbit

  • Bug Fixes

    • Improved stability in content processing to prevent rare errors related to ID handling.
    • Ensures internal initialization occurs reliably, reducing the chance of unexpected failures during conversions.
  • Chores

    • Internal improvements to support more robust ID tracking, enhancing overall system reliability without changing user workflows.

@github-actions
Copy link

Automated Quality Checks (from CONTRIBUTING minimum standards)

  • Repo link: missing
  • pkg.go.dev: missing
  • goreportcard: missing
  • coverage: missing

These checks are a best-effort automation and do not replace human review.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 10, 2025

Walkthrough

Introduces an exported Has([]byte) bool method on IDGenerator, adds ensureInit() to lazily initialize the internal used map, and updates Generate and Put to call ensureInit before accessing the map.

Changes

Cohort / File(s) Summary
Markdown ID generator updates
pkg/markdown/convert.go
Added exported Has([]byte) bool to check existence in used. Introduced ensureInit() to initialize used when nil. Updated Generate and Put to call ensureInit() before map access.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant IDGen as IDGenerator
  participant Init as ensureInit()
  participant Map as used map

  Caller->>IDGen: Generate(value)
  IDGen->>Init: ensureInit()
  Init-->>IDGen: used initialized
  IDGen->>Map: read/update entries
  Map-->>IDGen: result
  IDGen-->>Caller: id

  Caller->>IDGen: Put(value)
  IDGen->>Init: ensureInit()
  Init-->>IDGen: used initialized
  IDGen->>Map: insert value
  IDGen-->>Caller: ok

  Caller->>IDGen: Has(value)
  IDGen->>Init: ensureInit()
  Init-->>IDGen: used initialized
  IDGen->>Map: lookup value
  Map-->>IDGen: boolean
  IDGen-->>Caller: exists?
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nudge the map, ensure it’s set,
A hop, a check—no nils to fret.
Has? I peek, with whiskered care,
Put and Generate now play fair.
Ears up high, IDs aligned,
A tidy warren, well-defined. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed Although the title highlights the key fix to initialize the IDGenerator map, its reference to “improve Markdown” is rather broad and does not clearly convey the specific enhancements (such as the new Has() method and updated ToHTML behavior) introduced in this PR.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between df3b620 and 3c5717e.

📒 Files selected for processing (1)
  • pkg/markdown/convert.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Use standard formatting (gofmt) and idiomatic Go style
Favor small, testable functions in Go
Keep exported Go APIs documented with Go-style comments
Maintain ≥80% coverage for non-data packages and ≥90% for data packages when adding new testable Go code

Files:

  • pkg/markdown/convert.go
{**/*.go,.github/scripts/**/*.js}

📄 CodeRabbit inference engine (AGENTS.md)

Use American English spelling in code comments

Files:

  • pkg/markdown/convert.go
🧬 Code graph analysis (1)
pkg/markdown/convert.go (1)
pkg/slug/generator.go (1)
  • Generate (10-14)
🪛 GitHub Actions: tests
pkg/markdown/convert.go

[error] 66-66: compile error: IDGenerator (type) is not an expression

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (3)
pkg/markdown/convert.go (3)

45-50: LGTM! Good defensive initialization pattern.

The ensureInit method correctly prevents nil map panics by lazily initializing the internal map.


54-54: LGTM! Prevents nil map panic.

Calling ensureInit() before accessing the map is the correct approach.


60-60: LGTM! Prevents nil map panic.

Calling ensureInit() before accessing the map is the correct approach.

Comment on lines +64 to +68
// Has checks if the given ID is already used.
func (g *IDGenerator) Has(value []byte) bool {
(g *IDGenerator)
return g.used[util.BytesToReadOnlyString(value)]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Fix compile error and add missing initialization.

Two issues must be fixed:

  1. Line 66 contains invalid syntax (g *IDGenerator) causing a compile error.
  2. The method accesses g.used without calling ensureInit(), risking a nil map panic.

Apply this diff to fix both issues:

 // Has checks if the given ID is already used.
 func (g *IDGenerator) Has(value []byte) bool {
-	(g *IDGenerator)
+	g.ensureInit()
 	return g.used[util.BytesToReadOnlyString(value)]
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Has checks if the given ID is already used.
func (g *IDGenerator) Has(value []byte) bool {
(g *IDGenerator)
return g.used[util.BytesToReadOnlyString(value)]
}
// Has checks if the given ID is already used.
func (g *IDGenerator) Has(value []byte) bool {
g.ensureInit()
return g.used[util.BytesToReadOnlyString(value)]
}
🧰 Tools
🪛 GitHub Actions: tests

[error] 66-66: compile error: IDGenerator (type) is not an expression

🤖 Prompt for AI Agents
In pkg/markdown/convert.go around lines 64 to 68, the Has method contains a
stray invalid token "(g *IDGenerator)" causing a compile error and it reads
g.used without ensuring initialization; remove the stray token and call
g.ensureInit() before accessing g.used, then return the map lookup result (e.g.,
ensure initialization, then return g.used[util.BytesToReadOnlyString(value)]).

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

Verify test coverage for the new Has method.

As per coding guidelines, ensure ≥80% coverage for non-data packages when adding new testable code. Please verify that unit tests exist for the new Has method, including edge cases (empty map, existing ID, non-existing ID).

Run the following script to check for existing tests:


🏁 Script executed:

#!/bin/bash
# Description: Check if tests exist for IDGenerator methods

# Search for test files related to markdown package
fd -e go -g '*_test.go' pkg/markdown

# Search for test functions testing IDGenerator
rg -n 'func.*Test.*IDGenerator' pkg/markdown
rg -n 'func.*Test.*Has' pkg/markdown

# Check overall test coverage (if tests exist)
if fd -e go -g '*_test.go' pkg/markdown | grep -q .; then
  echo "Test files found. Run 'go test -cover ./pkg/markdown' to check coverage."
fi

Length of output: 101


🏁 Script executed:

#!/bin/bash
# Inspect test file for IDGenerator and Has coverage
echo "---------- convert_test.go ----------"
sed -n '1,200p' pkg/markdown/convert_test.go
echo "---------- Searching for Has ----------"
rg -n 'Has' pkg/markdown/convert_test.go || echo "No occurrences of 'Has'"
echo "---------- Searching for IDGenerator ----------"
rg -n 'IDGenerator' pkg/markdown/convert_test.go || echo "No occurrences of 'IDGenerator'"

Length of output: 1477


Add unit tests for IDGenerator.Has
No tests currently cover the new Has method. Add table-driven tests for edge cases (empty generator, existing ID, non-existing ID) to meet ≥80% coverage for this package.

🧰 Tools
🪛 GitHub Actions: tests

[error] 66-66: compile error: IDGenerator (type) is not an expression

🤖 Prompt for AI Agents
In pkg/markdown/convert.go around lines 64 to 68, add unit tests for
IDGenerator.Has in a new or existing test file (pkg/markdown/convert_test.go)
using table-driven tests that cover at least: empty generator (no IDs added),
existing ID (add an ID to the generator.used map using
util.BytesToReadOnlyString on the byte slice), and non-existing ID (check a
different byte slice); for each case create a subtest with clear name, construct
the IDGenerator state, call Has with the byte slice, and assert the returned
bool matches the expected value; run go test and adjust tests until package
coverage for pkg/markdown is ≥80%.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical runtime panic in the IDGenerator by ensuring proper map initialization and adds functionality to check for existing IDs. The changes prevent nil map assignment errors that could crash the Markdown processing.

  • Fixed potential runtime panic by adding lazy initialization for the IDGenerator map
  • Added Has() method to check for existing IDs in the generator
  • Enhanced robustness of the Markdown-to-HTML conversion process


// Has checks if the given ID is already used.
func (g *IDGenerator) Has(value []byte) bool {
(g *IDGenerator)
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid syntax - this appears to be a type cast without an expression. This line should be removed or replaced with g.ensureInit() if initialization is intended.

Suggested change
(g *IDGenerator)

Copilot uses AI. Check for mistakes.

// Has checks if the given ID is already used.
func (g *IDGenerator) Has(value []byte) bool {
(g *IDGenerator)
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Has() method doesn't call ensureInit() before accessing g.used map, which could cause a nil pointer dereference if the map hasn't been initialized yet.

Suggested change
(g *IDGenerator)
g.ensureInit()

Copilot uses AI. Check for mistakes.
Copy link
Owner

avelino commented Oct 17, 2025

Hi @Amitverma0509, the automated checks for this PR failed. Please review the build logs and fix the issues. Specifically, the tests/Running test and PR Quality Checks/quality-check checks failed. Please address these and re-request a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants