refactor(markdown): separate the markdown ctx and decouple it from the biz
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import katexStyle from "katex/dist/katex.min.css?raw"
|
||||
import { createElement, Fragment, memo, useEffect, useMemo, useRef, useState } from "react"
|
||||
|
||||
import { MemoedDangerousHTMLStyle } from "~/components/common/MemoedDangerousHTMLStyle"
|
||||
import { parseHtml } from "~/lib/parse-html"
|
||||
import { useWrappedElementSize } from "~/providers/wrapped-element-provider"
|
||||
|
||||
import type { MediaInfoRecord } from "../media"
|
||||
import { MediaContainerWidthProvider, MediaInfoRecordProvider } from "../media"
|
||||
import { MarkdownRenderContainerRefContext } from "./context"
|
||||
|
||||
export type HTMLProps<A extends keyof JSX.IntrinsicElements = "div"> = {
|
||||
children: string | null | undefined
|
||||
as: A
|
||||
|
||||
accessory?: React.ReactNode
|
||||
noMedia?: boolean
|
||||
mediaInfo?: MediaInfoRecord
|
||||
|
||||
handleTranslate?: (html: HTMLElement | null) => void
|
||||
} & JSX.IntrinsicElements[A] &
|
||||
Partial<{
|
||||
renderInlineStyle: boolean
|
||||
}>
|
||||
const HTMLImpl = <A extends keyof JSX.IntrinsicElements = "div">(props: HTMLProps<A>) => {
|
||||
const {
|
||||
children,
|
||||
renderInlineStyle,
|
||||
as = "div",
|
||||
accessory,
|
||||
noMedia,
|
||||
mediaInfo,
|
||||
handleTranslate: translate,
|
||||
...rest
|
||||
} = props
|
||||
const [remarkOptions, setRemarkOptions] = useState({
|
||||
renderInlineStyle,
|
||||
noMedia,
|
||||
})
|
||||
const [shouldForceReMountKey, setShouldForceReMountKey] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
setRemarkOptions((options) => {
|
||||
if (JSON.stringify(options) === JSON.stringify({ renderInlineStyle, noMedia })) {
|
||||
return options
|
||||
}
|
||||
|
||||
setShouldForceReMountKey((key) => key + 1)
|
||||
return { ...options, renderInlineStyle, noMedia }
|
||||
})
|
||||
}, [renderInlineStyle, noMedia])
|
||||
|
||||
const [refElement, setRefElement] = useState<HTMLElement | null>(null)
|
||||
|
||||
const onceRef = useRef(false)
|
||||
useEffect(() => {
|
||||
if (onceRef.current || !refElement) {
|
||||
return
|
||||
}
|
||||
|
||||
translate?.(refElement)
|
||||
onceRef.current = true
|
||||
}, [translate, refElement])
|
||||
|
||||
const markdownElement = useMemo(
|
||||
() =>
|
||||
children &&
|
||||
parseHtml(children, {
|
||||
...remarkOptions,
|
||||
}).toContent(),
|
||||
[children, remarkOptions],
|
||||
)
|
||||
|
||||
const { w: containerWidth } = useWrappedElementSize()
|
||||
|
||||
if (!markdownElement) return null
|
||||
return (
|
||||
<MarkdownRenderContainerRefContext.Provider value={refElement}>
|
||||
<MediaContainerWidthProvider width={containerWidth}>
|
||||
<MediaInfoRecordProvider mediaInfo={mediaInfo}>
|
||||
<MemoedDangerousHTMLStyle>{katexStyle}</MemoedDangerousHTMLStyle>
|
||||
{createElement(as, { ...rest, ref: setRefElement }, markdownElement)}
|
||||
</MediaInfoRecordProvider>
|
||||
</MediaContainerWidthProvider>
|
||||
{!!accessory && <Fragment key={shouldForceReMountKey}>{accessory}</Fragment>}
|
||||
</MarkdownRenderContainerRefContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const HTML = memo(HTMLImpl)
|
||||
@@ -1,15 +1,9 @@
|
||||
import katexStyle from "katex/dist/katex.min.css?raw"
|
||||
import { createElement, Fragment, memo, useEffect, useMemo, useRef, useState } from "react"
|
||||
import { useMemo, useState } from "react"
|
||||
|
||||
import { MemoedDangerousHTMLStyle } from "~/components/common/MemoedDangerousHTMLStyle"
|
||||
import { parseHtml } from "~/lib/parse-html"
|
||||
import type { RemarkOptions } from "~/lib/parse-markdown"
|
||||
import { parseMarkdown } from "~/lib/parse-markdown"
|
||||
import { cn } from "~/lib/utils"
|
||||
import { useWrappedElementSize } from "~/providers/wrapped-element-provider"
|
||||
|
||||
import type { MediaInfoRecord } from "../media"
|
||||
import { MediaContainerWidthProvider, MediaInfoRecordProvider } from "../media"
|
||||
import { MarkdownRenderContainerRefContext } from "./context"
|
||||
|
||||
export const Markdown: Component<
|
||||
@@ -39,84 +33,3 @@ export const Markdown: Component<
|
||||
</MarkdownRenderContainerRefContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
const HTMLImpl = <A extends keyof JSX.IntrinsicElements = "div">(
|
||||
props: {
|
||||
children: string | null | undefined
|
||||
as: A
|
||||
|
||||
accessory?: React.ReactNode
|
||||
noMedia?: boolean
|
||||
mediaInfo?: MediaInfoRecord
|
||||
|
||||
translate?: (html: HTMLElement | null) => void
|
||||
} & JSX.IntrinsicElements[A] &
|
||||
Partial<{
|
||||
renderInlineStyle: boolean
|
||||
}>,
|
||||
) => {
|
||||
const {
|
||||
children,
|
||||
renderInlineStyle,
|
||||
as = "div",
|
||||
accessory,
|
||||
noMedia,
|
||||
mediaInfo,
|
||||
translate,
|
||||
...rest
|
||||
} = props
|
||||
const [remarkOptions, setRemarkOptions] = useState({
|
||||
renderInlineStyle,
|
||||
noMedia,
|
||||
})
|
||||
const [shouldForceReMountKey, setShouldForceReMountKey] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
setRemarkOptions((options) => {
|
||||
if (JSON.stringify(options) === JSON.stringify({ renderInlineStyle, noMedia })) {
|
||||
return options
|
||||
}
|
||||
|
||||
setShouldForceReMountKey((key) => key + 1)
|
||||
return { ...options, renderInlineStyle, noMedia }
|
||||
})
|
||||
}, [renderInlineStyle, noMedia])
|
||||
|
||||
const [refElement, setRefElement] = useState<HTMLElement | null>(null)
|
||||
|
||||
const onceRef = useRef(false)
|
||||
useEffect(() => {
|
||||
if (onceRef.current || !refElement) {
|
||||
return
|
||||
}
|
||||
|
||||
translate?.(refElement)
|
||||
onceRef.current = true
|
||||
}, [translate, refElement])
|
||||
|
||||
const markdownElement = useMemo(
|
||||
() =>
|
||||
children &&
|
||||
parseHtml(children, {
|
||||
...remarkOptions,
|
||||
}).toContent(),
|
||||
[children, remarkOptions],
|
||||
)
|
||||
|
||||
const { w: containerWidth } = useWrappedElementSize()
|
||||
|
||||
if (!markdownElement) return null
|
||||
return (
|
||||
<MarkdownRenderContainerRefContext.Provider value={refElement}>
|
||||
<MediaContainerWidthProvider width={containerWidth}>
|
||||
<MediaInfoRecordProvider mediaInfo={mediaInfo}>
|
||||
<MemoedDangerousHTMLStyle>{katexStyle}</MemoedDangerousHTMLStyle>
|
||||
{createElement(as, { ...rest, ref: setRefElement }, markdownElement)}
|
||||
</MediaInfoRecordProvider>
|
||||
</MediaContainerWidthProvider>
|
||||
{!!accessory && <Fragment key={shouldForceReMountKey}>{accessory}</Fragment>}
|
||||
</MarkdownRenderContainerRefContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const HTML = memo(HTMLImpl)
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
import { createContext } from "react"
|
||||
import { createContext as reactCreateContext } from "react"
|
||||
import { createContext } from "use-context-selector"
|
||||
|
||||
export const MarkdownRenderContainerRefContext = createContext<HTMLElement | null>(null)
|
||||
import type { MarkdownImage, MarkdownRenderActions } from "./types"
|
||||
|
||||
export const MarkdownRenderContainerRefContext = reactCreateContext<HTMLElement | null>(null)
|
||||
|
||||
export const MarkdownImageRecordContext = createContext<Record<string, MarkdownImage>>({})
|
||||
|
||||
export const MarkdownRenderActionContext = reactCreateContext<MarkdownRenderActions>({
|
||||
transformUrl: (url) => url ?? "",
|
||||
isAudio: () => false,
|
||||
ensureAndRenderTimeStamp: () => false,
|
||||
})
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./Markdown"
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useContext } from "react"
|
||||
import { useContextSelector } from "use-context-selector"
|
||||
|
||||
import { cn } from "~/lib/utils"
|
||||
import { useEntryContentContext } from "~/modules/entry-content/hooks"
|
||||
import { useWrappedElementSize } from "~/providers/wrapped-element-provider"
|
||||
import { useEntry } from "~/store/entry"
|
||||
|
||||
import { Media } from "../../media"
|
||||
import { usePopulatedFullUrl } from "../utils"
|
||||
import { MarkdownImageRecordContext, MarkdownRenderActionContext } from "../context"
|
||||
|
||||
export const MarkdownBlockImage = (
|
||||
props: React.ImgHTMLAttributes<HTMLImageElement> & {
|
||||
@@ -15,12 +16,13 @@ export const MarkdownBlockImage = (
|
||||
},
|
||||
) => {
|
||||
const size = useWrappedElementSize()
|
||||
const { feedId } = useEntryContentContext()
|
||||
|
||||
const src = usePopulatedFullUrl(feedId, props.src)
|
||||
const { transformUrl } = useContext(MarkdownRenderActionContext)
|
||||
const src = transformUrl(props.src)
|
||||
|
||||
const { entryId } = useEntryContentContext()
|
||||
const media = useEntry(entryId, (entry) => entry?.entries.media?.find((m) => m.url === props.src))
|
||||
const media = useContextSelector(MarkdownImageRecordContext, (record) =>
|
||||
props.src ? record[props.src] : null,
|
||||
)
|
||||
|
||||
return (
|
||||
<Media
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { useContext } from "react"
|
||||
|
||||
import { cn } from "~/lib/utils"
|
||||
import { useEntryContentContext } from "~/modules/entry-content/hooks"
|
||||
|
||||
import { Media } from "../../media"
|
||||
import { usePopulatedFullUrl } from "../utils"
|
||||
import { MarkdownRenderActionContext } from "../context"
|
||||
|
||||
export const MarkdownInlineImage = (
|
||||
props: React.ImgHTMLAttributes<HTMLImageElement> & {
|
||||
@@ -12,8 +13,8 @@ export const MarkdownInlineImage = (
|
||||
}
|
||||
},
|
||||
) => {
|
||||
const { feedId } = useEntryContentContext()
|
||||
const populatedUrl = usePopulatedFullUrl(feedId, props.src)
|
||||
const { transformUrl } = useContext(MarkdownRenderActionContext)
|
||||
const populatedUrl = transformUrl(props.src)
|
||||
return (
|
||||
<Media
|
||||
type="photo"
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { FeedViewType } from "~/lib/enum"
|
||||
import { useEntryContentContext } from "~/modules/entry-content/hooks"
|
||||
import { useContext } from "react"
|
||||
|
||||
import type { LinkProps } from "../../link"
|
||||
import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "../../tooltip"
|
||||
import { ensureAndRenderTimeStamp, usePopulatedFullUrl } from "../utils"
|
||||
import { MarkdownRenderActionContext } from "../context"
|
||||
|
||||
export const MarkdownLink = (props: LinkProps) => {
|
||||
const { view, feedId } = useEntryContentContext()
|
||||
const { transformUrl, isAudio, ensureAndRenderTimeStamp } = useContext(
|
||||
MarkdownRenderActionContext,
|
||||
)
|
||||
|
||||
const populatedFullHref = usePopulatedFullUrl(feedId, props.href)
|
||||
const populatedFullHref = transformUrl(props.href)
|
||||
|
||||
const parseTimeStamp = view === FeedViewType.Audios
|
||||
const parseTimeStamp = isAudio(populatedFullHref)
|
||||
if (parseTimeStamp) {
|
||||
const childrenText = props.children
|
||||
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { FeedViewType } from "~/lib/enum"
|
||||
import { useEntryContentContextSelector } from "~/modules/entry-content/hooks"
|
||||
|
||||
import { ensureAndRenderTimeStamp } from "../utils"
|
||||
import { MarkdownRenderActionContext } from "../context"
|
||||
import { IsInParagraphContext } from "./ctx"
|
||||
|
||||
export const MarkdownP: Component<
|
||||
React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
|
||||
> = ({ children, ...props }) => {
|
||||
const view = useEntryContentContextSelector((s) => s.view)
|
||||
const parseTimeline = view === FeedViewType.Audios
|
||||
const { isAudio, ensureAndRenderTimeStamp } = React.useContext(MarkdownRenderActionContext)
|
||||
const parseTimeline = isAudio()
|
||||
if (parseTimeline && typeof children === "string") {
|
||||
const renderer = ensureAndRenderTimeStamp(children)
|
||||
if (renderer) return <p>{renderer}</p>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
export type MarkdownImage = {
|
||||
url: string
|
||||
blurhash?: string
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
export interface MarkdownRenderActions {
|
||||
transformUrl: (url?: string) => string
|
||||
isAudio: (url?: string) => boolean
|
||||
ensureAndRenderTimeStamp: (children: string) => React.ReactNode
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
import { useMemo } from "react"
|
||||
|
||||
import { useFeedByIdSelector } from "~/store/feed"
|
||||
|
||||
import { TimeStamp } from "../components/TimeStamp"
|
||||
|
||||
export const ensureAndRenderTimeStamp = (children: string) => {
|
||||
const firstPart = children.replace(" ", " ").split(" ")[0]
|
||||
// 00:00 , 00:00:00
|
||||
if (!firstPart) {
|
||||
return
|
||||
}
|
||||
const isTime = isValidTimeString(firstPart.trim())
|
||||
if (isTime) {
|
||||
return (
|
||||
<>
|
||||
<TimeStamp time={firstPart} />
|
||||
<span>{children.slice(firstPart.length)}</span>
|
||||
</>
|
||||
)
|
||||
}
|
||||
return false
|
||||
}
|
||||
function isValidTimeString(time: string): boolean {
|
||||
const timeRegex = /^\d{1,2}:[0-5]\d(?::[0-5]\d)?$/
|
||||
return timeRegex.test(time)
|
||||
}
|
||||
|
||||
export function timeStringToSeconds(time: string): number | null {
|
||||
const timeParts = time.split(":").map(Number)
|
||||
|
||||
if (timeParts.length === 2) {
|
||||
const [minutes, seconds] = timeParts
|
||||
return minutes * 60 + seconds
|
||||
} else if (timeParts.length === 3) {
|
||||
const [hours, minutes, seconds] = timeParts
|
||||
return hours * 3600 + minutes * 60 + seconds
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const safeUrl = (url: string, baseUrl: string) => {
|
||||
try {
|
||||
return new URL(url, baseUrl).href
|
||||
} catch {
|
||||
return url
|
||||
}
|
||||
}
|
||||
export const usePopulatedFullUrl = (feedId: string, relativeUrl?: string) => {
|
||||
const feedSiteUrl = useFeedByIdSelector(feedId, (feed) =>
|
||||
"siteUrl" in feed ? feed.siteUrl : undefined,
|
||||
)
|
||||
|
||||
const populatedFullHref = useMemo(() => {
|
||||
if (!relativeUrl) return void 0
|
||||
|
||||
if (relativeUrl.startsWith("http")) return relativeUrl
|
||||
if (relativeUrl.startsWith("/") && feedSiteUrl) return safeUrl(relativeUrl, feedSiteUrl)
|
||||
return relativeUrl
|
||||
}, [feedSiteUrl, relativeUrl])
|
||||
|
||||
return populatedFullHref
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import type { ExternalToast } from "sonner"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { CopyButton } from "~/components/ui/code-highlighter"
|
||||
import { Markdown } from "~/components/ui/markdown"
|
||||
import { Markdown } from "~/components/ui/markdown/Markdown"
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
export const getFetchErrorMessage = (error: Error) => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { FeedViewType } from "~/lib/enum"
|
||||
|
||||
import { DayOf } from "./constants"
|
||||
import { DailyReportModalContent, DailyReportTitle } from "./daily"
|
||||
import { useParseDailyDate } from "./hooks"
|
||||
import { useParseDailyDate } from "./useParseDailyDate"
|
||||
|
||||
const tabs = [DayOf.Today, DayOf.Yesterday]
|
||||
|
||||
@@ -23,7 +23,11 @@ export const FeedDailyModalContent = () => {
|
||||
|
||||
<div className="flex grow flex-col items-center overflow-auto">
|
||||
{tabs.map((tab: any) => (
|
||||
<TabsContent key={tab} value={tab}>
|
||||
<TabsContent
|
||||
key={tab}
|
||||
value={tab}
|
||||
className="flex grow flex-col data-[state=inactive]:hidden"
|
||||
>
|
||||
<DailyReportModalContent
|
||||
view={FeedViewType.SocialMedia}
|
||||
{...(tab === DayOf.Today ? today : yesterday)}
|
||||
|
||||
@@ -12,7 +12,7 @@ import { Collapse } from "~/components/ui/collapse"
|
||||
import { RelativeTime } from "~/components/ui/datetime"
|
||||
import type { LinkProps } from "~/components/ui/link"
|
||||
import { LoadingCircle } from "~/components/ui/loading"
|
||||
import { Markdown } from "~/components/ui/markdown"
|
||||
import { Markdown } from "~/components/ui/markdown/Markdown"
|
||||
import { MarkdownLink } from "~/components/ui/markdown/renderers"
|
||||
import { Media } from "~/components/ui/media"
|
||||
import { usePreviewMedia } from "~/components/ui/media/hooks"
|
||||
@@ -34,8 +34,8 @@ import { Queries } from "~/queries"
|
||||
import { useEntry } from "~/store/entry"
|
||||
import { useFeedById } from "~/store/feed"
|
||||
|
||||
import { useParseDailyDate } from "./hooks"
|
||||
import type { DailyItemProps, DailyView } from "./types"
|
||||
import { useParseDailyDate } from "./useParseDailyDate"
|
||||
|
||||
export const DailyItem = ({ view, day }: DailyItemProps) => {
|
||||
const { title, startDate, endDate } = useParseDailyDate(day)
|
||||
@@ -184,34 +184,39 @@ export const DailyReportModalContent: Component<DailyReportContentProps> = ({
|
||||
view,
|
||||
}) => {
|
||||
const content = useQueryData({ endDate, startDate, view })
|
||||
|
||||
const RelatedEntryLink = useState(() => createRelatedEntryLink("toast"))[0]
|
||||
const { t } = useTranslation()
|
||||
if (!content.data && !content.isLoading)
|
||||
return (
|
||||
<div className="center pointer-events-none absolute inset-0 translate-y-6 flex-col gap-4 opacity-80">
|
||||
<EmptyIcon />
|
||||
<p>{t("ai_daily.no_found")}</p>
|
||||
</div>
|
||||
)
|
||||
return (
|
||||
<div className="center flex-col">
|
||||
{content.isLoading ? (
|
||||
<LoadingCircle
|
||||
size="large"
|
||||
className="center pointer-events-none absolute inset-0 mt-8 text-center"
|
||||
/>
|
||||
) : content.data ? (
|
||||
<Markdown
|
||||
components={{
|
||||
a: RelatedEntryLink as Components["a"],
|
||||
}}
|
||||
className="prose-sm mt-4 px-6 prose-p:my-1 prose-ul:my-1 prose-ul:list-outside prose-ul:list-disc prose-li:marker:text-accent"
|
||||
>
|
||||
{content.data}
|
||||
</Markdown>
|
||||
) : (
|
||||
<div className="center pointer-events-none absolute inset-0 translate-y-6 flex-col gap-4 opacity-80">
|
||||
<EmptyIcon />
|
||||
<p>{t("ai_daily.no_found")}</p>
|
||||
</div>
|
||||
)}
|
||||
<div className="center grow flex-col">
|
||||
<div className="flex grow flex-col">
|
||||
{content.isLoading ? (
|
||||
<LoadingCircle
|
||||
size="large"
|
||||
className="center pointer-events-none absolute inset-0 mt-8 text-center"
|
||||
/>
|
||||
) : content.data ? (
|
||||
<Markdown
|
||||
components={{
|
||||
a: RelatedEntryLink as Components["a"],
|
||||
}}
|
||||
className="prose-sm mt-4 h-0 grow overflow-auto px-6 prose-p:my-1 prose-ul:my-1 prose-ul:list-outside prose-ul:list-disc prose-li:marker:text-accent"
|
||||
>
|
||||
{content.data}
|
||||
</Markdown>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{!!content.data && (
|
||||
<FlatMarkAllReadButton
|
||||
className="ml-auto"
|
||||
className="ml-auto shrink-0"
|
||||
filter={{
|
||||
startTime: startDate,
|
||||
endTime: endDate,
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { useCallback } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { useModalStack } from "~/components/ui/modal"
|
||||
|
||||
import { FeedDailyModalContent } from "./FeedDailyModalContent"
|
||||
|
||||
export const useAIDailyReportModal = () => {
|
||||
const { present } = useModalStack()
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useCallback(() => {
|
||||
present({
|
||||
content: () => <FeedDailyModalContent />,
|
||||
title: t("ai_daily.header"),
|
||||
resizeable: true,
|
||||
clickOutsideToDismiss: true,
|
||||
|
||||
resizeDefaultSize: { width: 660, height: 450 },
|
||||
})
|
||||
}, [present, t])
|
||||
}
|
||||
+1
-20
@@ -1,10 +1,7 @@
|
||||
import { useCallback, useMemo } from "react"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { useModalStack } from "~/components/ui/modal"
|
||||
|
||||
import { DayOf } from "./constants"
|
||||
import { FeedDailyModalContent } from "./FeedDailyModalContent"
|
||||
|
||||
export const useParseDailyDate = (day: DayOf) => {
|
||||
const { t } = useTranslation("common")
|
||||
@@ -62,19 +59,3 @@ export const useParseDailyDate = (day: DayOf) => {
|
||||
return { title, startDate, endDate }
|
||||
}, [day, t])
|
||||
}
|
||||
|
||||
export const useAIDailyReportModal = () => {
|
||||
const { present } = useModalStack()
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useCallback(() => {
|
||||
present({
|
||||
content: () => <FeedDailyModalContent />,
|
||||
title: t("ai_daily.header"),
|
||||
resizeable: true,
|
||||
clickOutsideToDismiss: true,
|
||||
|
||||
resizeDefaultSize: { width: 660, height: 450 },
|
||||
})
|
||||
}, [present])
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import { Button } from "~/components/ui/button"
|
||||
import { CopyButton } from "~/components/ui/code-highlighter"
|
||||
import { Form, FormItem, FormLabel } from "~/components/ui/form"
|
||||
import { Input } from "~/components/ui/input"
|
||||
import { Markdown } from "~/components/ui/markdown"
|
||||
import { Markdown } from "~/components/ui/markdown/Markdown"
|
||||
import { useCurrentModal, useIsTopModal, useModalStack } from "~/components/ui/modal"
|
||||
import {
|
||||
Select,
|
||||
|
||||
@@ -80,7 +80,7 @@ export function DiscoverForm({ type = "search" }: { type?: string }) {
|
||||
mutationFn: async ({ keyword, target }: { keyword: string; target: "feeds" | "lists" }) => {
|
||||
const { data } = await apiClient.discover.$post({
|
||||
json: {
|
||||
keyword,
|
||||
keyword: keyword.trim(),
|
||||
target,
|
||||
},
|
||||
})
|
||||
@@ -119,7 +119,7 @@ export function DiscoverForm({ type = "search" }: { type?: string }) {
|
||||
|
||||
const keyword = form.watch("keyword")
|
||||
useEffect(() => {
|
||||
const trimmedKeyword = keyword.trim()
|
||||
const trimmedKeyword = keyword.trimStart()
|
||||
if (!prefix) {
|
||||
form.setValue("keyword", trimmedKeyword)
|
||||
return
|
||||
|
||||
@@ -18,7 +18,7 @@ import { useIsOnline } from "~/hooks/common"
|
||||
import { stopPropagation } from "~/lib/dom"
|
||||
import { FeedViewType } from "~/lib/enum"
|
||||
import { cn, getOS, isBizId } from "~/lib/utils"
|
||||
import { useAIDailyReportModal } from "~/modules/ai/ai-daily/hooks"
|
||||
import { useAIDailyReportModal } from "~/modules/ai/ai-daily/useAIDailyReportModal"
|
||||
import { EntryHeader } from "~/modules/entry-content/header"
|
||||
import { useRefreshFeedMutation } from "~/queries/feed"
|
||||
import { useFeedById, useFeedHeaderTitle } from "~/store/feed"
|
||||
@@ -47,7 +47,7 @@ export const EntryListHeader: FC<{
|
||||
|
||||
const titleInfo = !!headerTitle && (
|
||||
<div className={!titleAtBottom ? "min-w-0 translate-y-1" : void 0}>
|
||||
<div className="mt-1 h-6 min-w-0 break-all text-lg font-bold leading-tight">
|
||||
<div className="h-6 min-w-0 break-all text-lg font-bold leading-tight">
|
||||
<EllipsisHorizontalTextWithTooltip className="inline-block !w-auto max-w-full">
|
||||
<span className="relative -top-px">{headerTitle}</span>
|
||||
</EllipsisHorizontalTextWithTooltip>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as HoverCard from "@radix-ui/react-hover-card"
|
||||
|
||||
import { HTML } from "~/components/ui/markdown"
|
||||
import { HTML } from "~/components/ui/markdown/HTML"
|
||||
import { ScrollArea } from "~/components/ui/scroll-area"
|
||||
import { tooltipStyle } from "~/components/ui/tooltip/styles"
|
||||
import { useMeasure } from "~/hooks/common"
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useContext, useContextSelector } from "use-context-selector"
|
||||
|
||||
import { isDev } from "~/constants"
|
||||
|
||||
import { EntryContentContext } from "./provider"
|
||||
|
||||
export const useEntryContentContext = () => {
|
||||
const ctx = useContext(EntryContentContext)
|
||||
if (!ctx && isDev) {
|
||||
console.error("Notice: EntryContentContext is not provided.", new Error("x").stack)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
export const useEntryContentContextSelector = <T>(selector: (ctx: EntryContentContext) => T) =>
|
||||
useContextSelector(EntryContentContext, selector)
|
||||
@@ -18,7 +18,6 @@ import { enableShowSourceContent } from "~/atoms/source-content"
|
||||
import { m } from "~/components/common/Motion"
|
||||
import { ShadowDOM } from "~/components/common/ShadowDOM"
|
||||
import { AutoResizeHeight } from "~/components/ui/auto-resize-height"
|
||||
import { HTML } from "~/components/ui/markdown"
|
||||
import { Toc } from "~/components/ui/markdown/components/Toc"
|
||||
import { useInPeekModal } from "~/components/ui/modal/inspire/PeekModal"
|
||||
import { RootPortal } from "~/components/ui/portal"
|
||||
@@ -45,6 +44,7 @@ import { useFeedById } from "~/store/feed"
|
||||
|
||||
import { LoadingWithIcon } from "../../components/ui/loading"
|
||||
import { EntryPlaceholderDaily } from "../ai/ai-daily/EntryPlaceholderDaily"
|
||||
import { EntryContentHTMLRenderer } from "../renderer/html"
|
||||
import {
|
||||
getTranslationCache,
|
||||
setEntryContentScrollToTop,
|
||||
@@ -57,7 +57,6 @@ import { SourceContentPanel } from "./components/SourceContentView"
|
||||
import { SupportCreator } from "./components/SupportCreator"
|
||||
import { EntryHeader } from "./header"
|
||||
import { EntryContentLoading } from "./loading"
|
||||
import { EntryContentProvider } from "./provider"
|
||||
|
||||
export interface EntryContentClassNames {
|
||||
header?: string
|
||||
@@ -217,12 +216,7 @@ export const EntryContentRender: Component<{
|
||||
}
|
||||
|
||||
return (
|
||||
<EntryContentProvider
|
||||
entryId={entry.entries.id}
|
||||
feedId={entry.feedId}
|
||||
audioSrc={entry.entries?.attachments?.[0].url}
|
||||
view={view}
|
||||
>
|
||||
<>
|
||||
<EntryHeader
|
||||
entryId={entry.entries.id}
|
||||
view={0}
|
||||
@@ -267,8 +261,11 @@ export const EntryContentRender: Component<{
|
||||
<ErrorBoundary fallback={RenderError}>
|
||||
{!isInReadabilityMode ? (
|
||||
<ShadowDOM>
|
||||
<HTML
|
||||
translate={translate}
|
||||
<EntryContentHTMLRenderer
|
||||
view={view}
|
||||
feedId={feed?.id}
|
||||
entryId={entryId}
|
||||
handleTranslate={translate}
|
||||
mediaInfo={mediaInfo}
|
||||
noMedia={noMedia}
|
||||
accessory={contentAccessories}
|
||||
@@ -278,10 +275,10 @@ export const EntryContentRender: Component<{
|
||||
renderInlineStyle={readerRenderInlineStyle}
|
||||
>
|
||||
{content}
|
||||
</HTML>
|
||||
</EntryContentHTMLRenderer>
|
||||
</ShadowDOM>
|
||||
) : (
|
||||
<ReadabilityContent entryId={entryId} />
|
||||
<ReadabilityContent entryId={entryId} feedId={feed.id} />
|
||||
)}
|
||||
</ErrorBoundary>
|
||||
</div>
|
||||
@@ -323,7 +320,7 @@ export const EntryContentRender: Component<{
|
||||
</ScrollArea.ScrollArea>
|
||||
<SourceContentPanel src={entry.entries.url} />
|
||||
</div>
|
||||
</EntryContentProvider>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -364,9 +361,10 @@ const TitleMetaHandler: Component<{
|
||||
return null
|
||||
}
|
||||
|
||||
const ReadabilityContent = ({ entryId }: { entryId: string }) => {
|
||||
const ReadabilityContent = ({ entryId, feedId }: { entryId: string; feedId: string }) => {
|
||||
const { t } = useTranslation()
|
||||
const result = useEntryReadabilityContent(entryId)
|
||||
const view = useRouteParamsSelector((route) => route.view)
|
||||
|
||||
return (
|
||||
<div className="grow">
|
||||
@@ -382,12 +380,15 @@ const ReadabilityContent = ({ entryId }: { entryId: string }) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<HTML
|
||||
<EntryContentHTMLRenderer
|
||||
view={view}
|
||||
feedId={feedId}
|
||||
entryId={entryId}
|
||||
as="article"
|
||||
className="prose dark:prose-invert prose-h1:text-[1.6em] prose-h1:font-bold"
|
||||
>
|
||||
{result?.content ?? ""}
|
||||
</HTML>
|
||||
</EntryContentHTMLRenderer>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
+19
-5
@@ -1,16 +1,16 @@
|
||||
import { useContext } from "react"
|
||||
|
||||
import { AudioPlayer } from "~/atoms/player"
|
||||
import { nextFrame } from "~/lib/dom"
|
||||
import { useEntryContentContext } from "~/modules/entry-content/hooks"
|
||||
import { useEntry } from "~/store/entry"
|
||||
|
||||
import { timeStringToSeconds } from "../utils"
|
||||
import { EntryInfoContext } from "../context"
|
||||
|
||||
export const TimeStamp = (props: { time: string }) => {
|
||||
const { entryId, audioSrc: src } = useEntryContentContext()
|
||||
|
||||
const { entryId } = useContext(EntryInfoContext)
|
||||
const entry = useEntry(entryId)
|
||||
const mediaDuration = entry?.entries.attachments?.[0]?.duration_in_seconds
|
||||
|
||||
const src = entry?.entries?.attachments?.[0].url
|
||||
if (!src) return <span>{props.time}</span>
|
||||
|
||||
const seekTo = timeStringToSeconds(props.time)
|
||||
@@ -89,3 +89,17 @@ const CircleProgress: React.FC<CircleProgressProps> = ({
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
function timeStringToSeconds(time: string): number | null {
|
||||
const timeParts = time.split(":").map(Number)
|
||||
|
||||
if (timeParts.length === 2) {
|
||||
const [minutes, seconds] = timeParts
|
||||
return minutes * 60 + seconds
|
||||
} else if (timeParts.length === 3) {
|
||||
const [hours, minutes, seconds] = timeParts
|
||||
return hours * 3600 + minutes * 60 + seconds
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
+11
@@ -1,3 +1,4 @@
|
||||
import { createContext as createReactContext } from "react"
|
||||
import { createContext } from "use-context-selector"
|
||||
|
||||
import { FeedViewType } from "~/lib/enum"
|
||||
@@ -20,3 +21,13 @@ export const EntryContentContext = createContext<EntryContentContext>(defaultCon
|
||||
export const EntryContentProvider: Component<EntryContentContext> = ({ children, ...value }) => (
|
||||
<EntryContentContext.Provider value={value}>{children}</EntryContentContext.Provider>
|
||||
)
|
||||
|
||||
export interface EntryInfoContext {
|
||||
feedId: string
|
||||
entryId: string
|
||||
}
|
||||
const defaultInfoContextValue: EntryInfoContext = {
|
||||
feedId: "",
|
||||
entryId: "",
|
||||
}
|
||||
export const EntryInfoContext = createReactContext<EntryInfoContext>(defaultInfoContextValue)
|
||||
@@ -0,0 +1,115 @@
|
||||
import { useMemo } from "react"
|
||||
|
||||
import {
|
||||
MarkdownImageRecordContext,
|
||||
MarkdownRenderActionContext,
|
||||
} from "~/components/ui/markdown/context"
|
||||
import type { HTMLProps } from "~/components/ui/markdown/HTML"
|
||||
import { HTML } from "~/components/ui/markdown/HTML"
|
||||
import type { MarkdownImage, MarkdownRenderActions } from "~/components/ui/markdown/types"
|
||||
import { FeedViewType } from "~/lib/enum"
|
||||
import { useEntry } from "~/store/entry/hooks"
|
||||
import { useFeedByIdSelector } from "~/store/feed/hooks"
|
||||
|
||||
import { TimeStamp } from "./components/TimeStamp"
|
||||
import { EntryInfoContext } from "./context"
|
||||
|
||||
export function EntryContentHTMLRenderer<AS extends keyof JSX.IntrinsicElements = "div">({
|
||||
view,
|
||||
feedId,
|
||||
entryId,
|
||||
children,
|
||||
...props
|
||||
}: {
|
||||
view: FeedViewType
|
||||
feedId: string
|
||||
entryId: string
|
||||
children: Nullable<string>
|
||||
} & HTMLProps<AS>) {
|
||||
const entry = useEntry(entryId)
|
||||
|
||||
const populatedFullUrl = usePopulatedFullUrl(feedId, entry?.entries.url || void 0)
|
||||
const images: Record<string, MarkdownImage> = useMemo(() => {
|
||||
return (
|
||||
entry?.entries.media?.reduce(
|
||||
(acc, media) => {
|
||||
if (media.height && media.width) {
|
||||
acc[media.url] = {
|
||||
url: media.url,
|
||||
width: media.width,
|
||||
height: media.height,
|
||||
}
|
||||
}
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, MarkdownImage>,
|
||||
) ?? {}
|
||||
)
|
||||
}, [entry])
|
||||
const actions: MarkdownRenderActions = useMemo(() => {
|
||||
return {
|
||||
isAudio() {
|
||||
return view === FeedViewType.Audios
|
||||
},
|
||||
transformUrl(url) {
|
||||
return populatedFullUrl ?? url ?? ""
|
||||
},
|
||||
ensureAndRenderTimeStamp,
|
||||
}
|
||||
}, [populatedFullUrl, view])
|
||||
return (
|
||||
<MarkdownImageRecordContext.Provider value={images}>
|
||||
<MarkdownRenderActionContext.Provider value={actions}>
|
||||
<EntryInfoContext.Provider value={useMemo(() => ({ feedId, entryId }), [feedId, entryId])}>
|
||||
{/* @ts-expect-error */}
|
||||
<HTML {...props}>{children}</HTML>
|
||||
</EntryInfoContext.Provider>
|
||||
</MarkdownRenderActionContext.Provider>
|
||||
</MarkdownImageRecordContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
const safeUrl = (url: string, baseUrl: string) => {
|
||||
try {
|
||||
return new URL(url, baseUrl).href
|
||||
} catch {
|
||||
return url
|
||||
}
|
||||
}
|
||||
const usePopulatedFullUrl = (feedId: string, relativeUrl?: string) => {
|
||||
const feedSiteUrl = useFeedByIdSelector(feedId, (feed) =>
|
||||
"siteUrl" in feed ? feed.siteUrl : undefined,
|
||||
)
|
||||
|
||||
const populatedFullHref = useMemo(() => {
|
||||
if (!relativeUrl) return void 0
|
||||
|
||||
if (relativeUrl.startsWith("http")) return relativeUrl
|
||||
if (relativeUrl.startsWith("/") && feedSiteUrl) return safeUrl(relativeUrl, feedSiteUrl)
|
||||
return relativeUrl
|
||||
}, [feedSiteUrl, relativeUrl])
|
||||
|
||||
return populatedFullHref
|
||||
}
|
||||
|
||||
const ensureAndRenderTimeStamp = (children: string) => {
|
||||
const firstPart = children.replace(" ", " ").split(" ")[0]
|
||||
// 00:00 , 00:00:00
|
||||
if (!firstPart) {
|
||||
return
|
||||
}
|
||||
const isTime = isValidTimeString(firstPart.trim())
|
||||
if (isTime) {
|
||||
return (
|
||||
<>
|
||||
<TimeStamp time={firstPart} />
|
||||
<span>{children.slice(firstPart.length)}</span>
|
||||
</>
|
||||
)
|
||||
}
|
||||
return false
|
||||
}
|
||||
function isValidTimeString(time: string): boolean {
|
||||
const timeRegex = /^\d{1,2}:[0-5]\d(?::[0-5]\d)?$/
|
||||
return timeRegex.test(time)
|
||||
}
|
||||
Reference in New Issue
Block a user