Skip to content

Commit 4c142e3

Browse files
committed
🌟feat: html render support
1 parent 31f0df5 commit 4c142e3

File tree

5 files changed

+142
-42
lines changed

5 files changed

+142
-42
lines changed

next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const withPWA = require('next-pwa')({
44
disable: process.env.NODE_ENV === 'development',
55
})
66
module.exports = withPWA({
7-
transpilePackages: ['@mdxeditor/editor', 'react-diff-view','highlight.js','remark-gfm'],
7+
transpilePackages: ['@mdxeditor/editor', 'react-diff-view','highlight.js','remark-gfm','rehype-raw'],
88
webpack: (config, { isServer }) => {
99
config.experiments = { ...config.experiments, topLevelAwait: true };
1010
if (!isServer) {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blinko",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"private": true,
55
"browser": {
66
"fs": false,
@@ -110,6 +110,8 @@
110110
"react-masonry-css": "^1.0.16",
111111
"react-photo-view": "^1.2.6",
112112
"react-syntax-highlighter": "^15.5.0",
113+
"rehype-raw": "^7.0.0",
114+
"rehype-sanitize": "^6.0.0",
113115
"remark-gfm": "^4.0.0",
114116
"sharp": "^0.33.5",
115117
"superjson": "^2.2.1",

pnpm-lock.yaml

Lines changed: 114 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Common/MarkdownRender.tsx

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { RootStore } from '@/store';
1414
import { StorageState } from '@/store/standard/StorageState';
1515
import { Icon } from '@iconify/react';
1616
import { PhotoProvider, PhotoView } from 'react-photo-view';
17+
import rehypeRaw from 'rehype-raw';
18+
import rehypeSanitize from 'rehype-sanitize';
1719

1820
const highlightTags = (text) => {
1921
if (!text) return text
@@ -58,43 +60,6 @@ const LinkPreview = ({ href }) => {
5860
const store = RootStore.Local(() => ({
5961
previewData: new StorageState<LinkInfo | null>({ key: href, default: null })
6062
}))
61-
try {
62-
if (typeof href == 'object') {
63-
return <div className='react-image'>
64-
<PhotoProvider >
65-
<PhotoView src={href?.props?.src}>
66-
<Image src={href?.props?.src} />
67-
</PhotoView>
68-
</PhotoProvider>
69-
</div>
70-
}
71-
if (href?.startsWith('<img')) {
72-
const src = href.match(/src="([^"]+)"/)?.[1]
73-
const regex = /height="(\d+)"\s+width="(\d+)"/;
74-
const matches = href.match(regex);
75-
console.log(matches)
76-
let style = {}
77-
if (matches) {
78-
let height = matches[1];
79-
let width = matches[2];
80-
style = { height: height + 'px', width: width + 'px' }
81-
console.log(`Height: ${height}, Width: ${width}`);
82-
} else {
83-
console.log("No match found.");
84-
}
85-
return <div className='text-image'>
86-
<PhotoProvider >
87-
<PhotoView src={src}>
88-
<Image src={src} {...style} />
89-
</PhotoView>
90-
</PhotoProvider>
91-
</div>
92-
}
93-
} catch (error) {
94-
console.log(error)
95-
return href
96-
}
97-
9863
useEffect(() => {
9964
const fetchData = async () => {
10065
try {
@@ -124,6 +89,21 @@ const LinkPreview = ({ href }) => {
12489
);
12590
};
12691

92+
const ImageWrapper = ({ src, alt, width, height }) => {
93+
return (
94+
<div className="custom-image-wrapper">
95+
<div className='react-image'>
96+
<PhotoProvider >
97+
<PhotoView src={src}>
98+
<Image src={src} width={width} height={height} />
99+
</PhotoView>
100+
</PhotoProvider>
101+
</div>
102+
</div>
103+
);
104+
};
105+
106+
127107

128108
export const MarkdownRender = observer(({ content = '', onChange }: { content?: string, onChange?: (newContent: string) => void }) => {
129109
const { theme } = useTheme()
@@ -150,6 +130,7 @@ export const MarkdownRender = observer(({ content = '', onChange }: { content?:
150130
<div ref={contentRef} data-markdown-theme={theme} className={`markdown-body content ${isExpanded ? "expanded" : "collapsed"}`}>
151131
<ReactMarkdown
152132
remarkPlugins={[remarkGfm]}
133+
rehypePlugins={[rehypeRaw, rehypeSanitize]} //xss
153134
components={{
154135
p: ({ node, children }) => <p>{highlightTags(children)}</p>,
155136
code: Code,
@@ -180,6 +161,7 @@ export const MarkdownRender = observer(({ content = '', onChange }: { content?:
180161
}
181162
return <li >{children}</li>
182163
},
164+
img: ImageWrapper
183165
}}
184166
>
185167
{content}

src/styles/editor.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
color: #0969da !important;
1111
}
1212

13+
._linkDialogPopoverContent_uazmk_600, ._tableColumnEditorPopoverContent_uazmk_601, ._dialogContent_uazmk_602{
14+
background-color:var(--background)!important;
15+
}
16+
1317
.prose :where(h1):not(:where([class~="not-prose"],[class~="not-prose"] *)),
1418
.prose :where(h2):not(:where([class~="not-prose"],[class~="not-prose"] *)),
1519
.prose :where(h3):not(:where([class~="not-prose"],[class~="not-prose"] *)),
@@ -25,8 +29,7 @@
2529
height: 100%;
2630
}
2731

28-
#radix-\:r51\:,
29-
#radix-\:rq8\: {
32+
[id^="radix-"] {
3033
display: none;
3134
}
3235

0 commit comments

Comments
 (0)