fix: extract CodeRenderer as named component, fix external link detection

This commit is contained in:
2026-04-10 00:56:14 +02:00
parent 51820e6507
commit 39cb194b5b

View File

@@ -72,6 +72,41 @@ function extractHastText(node: Element): string {
.trim(); .trim();
} }
function CodeRenderer({ children }: { children?: React.ReactNode }) {
const insidePre = useContext(InsidePreContext);
if (insidePre) {
return (
<code
style={{
fontFamily: "var(--font-mono, 'Geist Mono', monospace)",
fontSize: "0.9rem",
color: "#e2e8f0",
lineHeight: 1.65,
}}
>
{children}
</code>
);
}
return (
<code
style={{
background: "rgba(250, 204, 21, 0.1)",
border: "1px solid rgba(250, 204, 21, 0.3)",
borderRadius: 5,
padding: "1px 7px",
color: "#fde68a",
fontFamily: "var(--font-mono, 'Geist Mono', monospace)",
fontSize: "0.875em",
}}
>
{children}
</code>
);
}
const components: Components = { const components: Components = {
h1: ({ children }) => ( h1: ({ children }) => (
<h1 <h1
@@ -221,8 +256,8 @@ const components: Components = {
a: ({ href, children }) => ( a: ({ href, children }) => (
<a <a
href={href} href={href}
target={href?.startsWith("http") ? "_blank" : undefined} target={href && /^https?:\/\//.test(href) ? "_blank" : undefined}
rel={href?.startsWith("http") ? "noopener noreferrer" : undefined} rel={href && /^https?:\/\//.test(href) ? "noopener noreferrer" : undefined}
style={{ style={{
background: "linear-gradient(90deg, #facc15, #60a5fa)", background: "linear-gradient(90deg, #facc15, #60a5fa)",
WebkitBackgroundClip: "text", WebkitBackgroundClip: "text",
@@ -266,41 +301,7 @@ const components: Components = {
</InsidePreContext.Provider> </InsidePreContext.Provider>
), ),
code: ({ children }) => { code: CodeRenderer,
const insidePre = useContext(InsidePreContext);
if (insidePre) {
return (
<code
style={{
fontFamily: "var(--font-mono, 'Geist Mono', monospace)",
fontSize: "0.9rem",
color: "#e2e8f0",
lineHeight: 1.65,
}}
>
{children}
</code>
);
}
// Inline code
return (
<code
style={{
background: "rgba(250, 204, 21, 0.1)",
border: "1px solid rgba(250, 204, 21, 0.3)",
borderRadius: 5,
padding: "1px 7px",
color: "#fde68a",
fontFamily: "var(--font-mono, 'Geist Mono', monospace)",
fontSize: "0.875em",
}}
>
{children}
</code>
);
},
}; };
export default function MarkdownContent({ content }: { content: string }) { export default function MarkdownContent({ content }: { content: string }) {