Skip to content

Commit b42fae6

Browse files
authored
fix: Markdown README issues (#507)
1 parent 3260636 commit b42fae6

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

src/components/Markdown.tsx

+40-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ReactMarkdown from 'react-markdown'
44
import rehypeRaw from 'rehype-raw'
55
import styled from 'styled-components'
66

7+
import { isExternalUrl, removeTrailingSlashes } from '../utils/urls'
8+
79
import MultilineCode from './Code'
810
import InlineCode from './InlineCode'
911

@@ -42,8 +44,10 @@ function getLastStringChild(children: any, depth = 0): any {
4244
function MarkdownPreformatted({ children, ...props }: any) {
4345
let lang
4446

45-
if (children.props?.className?.startsWith('lang-')) {
46-
lang = children.props.className.slice(5)
47+
const className = children?.[0]?.props?.className
48+
49+
if (className && typeof className === 'string') {
50+
lang = /language-(\w+)/.exec(className)?.[1] || ''
4751
}
4852
const stringChild = getLastStringChild(children) || ''
4953

@@ -170,9 +174,9 @@ function MarkdownImage({
170174
...props
171175
}: any) {
172176
// Convert local image paths to full path on github
173-
// Only works if primary git branch is named "master"
174-
if (gitUrl && src && !src.match(/^https*/)) {
175-
src = `${gitUrl}/raw/${mainBranch}/${src}`
177+
if (gitUrl && src && !isExternalUrl(src)) {
178+
src = src.replace(/^\//, '')
179+
src = `${removeTrailingSlashes(gitUrl)}/raw/${mainBranch}/${src}`
176180
}
177181

178182
return (
@@ -186,6 +190,33 @@ function MarkdownImage({
186190
)
187191
}
188192

193+
function MarkdownLink({
194+
href,
195+
gitUrl,
196+
mainBranch,
197+
...props
198+
}: {
199+
href: string
200+
gitUrl: string
201+
mainBranch: string
202+
}) {
203+
// Convert local readme hrefs to full path on github
204+
if (gitUrl && href && !isExternalUrl(href)) {
205+
// Remove potential starting slash
206+
href = href.replace(/^\//, '')
207+
href = `${removeTrailingSlashes(gitUrl)}/blob/${mainBranch}/${href}`
208+
}
209+
210+
return (
211+
<MdA
212+
{...props}
213+
target="_blank"
214+
rel="noopener noreferrer"
215+
href={href}
216+
/>
217+
)
218+
}
219+
189220
function Markdown({ text, gitUrl, mainBranch }: MarkdownProps) {
190221
return useMemo(
191222
() => (
@@ -208,7 +239,10 @@ function Markdown({ text, gitUrl, mainBranch }: MarkdownProps) {
208239
}),
209240
p: render({ component: MdP }),
210241
div: render({ component: MdDiv }),
211-
a: render({ component: MdA, props: { target: '_blank' } }),
242+
a: render({
243+
component: MarkdownLink,
244+
props: { gitUrl, mainBranch },
245+
}),
212246
span: render({ component: MdSpan }),
213247
code: render({ component: InlineCode }),
214248
pre: render({

src/stories/Markdown.stories.tsx

+23-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const markdown = `# Plural Console
1010
</div>
1111
</div>
1212
13+
![Console](/assets/public/PluralConsole-background.png)
14+
1315
# H1 - Some un-ordered lists
1416
1517
* Reception of over-the-air application updates
@@ -47,14 +49,33 @@ Lorem <i>italic</i> dolor sit <em>em</em>, consectetur adipiscing elit, sed do e
4749
4850
> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
4951
50-
###### H6 - A code block
52+
###### H6 - A code block without a language
5153
52-
\`\`\`sh
54+
\`\`\`
5355
brew install erlang@23
5456
cp -r /opt/homebrew/opt/erlang@23/lib/erlang ~/.asdf/installs/erlang/23.1.5
5557
asdf reshim erlang 23.1.5
5658
\`\`\`
5759
60+
###### H6 - A code block with a language
61+
62+
\`\`\`javascript
63+
function sum(x,y) {
64+
return x + y;
65+
}
66+
67+
const num1 = 12;
68+
const num2 = 24;
69+
70+
console.log(sum(num1, num2));
71+
\`\`\`
72+
73+
###### Links
74+
[Absolute url](https://google.com)
75+
[Absolute url - no protocol](//google.com)
76+
[Relative url – Console Readme](README.md)
77+
[Root relative url – Console Security](/SECURITY.md)
78+
5879
___
5980
6081
# Some more stuff from Chatwoot's readme

0 commit comments

Comments
 (0)