-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add subject label to item descriptions
This change required a markdown parser to be able to inject the label below any paragraphs that contain only media (images, videos, audio) and above the first paragraph that contains textual content. Also noticed that the `Banners` component was incorrectly placed in a scrollable container, making banner alerts (such as validation errors) not appear to users who have scrolled down on an item that has a long description.
- Loading branch information
1 parent
25ecf9e
commit bfc5b43
Showing
9 changed files
with
97 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { SubjectDTO } from '~/shared/api/types/subject'; | ||
import { Box } from '~/shared/ui'; | ||
import { TargetSubjectLabel } from '~/widgets/TargetSubjectLabel'; | ||
|
||
type Props = { | ||
subject: SubjectDTO | null; | ||
}; | ||
|
||
export const TargetSubjectLine = ({ subject }: Props) => { | ||
if (!subject) return null; | ||
|
||
return ( | ||
<Box | ||
sx={{ | ||
'&:not(:first-child)': { mt: 4.8 }, | ||
mb: 1.2, | ||
}} | ||
> | ||
<TargetSubjectLabel subject={subject} /> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
import ReactMarkdown from 'react-markdown'; | ||
import { ReactMarkdownOptions } from 'react-markdown/lib/react-markdown'; | ||
import rehypeRaw from 'rehype-raw'; | ||
import remarkGfm from 'remark-gfm'; | ||
|
||
import { useMarkdownExtender } from './lib/useMarkdownExtender'; | ||
|
||
import './style.css'; | ||
import { useCustomTranslation } from '~/shared/utils'; | ||
|
||
interface MarkdownProps { | ||
type MarkdownProps = Omit<ReactMarkdownOptions, 'children'> & { | ||
markdown: string; | ||
} | ||
}; | ||
|
||
export const Markdown = (props: MarkdownProps) => { | ||
const { isLoading, markdown } = useMarkdownExtender(props.markdown); | ||
export const Markdown = ({ markdown: markdownProp, ...rest }: MarkdownProps) => { | ||
const { t } = useCustomTranslation(); | ||
const { isLoading, markdown } = useMarkdownExtender(markdownProp); | ||
|
||
if (isLoading) { | ||
return <div>Loading...</div>; | ||
return <div>{t('loading')}</div>; | ||
} | ||
|
||
return ( | ||
<div id="markdown-wrapper" data-testid="markdown"> | ||
<ReactMarkdown rehypePlugins={[rehypeRaw, remarkGfm]}>{markdown}</ReactMarkdown> | ||
<ReactMarkdown rehypePlugins={[rehypeRaw, remarkGfm]} {...rest}> | ||
{markdown} | ||
</ReactMarkdown> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Inserts the given string into the given markdown after the first line containing content | ||
* that does not contain solely media. | ||
* | ||
* @param markdown Markdown content to parse | ||
* @param inserted Inserted string | ||
* @returns Processed markdown with string inserted | ||
*/ | ||
export const insertAfterMedia = (markdown: string, inserted: string) => { | ||
const lines = markdown.split('\n'); | ||
// Stop at first line containing content that is not: | ||
// - solely a media element (<img>/<video>/<audio>/) | ||
// - a media element wrapped in an alignment block (::: hljs-* ... :::) | ||
let i: number; | ||
for (i = 0; i < lines.length; i++) { | ||
const lineWithoutMedia = lines[i] | ||
.replaceAll(/^:::\s+hljs-\S+|:::$|<(img|video|audio)[^>]+>|!\[[^\]]*\]\([^)]*\)/gi, '') | ||
.trim(); | ||
if (lineWithoutMedia) break; | ||
} | ||
|
||
if (i === lines.length) { | ||
// Append string to bottom if no lines containing non-media content were found | ||
lines.push(inserted); | ||
} else { | ||
// Else insert string before line containing non-media content | ||
lines.splice(i, 0, inserted); | ||
} | ||
|
||
return lines.join('\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters