diff --git a/CHANGELOG.md b/CHANGELOG.md index 740a33ed..ba843941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/components/gherkin/Description.spec.tsx b/src/components/gherkin/Description.spec.tsx index dc793306..e7c50682 100644 --- a/src/components/gherkin/Description.spec.tsx +++ b/src/components/gherkin/Description.spec.tsx @@ -39,4 +39,27 @@ So that I can be happy` '

As a user
\nI want to do stuff
\nSo that I can be happy

' ) }) + + it('removes common indentation carried over from the source', () => { + const description = ` Beware that some formatters + use the _media type_. + + ## Notes` + const { container, getByRole } = render() + expect(container.querySelector('pre')).to.be.null + expect(container).to.contain.html( + '

Beware that some formatters
\nuse the media type.

' + ) + 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() + expect(container).to.contain.html('

Here is some art:

') + expect(container.querySelector('pre')?.textContent).to.eq(' /\\_/\\\n ( o.o )\n') + }) }) diff --git a/src/components/gherkin/Description.tsx b/src/components/gherkin/Description.tsx index 8b922565..f6a7b3d2 100644 --- a/src/components/gherkin/Description.tsx +++ b/src/components/gherkin/Description.tsx @@ -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 = ({ description, styles, @@ -18,7 +28,7 @@ const DefaultRenderer: DefaultComponent = } return (
- +
) }