Skip to content

Commit

Permalink
[ASL-4380] Markdown fixes to display nested lists (#323)
Browse files Browse the repository at this point in the history
* [ASL-4380] Markdown fixes to display nested lists
- Remove trimming of whitespace at the start of lines
- Use remarkBreaks rather than styling it using pre

* [ASL-4380] Markdown fixes to display nested lists
- Remove trimming of whitespace at the start of lines
- Use remarkBreaks rather than styling it using pre

* [ASL-4380] Fix test whitespace
  • Loading branch information
jeff-horton-ho-sas authored Feb 7, 2024
1 parent 240d99e commit 2794938
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 73 deletions.
106 changes: 48 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ukhomeoffice/asl-components",
"version": "12.1.0",
"version": "13.0.0",
"description": "React components for ASL layouts and elements",
"main": "src/index.jsx",
"styles": "styles/index.scss",
Expand Down Expand Up @@ -39,6 +39,7 @@
"qs": "^6.6.1",
"react": "^16.9.0",
"react-markdown": "^6.0.2",
"remark-breaks": "^2.0.2",
"react-redux": "^7.1.0",
"redux": "^4.0.1",
"shasum": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/conditions/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function Conditions({
{
conditions &&
<Fragment>
<Inset className="condition"><Markdown>{ tempCondition }</Markdown></Inset>
<Inset className="condition"><Markdown significantLineBreaks>{ tempCondition }</Markdown></Inset>
<ConditionReminders reminders={ tempReminders } />
</Fragment>
}
Expand Down
21 changes: 11 additions & 10 deletions src/markdown/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable react/display-name */
import React, { Fragment } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkBreaks from 'remark-breaks';

function RenderLink({ href, children }) {
return <Fragment>[{ children }]({ href })</Fragment>;
Expand All @@ -14,13 +15,6 @@ const components = {
linkReference: RenderLinkReference
};

const trim = str => {
if (typeof str === 'string') {
return str.split('\n').map(s => s.trim()).join('\n').trim();
}
return str;
};

// eslint-disable-next-line no-unused-vars
const wrapInSpanIfOnlyChild = enabled => ({ node, siblingCount, index, ...props }) => {
if (enabled && siblingCount === 1) {
Expand All @@ -29,16 +23,23 @@ const wrapInSpanIfOnlyChild = enabled => ({ node, siblingCount, index, ...props
return <p {...props} />;
};

export default function Markdown({ children, links = false, unwrapSingleLine = false, source, ...props }) {

export default function Markdown({
children,
links = false,
unwrapSingleLine = false,
significantLineBreaks = false,
source,
...props
}) {
return <ReactMarkdown
includeElementIndex={true}
components={{
...(!links && components),
p: wrapInSpanIfOnlyChild(unwrapSingleLine)
}}
remarkPlugins={significantLineBreaks ? [remarkBreaks] : []}
{...props}
>
{ trim(source || children) }
{ source || children }
</ReactMarkdown>;
}
6 changes: 3 additions & 3 deletions src/snippet/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ describe('<Snippet />', () => {
const content = {
string: 'one line',
list: `* one
* two
* three`,
* two
* three`,
paragraphs: `one
two`
two`
};

test('does not include a wrapping element on single line input', () => {
Expand Down

0 comments on commit 2794938

Please sign in to comment.