Skip to content

Commit 3173949

Browse files
committed
Fix the single image not visible issue, remove the hard coded HTML tags
1 parent 524e254 commit 3173949

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

src-ts/tools/dev-center/dev-center-lib/MarkdownDoc/MarkdownCode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function LineNumbers(props: LineNumbersProps): React.ReactElement | null {
109109
return () => {
110110
clearTimeout(debounceTimer.current)
111111
}
112-
}, [size.width, onVisibilityChange, codeRef.current])
112+
}, [size.width, onVisibilityChange, codeRef])
113113

114114
if (!showLineNumbers) {
115115
// tslint:disable-next-line no-null-keyword

src-ts/tools/dev-center/dev-center-lib/MarkdownDoc/MarkdownImages.module.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@
5757
}
5858
}
5959

60+
.imageBlock {
61+
position: relative;
62+
margin: auto;
63+
overflow: hidden;
64+
background-color: $black-5;
65+
border-radius: 8px;
66+
text-align: center;
67+
}
68+
6069
.imageContainer {
6170
max-width: 481px;
71+
margin: auto;
6272
}
6373

6474
@include ltelg {

src-ts/tools/dev-center/dev-center-lib/MarkdownDoc/MarkdownImages.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const MarkdownImages: React.FC<MarkdownImagesProps> = ({
8585
</div>
8686
)
8787
}
88-
return (
88+
return children.length > 1 ? (
8989
// @ts-ignore
9090
<Carousel
9191
itemsToShow={1}
@@ -104,6 +104,12 @@ const MarkdownImages: React.FC<MarkdownImagesProps> = ({
104104
</div>
105105
))}
106106
</Carousel>
107+
) : (
108+
<>
109+
<div key={`md-image-single`} className={`${styles['imageBlock']}}`}>
110+
<div className={styles['imageContainer']}>{children[0]}</div>
111+
</div>
112+
</>
107113
)
108114
}
109115

src-ts/tools/dev-center/dev-center-lib/MarkdownDoc/TableOfContents.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ function useOnScroll({ onScroll }: { onScroll: () => void }): void {
6565
const debounceTimer: React.MutableRefObject<
6666
ReturnType<typeof setTimeout> | undefined
6767
> = React.useRef<ReturnType<typeof setTimeout>>()
68-
const handleScroll: () => void = () => {
69-
if (debounceTimer.current) {
70-
clearTimeout(debounceTimer.current)
71-
debounceTimer.current = undefined
68+
React.useEffect(() => {
69+
const handleScroll: () => void = () => {
70+
if (debounceTimer.current) {
71+
clearTimeout(debounceTimer.current)
72+
debounceTimer.current = undefined
73+
}
74+
debounceTimer.current = setTimeout(() => {
75+
debounceTimer.current = undefined
76+
onScroll()
77+
}, 1)
7278
}
73-
debounceTimer.current = setTimeout(() => {
74-
debounceTimer.current = undefined
75-
onScroll()
76-
}, 1)
77-
}
7879

79-
React.useEffect(() => {
8080
onScroll()
8181
window.addEventListener('scroll', handleScroll)
8282
return () => {

src-ts/tools/dev-center/dev-center-lib/MarkdownDoc/markdownRenderer/renderer.tsx

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ export type MarkdownString = string
1414
export type MarkdownResult = React.ReactNode
1515
export type TOC = Array<{ headingId: string; level: number; title: string }>
1616

17+
enum MarkdownHeaderTag {
18+
h1 = 'h1',
19+
h2 = 'h2',
20+
h3 = 'h3',
21+
h4 = 'h4',
22+
h5 = 'h5',
23+
h6 = 'h6',
24+
}
25+
26+
enum MarkdownParagraphTag {
27+
p = 'p',
28+
}
1729
export interface MarkdownRenderOptions {
1830
baseUrl?: string
19-
groupBy?: 'h2'
31+
groupBy?: MarkdownHeaderTag.h2
2032
highlightCode?: (code: string, lang: string) => string
2133
sanitize?: boolean
2234
toc?: TOC
@@ -102,7 +114,7 @@ export class Renderer implements MarkdownRenderer {
102114
const isH1Tag: (tagName: keyof JSX.IntrinsicElements) => boolean = (
103115
tagName: keyof JSX.IntrinsicElements
104116
) => {
105-
return tagName === 'h1'
117+
return tagName === MarkdownHeaderTag.h1
106118
}
107119
const isGroupByTag: (
108120
tagName: keyof JSX.IntrinsicElements
@@ -196,7 +208,7 @@ export class Renderer implements MarkdownRenderer {
196208
t.tokens
197209
.filter((child) => !isLineBreak(child))
198210
.every((child) => child.type === 'image') &&
199-
t.tokens.filter((child) => !isLineBreak(child)).length >= 2
211+
t.tokens.filter((child) => !isLineBreak(child)).length >= 1
200212
) {
201213
return true
202214
}
@@ -334,20 +346,21 @@ export class Renderer implements MarkdownRenderer {
334346
}
335347

336348
const tag: string = extractTag(html)
337-
if (
338-
tag &&
339-
['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'].indexOf(tag) !== -1
340-
) {
341-
let id: string | undefined
342-
if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].indexOf(tag) !== -1) {
343-
token = token as marked.Tokens.Heading
344-
id = extractId(html, `h${token.depth}`).trim()
349+
if (tag) {
350+
const isParagraphTag: boolean = tag === MarkdownParagraphTag.p
351+
const isHeaderTag: boolean = Object.values(MarkdownHeaderTag).indexOf(tag as MarkdownHeaderTag) !== -1
352+
if (isParagraphTag || isHeaderTag) {
353+
let id: string | undefined
354+
if (isHeaderTag) {
355+
token = token as marked.Tokens.Heading
356+
id = extractId(html, `h${token.depth}`).trim()
357+
}
358+
return React.createElement(tag, {
359+
className: getClassname(token),
360+
dangerouslySetInnerHTML: { __html: stripTag(html, tag) },
361+
id,
362+
})
345363
}
346-
return React.createElement(tag, {
347-
className: getClassname(token),
348-
dangerouslySetInnerHTML: { __html: stripTag(html, tag) },
349-
id,
350-
})
351364
}
352365

353366
return (

0 commit comments

Comments
 (0)