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

Slot for markdown links #1002

Merged
merged 5 commits into from
May 15, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inseefr/lunatic",
"version": "3.0.1",
"version": "3.0.2",
"description": "Library of questionnaire components",
"type": "module",
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions src/components/shared/HOC/slottableComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import type { CustomCheckboxGroup } from '../../CheckboxGroup/CustomCheckboxGrou
import type { RouterLink } from '../MDLabel/RouterLink';
import type { SummaryResponses, SummaryTitle } from '../../Summary/Summary';
import type { LunaticComponentProps } from '../../type';
import type { MarkdownLink } from '../MDLabel/MarkdownLink';

/**
* Contains the type of every customizable component
Expand Down Expand Up @@ -119,6 +120,7 @@ export type LunaticSlotComponents = {
ComponentWrapper: ComponentType<
PropsWithChildren<LunaticComponentProps & { index: number }>
>;
MarkdownLink: typeof MarkdownLink;
};

const empty = {} as Partial<LunaticSlotComponents> | undefined;
Expand Down
49 changes: 23 additions & 26 deletions src/components/shared/MDLabel/MDLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import type { ComponentProps } from 'react';
import Markdown from 'react-markdown';
import { MDLabelLink } from './MDLabelLink';
import { Fragment, type PropsWithChildren } from 'react';
import Markdown, { type Components } from 'react-markdown';
import { MarkdownLink } from './MarkdownLink';
import remarkBreaks from 'remark-breaks';
import emoji from 'remark-emoji';

type Props = { expression: string };

export const MDLabel = ({ expression }: Props) => {
export function MDLabel({ expression }: Props) {
const hasParagraphs = /\n\n/.test(expression);
const components = {
p: hasParagraphs ? 'p' : Fragment,
br: 'br',
a: MarkdownA,
} as Partial<Components>;
return (
<Markdown
components={renderComponentsFor(expression)}
components={components}
remarkPlugins={[[emoji, { accessible: true }], remarkBreaks]}
>
{expression}
</Markdown>
);
};

const renderComponentsFor = (
expression: string
): ComponentProps<typeof Markdown>['components'] => {
const components = {
p: (props) => <>{props.children}</>,
br: 'br',
a: (props) => (
<MDLabelLink {...({ ...props } as ComponentProps<typeof MDLabelLink>)} />
),
} satisfies ComponentProps<typeof Markdown>['components'];

if (/\n\n/.test(expression)) {
return {
...components,
p: 'p',
};
}

return components;
}
const MarkdownA = ({
title,
href,
children,
}: PropsWithChildren<{ title?: string; href: string }>) => {
const tooltip = title ? <MDLabel expression={title} /> : null;
return (
<MarkdownLink href={href} tooltip={tooltip}>
{children}
</MarkdownLink>
);
};
59 changes: 0 additions & 59 deletions src/components/shared/MDLabel/MDLabelLink.tsx

This file was deleted.

43 changes: 43 additions & 0 deletions src/components/shared/MDLabel/MarkdownLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useId, type PropsWithChildren, type ReactNode } from 'react';
import { Tooltip } from 'react-tooltip';
import { RouterLink } from './RouterLink';
import { slottableComponent } from '../HOC/slottableComponent';

type Props = PropsWithChildren<{ href: string; tooltip?: ReactNode }>;

export const MarkdownLink = slottableComponent<Props>(
'MarkdownLink',
({ href, children, tooltip }) => {
const id = useId();

const extraProps = tooltip
? { 'data-tooltip-id': `tooltip-${id}`, className: 'link-md' }
: {};
const isAbsoluteLink = href.trim().startsWith('/');

return (
<>
{isAbsoluteLink ? (
<RouterLink to={href} id={id} {...extraProps}>
{children}
</RouterLink>
) : (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
id={id}
{...extraProps}
>
{children}
</a>
)}
{tooltip && (
<Tooltip className="tooltip-content" id={`tooltip-${id}`}>
{tooltip}
</Tooltip>
)}
</>
);
}
);
Empty file.
17 changes: 8 additions & 9 deletions src/stories/utils/orchestrator.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import './custom-lunatic.scss';
import './orchestrator.scss';

import { useLunatic, Button, components, ModalControls } from '../..';
import React, { memo, useCallback, useState, useEffect } from 'react';
import {
Button,
components,
LunaticComponents,
ModalControls,
useLunatic,
} from '../..';
import React, { memo, useCallback, useEffect, useState } from 'react';

import { Logger } from '../../utils/logger';
import { Overview } from './overview';
import Waiting from './waiting';
import { LunaticComponents } from '../..';
import { SchemaValidator } from './SchemaValidator.jsx';

const Input = components.Input;
Expand Down Expand Up @@ -247,11 +251,6 @@ function OrchestratorForStories({
isCritical={errorsForModal.isCritical}
/>
)}
<Waiting status={waiting}>
<div className="waiting-orchestrator">
Initialisation des données de suggestion...
</div>
</Waiting>
</aside>
</div>
</Provider>
Expand Down
1 change: 0 additions & 1 deletion src/stories/utils/waiting/index.js

This file was deleted.

1 change: 0 additions & 1 deletion src/stories/utils/waiting/preloader.svg

This file was deleted.

21 changes: 0 additions & 21 deletions src/stories/utils/waiting/waiting.jsx

This file was deleted.

21 changes: 0 additions & 21 deletions src/stories/utils/waiting/waiting.scss

This file was deleted.

Loading