Skip to content

Commit

Permalink
fix: shiki plugin space style (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 authored Mar 14, 2024
1 parent 2a10e2d commit 3b6aafe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/khaki-falcons-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspress/theme-default": patch
"@rspress/plugin-shiki": patch
---

fix: plugin-shiki space style
4 changes: 4 additions & 0 deletions packages/plugin-shiki/shiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
--shiki-token-link: #f9a76d;
}

.rspress-doc span.line {
padding: 0 1.25rem;
}

.diff,
.code-line-highlighted {
transition: background-color 0.5s;
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-shiki/src/shiki/rehypePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export const rehypePluginShiki: Plugin<[Options], Root> = function ({
}
});

// Strip the final empty span
const lastLine = codeLines[codeLines.length - 1];
if (lastLine?.children.length === 0) {
codeLines.pop();
}

// @ts-expect-error
parent.children.splice(index!, 1, {
type: 'element',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export function PackageManagerTabs({
{Object.entries(commandInfo).map(([key, value]) => (
<Tab key={key}>
<Pre>
<Code className="language-js">{value}</Code>
{/* For this case, we can specify to highlight the code in runtime instead of compile time */}
<Code className="language-js" codeHighlighter="prism">
{value}
</Code>
</Pre>
</Tab>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import { SvgWrapper } from '#theme/components/SvgWrapper';
export interface CodeProps {
children: string;
className?: string;
codeHighlighter?: 'prism' | 'shiki';
meta?: string;
}

export function Code(props: CodeProps) {
const { siteData } = usePageData();
const { defaultWrapCode, codeHighlighter } = siteData.markdown;
const codeHighlighter =
props.codeHighlighter ?? siteData.markdown.codeHighlighter;
const { defaultWrapCode } = siteData.markdown;
const [codeWrap, setCodeWrap] = useState(defaultWrapCode);
const wrapButtonRef = useRef<HTMLButtonElement>(null);
const codeBlockRef = useRef<HTMLDivElement>();
Expand Down

0 comments on commit 3b6aafe

Please sign in to comment.