1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 03:05:24 -04:00

refactor code block detection and css

This commit is contained in:
Jake Jarvis 2025-04-15 20:05:27 -04:00
parent 89517ea815
commit 62409690c1
Signed by: jake
SSH Key Fingerprint: SHA256:nCkvAjYA6XaSPUqc4TfbBQTpzr8Xj7ritg/sGInCdkc
4 changed files with 34 additions and 41 deletions

View File

@ -27,6 +27,7 @@ samp,
pre { pre {
font-size: 1em; font-size: 1em;
font-family: var(--fonts-mono); font-family: var(--fonts-mono);
font-variant-ligatures: none; /* i hate them. fwiw. */
} }
small { small {

View File

@ -1,10 +1,8 @@
.inline, /**
.highlighted { * Inline code
font-family: var(--fonts-mono); **/
font-variant-ligatures: none; /* i hate them. fwiw. */
}
.inline { :not([data-rehype-pretty-code-figure]) .code {
padding: 0.2em 0.3em; padding: 0.2em 0.3em;
font-size: 0.925em; font-size: 0.925em;
page-break-inside: avoid; page-break-inside: avoid;
@ -13,14 +11,18 @@
border-radius: 0.6em; border-radius: 0.6em;
} }
figure:has(.highlighted) { /**
* Syntax-highlighted code blocks
**/
[data-rehype-pretty-code-figure]:has(.code) {
margin: 1em auto; margin: 1em auto;
position: relative; position: relative;
width: 100%; width: 100%;
background-color: var(--colors-background-header); background-color: var(--colors-background-header);
} }
.highlighted { [data-rehype-pretty-code-figure] .code {
display: block; display: block;
overflow-x: auto; overflow-x: auto;
padding: 1em; padding: 1em;
@ -28,48 +30,45 @@ figure:has(.highlighted) {
tab-size: 2px; tab-size: 2px;
border: 1px solid var(--colors-kinda-light); border: 1px solid var(--colors-kinda-light);
border-radius: 0.6em; border-radius: 0.6em;
counter-reset: line;
} }
.highlighted [style*="--shiki"] { [data-rehype-pretty-code-figure] .code [style*="--shiki"] {
color: var(--shiki-light); color: var(--shiki-light);
font-style: var(--shiki-light-font-style); font-style: var(--shiki-light-font-style);
font-weight: var(--shiki-light-font-weight); font-weight: var(--shiki-light-font-weight);
text-decoration: var(--shiki-light-text-decoration); text-decoration: var(--shiki-light-text-decoration);
} }
[data-theme="dark"] .highlighted [style*="--shiki"] { [data-theme="dark"] [data-rehype-pretty-code-figure] .code [style*="--shiki"] {
color: var(--shiki-dark); color: var(--shiki-dark);
font-style: var(--shiki-dark-font-style); font-style: var(--shiki-dark-font-style);
font-weight: var(--shiki-dark-font-weight); font-weight: var(--shiki-dark-font-weight);
text-decoration: var(--shiki-dark-text-decoration); text-decoration: var(--shiki-dark-text-decoration);
} }
.highlighted > [data-line]:nth-of-type(1), [data-rehype-pretty-code-figure] .code[data-line-numbers] > [data-line]:nth-of-type(1),
.highlighted > [data-line]:nth-of-type(2) { [data-rehype-pretty-code-figure] .code[data-line-numbers] > [data-line]:nth-of-type(2) {
/* excessive right padding to prevent copy button from covering the first two lines of code */ /* excessive right padding to prevent copy button from covering the first two lines of code */
padding-right: 4em; padding-right: 4em;
} }
.highlighted[data-line-numbers] { [data-rehype-pretty-code-figure] .code[data-line-numbers] > [data-line]::before {
counter-reset: line;
}
.highlighted[data-line-numbers] > [data-line]::before {
display: inline-block; display: inline-block;
width: 1em; width: 1em;
margin-right: 1.5em; margin-right: 1.5em;
text-align: right; text-align: right;
color: var(--colors-medium-light); color: var(--colors-medium-light);
user-select: none;
counter-increment: line; counter-increment: line;
content: counter(line); content: counter(line);
user-select: none;
} }
.highlighted[data-line-numbers-max-digits="2"] > [data-line]::before { [data-rehype-pretty-code-figure] .code[data-line-numbers-max-digits="2"] > [data-line]::before {
width: 1.25rem; width: 1.25rem;
} }
.highlighted[data-line-numbers-max-digits="3"] > [data-line]::before { [data-rehype-pretty-code-figure] .code[data-line-numbers-max-digits="3"] > [data-line]::before {
width: 1.75rem; width: 1.75rem;
} }

View File

@ -12,30 +12,23 @@ export type CodeProps = ComponentPropsWithoutRef<"code"> & {
// a simple wrapper component that "intelligently" picks between inline code and code blocks (w/ optional syntax // a simple wrapper component that "intelligently" picks between inline code and code blocks (w/ optional syntax
// highlighting & a clipboard button) // highlighting & a clipboard button)
const Code = ({ const Code = ({
"data-language": dataLanguage, "data-language": dataLanguage, // eslint-disable-line @typescript-eslint/no-unused-vars
"data-theme": dataTheme, // eslint-disable-line @typescript-eslint/no-unused-vars "data-theme": dataTheme,
className, className,
children, children,
...rest ...rest
}: CodeProps) => { }: CodeProps) => {
// detect if this input has already been touched by shiki via rehype-pretty-code
if (dataLanguage) {
// full multi-line code blocks with copy-to-clipboard button
return (
<>
<CopyButton className={styles.copyButton} source={children} />
<code className={clsx(styles.highlighted, className)} {...rest}>
{children}
</code>
</>
);
}
// simple inline code in paragraphs, headings, etc. (never highlighted)
return ( return (
<code className={clsx(styles.inline, className)} {...rest}> <>
{children} {
</code> // detect if this input has been touched by shiki via rehype-pretty-code and if so, include a copy-to-clipboard
// button as its sibling.
dataTheme && <CopyButton className={styles.copyButton} source={children} />
}
<code className={clsx(styles.code, className)} {...rest}>
{children}
</code>
</>
); );
}; };

View File

@ -1,5 +1,5 @@
/* /*!
* Pulled from @reach/skip-nav: https://github.com/reach/reach-ui/blob/main/packages/skip-nav/styles.css * @reach/skip-nav | MIT License | https://github.com/reach/reach-ui/blob/v0.18.0/packages/skip-nav/styles.css
*/ */
.hidden { .hidden {