Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- Strip common indentation from descriptions ([#586](https://github.com/cucumber/react-components/pull/586))

## [24.5.0] - 2026-09-28
### Added
Expand Down
23 changes: 23 additions & 0 deletions src/components/gherkin/Description.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@ So that I can be happy`
'<p>As a user<br>\nI want to do stuff<br>\nSo that I can be happy</p>'
)
})

it('removes common indentation carried over from the source', () => {
const description = ` Beware that some formatters
use the _media type_.

## Notes`
const { container, getByRole } = render(<Description description={description} />)
expect(container.querySelector('pre')).to.be.null
expect(container).to.contain.html(
'<p>Beware that some formatters<br>\nuse the <em>media type</em>.</p>'
)
expect(getByRole('heading', { name: 'Notes' })).to.be.visible
})

it('preserves relative indentation within the description', () => {
const description = ` Here is some art:

/\\_/\\
( o.o )`
const { container } = render(<Description description={description} />)
expect(container).to.contain.html('<p>Here is some art:</p>')
expect(container.querySelector('pre')?.textContent).to.eq(' /\\_/\\\n ( o.o )\n')
})
})
12 changes: 11 additions & 1 deletion src/components/gherkin/Description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {
} from '../customise/index.js'
import defaultStyles from './Description.module.scss'

// Gherkin keeps the source file's indentation, which Markdown would otherwise treat as code blocks
function dedent(text: string): string {
const lines = text.split('\n')
const indents = lines
.filter((line) => line.trim() !== '')
.map((line) => line.match(/^[ \t]*/)?.[0].length ?? 0)
const common = Math.min(...indents)
return lines.map((line) => line.slice(common)).join('\n')
}

const DefaultRenderer: DefaultComponent<DescriptionProps, DescriptionClasses> = ({
description,
styles,
Expand All @@ -18,7 +28,7 @@ const DefaultRenderer: DefaultComponent<DescriptionProps, DescriptionClasses> =
}
return (
<div className={styles.content}>
<HighLight text={description} markdown={true} />
<HighLight text={dedent(description)} markdown={true} />
</div>
)
}
Expand Down