-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
159d135
commit 84d21bf
Showing
15 changed files
with
806 additions
and
151 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
76 changes: 76 additions & 0 deletions
76
src/components/MarkdownComponent/MarkdownComponent.module.scss
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,76 @@ | ||
@use '../../styles/variables'; | ||
@use '../../styles/theme'; | ||
|
||
.markdown { | ||
h1, | ||
h2, | ||
h3, | ||
h4, | ||
h5, | ||
h6 { | ||
margin: 0 0 1em; | ||
font-weight: bold; | ||
line-height: 1.33; | ||
} | ||
|
||
h1 { | ||
padding-bottom: 0.3em; | ||
font-size: 2em; | ||
border-bottom: 1px solid rgba(variables.$white, 0.12); | ||
} | ||
|
||
h2 { | ||
padding-bottom: 0.3em; | ||
font-size: 1.5em; | ||
border-bottom: 1px solid rgba(variables.$white, 0.12); | ||
} | ||
|
||
h3 { | ||
font-size: 1.25em; | ||
} | ||
|
||
h4 { | ||
font-size: 1em; | ||
} | ||
|
||
h5 { | ||
font-size: 0.875em; | ||
} | ||
|
||
h6 { | ||
font-size: 0.85em; | ||
} | ||
|
||
hr { | ||
box-sizing: content-box; | ||
height: 0.25em; | ||
margin: 24px 0; | ||
padding: 0; | ||
overflow: hidden; | ||
background: transparent; | ||
background-color: rgba(variables.$white, 0.12); | ||
border: 0; | ||
} | ||
|
||
img { | ||
box-sizing: content-box; | ||
max-width: 100%; | ||
background-color: theme.$secondary-color; | ||
} | ||
|
||
a, | ||
a:visited { | ||
color: theme.$link-color; | ||
text-decoration: none; | ||
|
||
&:hover { | ||
text-decoration: underline; | ||
} | ||
} | ||
|
||
ul, | ||
ol, | ||
p { | ||
margin: 1.2em 0; | ||
} | ||
} |
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,37 +1,37 @@ | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import ReactMarkdown from 'react-markdown'; | ||
|
||
const MARKDOWN_LINK_REGEX = /\[([^[]+)]\(((https?:\/\/|www\.)?[^)]+)\)/gi; | ||
const MARKDOWN_ITALIC_REGEX = /(?:\*|_)(\S[\s\S]*?)(?:\*|_)/gi; | ||
const MARKDOWN_STRONG_REGEX = /(?:\*{2}|_{2})(\S[\s\S]*?)(?:\*{2}|_{2})/gi; | ||
const MARKDOWN_ITALIC_STRONG_REGEX = /(?:\*{3}|_{3})(\S[\s\S]*?)(?:\*{3}|_{3})/gi; | ||
const MARKDOWN_HEADERS_REGEX = /^(#{1,6})(.*)$/gm; | ||
const LINEBREAK_REGEX = /(?:\r\n|\r|\n)/g; | ||
import styles from './MarkdownComponent.module.scss'; | ||
|
||
type Props = { | ||
markdownString: string; | ||
className?: string; | ||
disallowedElements?: string[]; | ||
}; | ||
|
||
const parseMarkdown = (value: string): string => | ||
value | ||
.replace(/<[^>]+>/gm, '') // remove html should run first | ||
.replace(MARKDOWN_HEADERS_REGEX, (...[, header, title]) => { | ||
return '<h' + header.length + '>' + title.trim() + '</h' + header.length + '>'; | ||
}) | ||
.replace(MARKDOWN_ITALIC_STRONG_REGEX, (...[, word]) => `<strong><em>${word}</em></strong>`) | ||
.replace(MARKDOWN_STRONG_REGEX, (...[, word]) => `<strong>${word}</strong>`) | ||
.replace(MARKDOWN_ITALIC_REGEX, (...[, word]) => `<em>${word}</em>`) | ||
.replace(MARKDOWN_LINK_REGEX, (...[, word, link]) => { | ||
const externalLink = /^(https?|www\.|\/\/)/.test(link); | ||
const target = externalLink ? ' target="_blank"' : ''; | ||
const rel = externalLink ? ' rel="noopener"' : ''; | ||
|
||
return `<a href="${link}"${target}${rel}>${word}</a>`; | ||
}) | ||
.replace(LINEBREAK_REGEX, '<br />'); // linebreak formatter should run last | ||
|
||
const MarkdownComponent: React.FC<Props> = ({ markdownString, className }: Props) => { | ||
return <div className={className} dangerouslySetInnerHTML={{ __html: parseMarkdown(markdownString) }} />; | ||
const MarkdownComponent: React.FC<Props> = ({ markdownString, className, disallowedElements }: Props) => { | ||
return ( | ||
<ReactMarkdown | ||
className={classNames(styles.markdown, className)} | ||
components={{ | ||
a: ({ node, href, children, ...props }) => { | ||
const externalLink = /^(https?|www\.|\/\/)/.test(href || ''); | ||
const target = externalLink ? '_blank' : undefined; | ||
const rel = externalLink ? 'noopener' : undefined; | ||
return ( | ||
<a {...props} href={href} target={target} rel={rel}> | ||
{children} | ||
</a> | ||
); | ||
}, | ||
}} | ||
disallowedElements={disallowedElements} | ||
unwrapDisallowed | ||
> | ||
{markdownString} | ||
</ReactMarkdown> | ||
); | ||
}; | ||
|
||
export default MarkdownComponent; |
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
flex: 1; | ||
} | ||
|
||
.footer { | ||
div.footer { | ||
padding: 20px 40px; | ||
line-height: 18px; | ||
letter-spacing: 0.15px; | ||
|
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 |
---|---|---|
@@ -1,37 +1,10 @@ | ||
@use '../../styles/variables'; | ||
@use '../../styles/theme'; | ||
|
||
.about { | ||
div.about { | ||
max-width: 960px; | ||
margin: 24px auto; | ||
padding: 16px; | ||
font-size: 16px; | ||
line-height: 1.4em; | ||
background-color: rgba(variables.$white, 0.1); | ||
|
||
h1, h2, h3, h4, h5, h6 { | ||
font-weight: bold; | ||
} | ||
|
||
h1 { | ||
font-size: 24px; | ||
} | ||
|
||
h2 { | ||
font-size: 20px; | ||
} | ||
|
||
h3 { | ||
font-size: 18px; | ||
} | ||
|
||
a, | ||
a:visited { | ||
color: theme.$link-color; | ||
text-decoration: none; | ||
|
||
&:hover { | ||
text-decoration: underline; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.