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
25 changes: 25 additions & 0 deletions client/src/components/MarkdownViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import ReactMarkdown from 'react-markdown';
import rehypeExternalLinks from 'rehype-external-links';
import remarkBreaks from 'remark-breaks';

interface Props {
children: string;
}

export default function MarkdownViewer({ children }: Props) {
return (
<ReactMarkdown
rehypePlugins={[
() =>
rehypeExternalLinks({
target: '_blank',
rel: 'noopener noreferrer',
}),
]}
remarkPlugins={[remarkBreaks]}
>
{children}
</ReactMarkdown>
);
}
12 changes: 11 additions & 1 deletion client/src/components/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,17 @@ interface Props {
* @returns Draft.js editor state
*/
function markdownToEditorState(markdown: string) {
const rawData = markdownToDraft(markdown);
const rawData = markdownToDraft(markdown, {
remarkableOptions: { html: true },
});
// Set target="_blank" for all link entities
if (rawData.entityMap) {
Object.values(rawData.entityMap).forEach((entity: any) => {
if (entity.type === 'LINK') {
entity.data.targetOption = '_blank';
}
});
}
const contentState = convertFromRaw(rawData);
return EditorState.createWithContent(contentState);
}
Expand Down
7 changes: 2 additions & 5 deletions client/src/components/SectionInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
} from '@mui/material';
import { useTranslations } from '@src/stores/TranslationContext';
import React, { forwardRef, useId, useImperativeHandle, useState } from 'react';
import ReactMarkdown from 'react-markdown';
import rehypeExternalLinks from 'rehype-external-links';
import MarkdownViewer from './MarkdownViewer';

interface Props {
subject: string;
Expand Down Expand Up @@ -56,9 +55,7 @@ export default forwardRef(function SectionInfo(
open={infoDialogOpen}
>
<DialogContent id={`${dialogId}-dialog-content`}>
<ReactMarkdown rehypePlugins={[rehypeExternalLinks]}>
{infoText}
</ReactMarkdown>
<MarkdownViewer>{infoText}</MarkdownViewer>
</DialogContent>
<DialogActions>
<Button
Expand Down
15 changes: 7 additions & 8 deletions client/src/components/SurveyThanksPage.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Survey } from '@interfaces/survey';
import {
Box,
Link,
Typography,
Stack,
Theme,
Box,
Typography,
useMediaQuery,
Stack,
} from '@mui/material';
import { useTranslations } from '@src/stores/TranslationContext';
import { useImageHeaderQuery } from '@src/utils/useImageHeaderQuery';
import React, { useEffect, useState } from 'react';
import ReactMarkdown from 'react-markdown';
import rehypeExternalLinks from 'rehype-external-links';
import Footer from './Footer';
import { useImageHeaderQuery } from '@src/utils/useImageHeaderQuery';
import MarkdownViewer from './MarkdownViewer';

const styles = (theme: Theme) => ({
testSurveyHeader: {
Expand Down Expand Up @@ -128,9 +127,9 @@ export default function SurveyThanksPage({ survey, isTestSurvey }: Props) {
<Typography variant="h5" component="h1">
{survey.thanksPage.title?.[surveyLanguage]}
</Typography>
<ReactMarkdown rehypePlugins={[rehypeExternalLinks]}>
<MarkdownViewer>
{survey.thanksPage.text?.[surveyLanguage]}
</ReactMarkdown>
</MarkdownViewer>
</div>
{survey.thanksPage.imageName && (
<Box
Expand Down
13 changes: 3 additions & 10 deletions client/src/components/TextSection.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { SurveyTextSection } from '@interfaces/survey';
import { Typography } from '@mui/material';
import { useSurveyAnswers } from '@src/stores/SurveyAnswerContext';
import { useTranslations } from '@src/stores/TranslationContext';
import React from 'react';
import ReactMarkdown from 'react-markdown';
import rehypeExternalLinks from 'rehype-external-links';
import remarkBreaks from 'remark-breaks';
import MarkdownViewer from './MarkdownViewer';
import SectionInfo from './SectionInfo';
import { Typography } from '@mui/material';

interface Props {
section: SurveyTextSection;
Expand Down Expand Up @@ -42,12 +40,7 @@ export default function TextSection({ section, isFollowUp = false }: Props) {
)}
</div>
<div style={{ color: section.bodyColor ?? '#000000' }}>
<ReactMarkdown
rehypePlugins={[rehypeExternalLinks]}
remarkPlugins={[remarkBreaks]}
>
{section.body?.[surveyLanguage]}
</ReactMarkdown>
<MarkdownViewer>{section.body?.[surveyLanguage]}</MarkdownViewer>
</div>
</>
);
Expand Down