Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASL-4380] Markdown fixes to display nested lists #323

Merged
merged 4 commits into from
Feb 7, 2024
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
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
Loading