Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
feat: markdown support added
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 2, 2020
1 parent 9114fbe commit 0913ba1
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 54 deletions.
110 changes: 76 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@svgr/webpack": "^5.1.0",
"@types/react": "^16.9.19",
"@types/react-dom": "^16.9.5",
"@types/showdown": "^1.9.3",
"@types/styled-components": "^4.4.2",
"@types/twemoji": "^12.1.0",
"@types/uuid": "^3.4.7",
Expand Down Expand Up @@ -66,6 +67,7 @@
"react-apollo": "^3.1.3",
"react-dom": "^16.12.0",
"react-feather": "^2.0.3",
"showdown": "^1.9.1",
"styled-components": "^5.0.0",
"uuid": "^3.4.0"
},
Expand Down
15 changes: 9 additions & 6 deletions src/Chat/components/Emojify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { parse } from 'twemoji'
import styled from 'styled-components'

const Twemoji = styled.span`
export const WithTwemoji = styled.span`
img.emoji {
height: 1em;
width: 1em;
Expand All @@ -11,13 +11,16 @@ const Twemoji = styled.span`
}
`

export const convert = (text: string) =>
parse(text, {
folder: 'svg',
ext: '.svg',
})

export const emojify = (text: string) => (
<Twemoji
<WithTwemoji
dangerouslySetInnerHTML={{
__html: parse(text, {
folder: 'svg',
ext: '.svg',
}),
__html: convert(text),
}}
/>
)
39 changes: 39 additions & 0 deletions src/Chat/components/Markdownify.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as React from 'react'
import { Converter } from 'showdown'
import { convert, WithTwemoji } from './Emojify'
import styled from 'styled-components'

// See https://github.com/showdownjs/showdown#valid-options
const converter = new Converter({
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
strikethrough: true,
ghCodeBlocks: false,
simpleLineBreaks: true,
openLinksInNewWindow: true,
})

const FromMarkdown = styled(WithTwemoji)`
p {
padding: 0.5rem;
margin: 0;
}
code {
color: inherit;
padding: 0.15rem;
border-radius: 4px;
font-weight: bold;
text-shadow: 1px 1px 2px #000000dd;
}
img {
max-width: 100%;
}
`

export const markdownify = (text: string) => (
<FromMarkdown
dangerouslySetInnerHTML={{
__html: converter.makeHtml(convert(text)),
}}
/>
)
Loading

0 comments on commit 0913ba1

Please sign in to comment.