From ade53a4717f0cda2f931227178e8d03a62dc3ef3 Mon Sep 17 00:00:00 2001 From: slhmy Date: Sat, 28 Sep 2024 20:02:33 +0800 Subject: [PATCH] Force trim markdown H1 for problem description --- src/hooks/problem.ts | 2 ++ src/pipes/problem.ts | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 src/pipes/problem.ts diff --git a/src/hooks/problem.ts b/src/hooks/problem.ts index 1a1a84118..1628fafc7 100644 --- a/src/hooks/problem.ts +++ b/src/hooks/problem.ts @@ -3,6 +3,7 @@ import * as ProblemServiceModel from "@/models/service/problem"; import * as ProblemService from "@/apis/problem"; import { useDispatch } from "react-redux"; import { AddMessageSagaPattern } from "@/store/sagas/message"; +import { TrimMarkdownTitlePipe } from "@/pipes/problem"; export const useProblem = (slug: string, fallback?: () => void) => { const [problem, setProblem] = useState( @@ -18,6 +19,7 @@ export const useProblem = (slug: string, fallback?: () => void) => { useEffect(() => { ProblemService.getProblem(slug) .then((res) => { + res.description = TrimMarkdownTitlePipe(res.description); setProblem(res); }) .catch((err) => { diff --git a/src/pipes/problem.ts b/src/pipes/problem.ts new file mode 100644 index 000000000..a179a6ca6 --- /dev/null +++ b/src/pipes/problem.ts @@ -0,0 +1,4 @@ +// Remove the first H1 title from the markdown content +export const TrimMarkdownTitlePipe = (content: string): string => { + return content.replace(/^# .*\n/, ""); +};