Skip to content

Commit

Permalink
fix: optimize code string parser
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Aug 12, 2024
1 parent bca6276 commit b3d32d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
32 changes: 32 additions & 0 deletions src/renderer/src/components/ui/media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const MediaImpl: FC<MediaProps> = ({
type,
])

if (hidden) return <FallbackMedia {...props} />
return (
<div className={cn("overflow-hidden rounded", className)} style={style}>
{InnerContent}
Expand All @@ -208,3 +209,34 @@ const MediaImpl: FC<MediaProps> = ({
export const Media: FC<MediaProps> = memo((props) => (
<MediaImpl {...props} key={props.src} />
))

const FallbackMedia: FC<MediaProps> = ({
type,
mediaContainerClassName,
...props
}) => (
<div
className={cn(
!(props.width || props.height) && "size-full",
"center relative h-24 rounded bg-zinc-100 object-cover dark:bg-neutral-900",
"not-prose flex max-h-full flex-col space-y-1",
mediaContainerClassName,
)}
style={{
height: props.height ? `${props.height}px` : "",
width: props.width ? `${props.width}px` : "100%",
...props.style,
}}
>
<p>Media loaded failed</p>
<p className="flex items-center gap-1">
Go to
{" "}
<a href={props.src} target="_blank" rel="noreferrer" className="follow-link--underline">
{props.src}

</a>
<i className="i-mgc-external-link-cute-re" />
</p>
</div>
)
22 changes: 17 additions & 5 deletions src/renderer/src/lib/parse-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const parseHtml = async (
"video": ["src", "poster"],
},
})

.use(rehypeInferDescriptionMeta)
.use(rehypeStringify)

Expand Down Expand Up @@ -165,16 +166,27 @@ function extractCodeFromHtml(htmlString: string) {
const tempDiv = document.createElement("div")
tempDiv.innerHTML = htmlString

// 1. line break via <div />
const divElements = tempDiv.querySelectorAll("div")

let code = ""

divElements.forEach((div) => {
code += `${div.textContent}\n`
})
if (divElements.length > 0) {
divElements.forEach((div) => {
code += `${div.textContent}\n`
})
return code
}

// 2. line wrapper like <span><span>...</span></span>
const spanElements = tempDiv.querySelectorAll("span > span")

if (spanElements.length > 0) {
for (const node of tempDiv.children) {
code += `${node.textContent}\n`
}

if (divElements.length === 0) {
return tempDiv.textContent
return code
}

return code
Expand Down

0 comments on commit b3d32d0

Please sign in to comment.