Skip to content

Commit 1b6d578

Browse files
authored
fix rag heading from code (#25)
1 parent 5a14870 commit 1b6d578

5 files changed

Lines changed: 42 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 0.9.1 (2025-10-17)
4+
- [Fix] Fixed HeadingTransformer incorrectly treating hash symbols in code blocks as headings.
5+
- Now properly tracks code block boundaries (fenced with ``` or ~~~)
6+
- Skips heading processing for lines inside code blocks
7+
- Prevents Ruby/Python/Shell comments from being interpreted as markdown headings
8+
- Added comprehensive test coverage for code block handling
9+
310
## 0.9.0 (2025-10-17)
411
- [Feature] **No AI Version Detection** - The `compare` command now detects when websites don't serve AI-optimized versions.
512
- Triggers when reduction is <5% (nearly identical content for human and AI User-Agents)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
llm-docs-builder (0.9.0)
4+
llm-docs-builder (0.9.1)
55
zeitwerk (~> 2.6)
66

77
GEM

lib/llm_docs_builder/transformers/heading_transformer.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ def transform(content, options = {})
3838
separator = options[:heading_separator] || ' / '
3939
heading_stack = []
4040
lines = content.lines
41+
in_code_block = false
4142

4243
transformed_lines = lines.map do |line|
44+
# Track code block boundaries (fenced code blocks with ``` or ~~~)
45+
if line.match?(/^```|^~~~/)
46+
in_code_block = !in_code_block
47+
next line
48+
end
49+
50+
# Skip heading processing if inside code block
51+
next line if in_code_block
52+
4353
# Match markdown headings (1-6 hash symbols followed by space and text)
4454
heading_match = line.match(/^(#+)\s+(.+)$/)
4555

lib/llm_docs_builder/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module LlmDocsBuilder
44
# Current version of the LlmDocsBuilder gem
5-
VERSION = '0.9.0'
5+
VERSION = '0.9.1'
66
end

spec/transformers/heading_transformer_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@
145145
expect(result).to include('## API Reference / Authentication & Authorization')
146146
expect(result).to include('### API Reference / Authentication & Authorization / Using API Keys (OAuth2)')
147147
end
148+
149+
it 'ignores hash symbols in code blocks' do
150+
content = <<~MD
151+
# Configuration
152+
## Consumer Settings
153+
154+
```ruby
155+
# This is a Ruby comment
156+
config.timeout = 30
157+
# Another comment
158+
```
159+
160+
### auto_offset_reset
161+
MD
162+
163+
result = transformer.transform(content, options)
164+
165+
expect(result).to include('# Configuration')
166+
expect(result).to include('## Configuration / Consumer Settings')
167+
expect(result).to include('### Configuration / Consumer Settings / auto_offset_reset')
168+
expect(result).to include('# This is a Ruby comment')
169+
expect(result).to include('# Another comment')
170+
end
148171
end
149172

150173
context 'when content has no headings' do

0 commit comments

Comments
 (0)