Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/fixtures/code-block-runtime/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test.describe('<CodeBlockRuntime />', async () => {
await expect(container.locator('.rspress-code-title')).toHaveText(
'test.js',
);
const content = container.locator('.rspress-code-content');
const content = container.locator('.rp-codeblock__content');
const shikiContainer = content.locator('.shiki.css-variables').first();
await expect(shikiContainer).toHaveJSProperty('tagName', 'PRE');
await expect(shikiContainer.locator('code').first()).toHaveText(
Expand All @@ -53,7 +53,7 @@ test.describe('<CodeBlockRuntime />', async () => {
await expect(container.locator('.rspress-code-title')).toHaveText(
'highlight.ts',
);
const content = container.locator('.rspress-code-content');
const content = container.locator('.rp-codeblock__content');
const shikiContainer = content.locator('.shiki.css-variables').first();
await expect(shikiContainer).toHaveJSProperty('tagName', 'PRE');
await expect(shikiContainer.locator('code').first()).toHaveText(
Expand Down
15 changes: 15 additions & 0 deletions e2e/fixtures/code-block/doc/FileTree.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# FileTree

import { FileTree } from '@theme';

<FileTree>

```tsx title="foo.tsx"
console.log(foo);
```

```tsx title="doc/foo.tsx"
console.log(bar);
```

</FileTree>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @rspress/plugin-shiki
# Shiki

## transformers

Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/node/mdx/rehypePlugins/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SHIKI_TRANSFORMER_LINE_NUMBER,
transformerLineNumber,
} from './transformers';
import { transformerAddLang } from './transformers/add-lang';
import { transformerAddTitle } from './transformers/add-title';

const cssVariablesTheme = createCssVariablesTheme({
Expand All @@ -20,7 +21,11 @@ function createRehypeShikiOptions(
): RehypeShikiOptions {
const { transformers = [], ...restOptions } = options || {};

const newTransformers = [transformerAddTitle(), ...transformers];
const newTransformers = [
transformerAddTitle(),
transformerAddLang(),
...transformers,
];
if (
showLineNumbers &&
!newTransformers.some(
Expand All @@ -36,7 +41,6 @@ function createRehypeShikiOptions(
lazy: true, // Lazy loading all langs except ['tsx', 'ts', 'js'] , @see https://github.com/fuma-nama/fumadocs/blob/9b38baf2e66d7bc6f88d24b90a3857730a15fe3c/packages/core/src/mdx-plugins/rehype-code.ts#L169
langs: ['tsx', 'ts', 'js'],
...restOptions,
addLanguageClass: true,
transformers: newTransformers,
};
}
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/node/mdx/rehypePlugins/transformers/add-lang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ShikiTransformer } from 'shiki';

export const SHIKI_TRANSFORMER_ADD_LANG = 'shiki-transformer:add-lang';

export function transformerAddLang(): ShikiTransformer {
return {
name: SHIKI_TRANSFORMER_ADD_LANG,
pre(pre) {
const lang = this.options.lang;
if (lang.length > 0) {
pre.properties = {
...pre.properties,
lang,
};
}
return pre;
},
};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { SHIKI_TRANSFORMER_ADD_LANG, transformerAddLang } from './add-lang';
export { SHIKI_TRANSFORMER_ADD_TITLE, transformerAddTitle } from './add-title';
export {
SHIKI_TRANSFORMER_LINE_NUMBER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ function _createMdxContent(props) {
},
tabIndex: "0",
children: _jsx(_components.code, {
className: "language-js",
children: _jsxs(_components.span, {
className: "line",
children: [_jsx(_components.span, {
Expand Down Expand Up @@ -820,7 +819,6 @@ function _createMdxContent(props) {
},
tabIndex: "0",
children: _jsx(_components.code, {
className: "language-javascript",
children: _jsxs(_components.span, {
className: "line",
children: [_jsx(_components.span, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function _createMdxContent(props) {
},
tabIndex: "0",
children: _jsxs(_components.code, {
className: "language-jsx",
children: [_jsxs(_components.span, {
className: "line",
children: [_jsx(_components.span, {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-twoslash/static/global-styles/twoslash.css
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@
font-size: var(--twoslash-code-font-size);
}

.twoslash .rspress-code-content {
.twoslash .rp-codeblock__content {
padding: 16px 0;
color: var(--rp-code-block-color);
background-color: var(--rp-code-block-bg);
border-radius: var(--rp-radius);
}

.twoslash .rspress-code-content [class^='code-button-group'] {
.twoslash .rp-codeblock__content [class^='code-button-group'] {
display: none;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/theme-default/src/components/Callout/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
background-color: var(--rp-code-title-bg-with-opacity);
}

.rspress-code-content {
.rp-codeblock__content {
background-color: var(--rp-code-block-bg-with-opacity);
}

Expand Down
17 changes: 2 additions & 15 deletions packages/theme-default/src/components/CodeBlockRuntime/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,10 @@ export function CodeBlockRuntime({
development: false,
components: {
...getCustomMDXComponent(),
// implement `addLanguageClass: true`
pre: props => (
<PreWithCodeButtonGroup
title={title}
containerElementClassName={`language-${lang}`}
{...props}
{...otherProps}
/>
),
code: ({ className, ...otherProps }) => (
<Code
{...otherProps}
className={[className, `language-${lang}`]
.filter(Boolean)
.join(' ')}
/>
<PreWithCodeButtonGroup title={title} {...props} {...otherProps} />
),
code: props => <Code {...props} />,
},
Fragment,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/theme-default/src/components/DocContent/doc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
}
// #endregion inline code

&:where(div[class^='language-']) {
&:where(.rp-codeblock) {
position: relative;
margin: 16px 0;
border: var(--rp-code-block-border);
Expand Down Expand Up @@ -304,14 +304,14 @@
}
}

&:where(.rspress-code-title) {
&:where(.rp-codeblock__title) {
font-family: var(--rp-font-family-mono);
padding: 12px 16px;
font-size: var(--rp-code-font-size);
background-color: var(--rp-code-title-bg);
}

&:where(.rspress-code-content) {
&:where(.rp-codeblock__content) {
font-size: var(--rp-code-font-size);
font-family: var(--rp-font-family-mono);
position: relative;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.rspress-code-content:hover .rp-code-button-group__button {
.rp-codeblock__content:hover .rp-code-button-group__button {
opacity: 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ function ShikiPre({
}: ShikiPreProps) {
const { codeWrap, toggleCodeWrap } = useCodeWrap();
return (
<div className={containerElementClassName}>
<div className={clsx('rp-codeblock', containerElementClassName)}>
{title && (
<div className="rspress-code-title rp-codeblock_title">{title}</div>
<div className="rp-codeblock__title rspress-code-title">{title}</div>
)}
<div className="rspress-code-content rp-codeblock_content">
<div className="rp-codeblock__content rspress-code-content">
<div>
<pre
ref={preElementRef}
className={clsx(codeWrap && 'rp-code-force-wrap', className)}
className={clsx(
'rp-codeblock__pre',
codeWrap && 'rp-code-force-wrap',
className,
)}
{...otherProps}
>
{child}
Expand Down Expand Up @@ -72,16 +76,16 @@ export interface PreWithCodeButtonGroupProps
* expected wrapped pre element is:
* ```html
*<div class="language-js">
* <div class="rspress-code-title rp-codeblock_title">test.js</div>
* <div class="rspress-code-content rp-codeblock_content">
* <div class="rp-codeblock__title rspress-code-title">test.js</div>
* <div class="rp-codeblock__content rspress-code-content">
* <div>
* <pre class="shiki css-variables" tabindex="0">
* <pre class="rp-codeblock__pre shiki css-variables" tabindex="0">
* <code class="language-js">
* </code>
* </pre>
* </div>
* <div class="rp-code-button-group">
* <button class="rp-code-button-group__button" title="Toggle code wrap"></button>
* <button class="rp-code-button-group__button rp-code-wrap-button" title="Toggle code wrap"></button>
* <button class="rp-code-button-group__button rp-code-copy-button" title="Copy code"></button>
* </div>
* </div>
Expand Down
114 changes: 114 additions & 0 deletions packages/theme-default/src/components/FileTree/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
.rp-file-tree {
display: flex;
border: var(--rp-code-block-border);
border-radius: var(--rp-radius);
overflow: hidden;
background-color: var(--rp-code-block-bg);

&__sidebar {
flex: 0 0 220px;
box-sizing: border-box;
max-width: 100%;
border-right: var(--rp-code-block-border);
background-color: var(--rp-code-title-bg);
padding: 12px 16px;
overflow-y: auto;
}

&__content {
flex: 1 1 auto;
min-width: 0;
padding: 12px 16px;
background-color: var(--rp-code-block-bg);
overflow-x: auto;

.rp-codeblock {
border: none;
border-radius: 0;
padding: 0;
margin: 0;
.rp-codeblock__title {
display: none;
}
}
}

&__list {
margin: 0;
padding: 0;
list-style: none;

& & {
margin-top: 4px;
padding-left: 12px;
border-left: 1px solid rgba(0, 0, 0, 0.05);
}

.dark & & {
border-left: 1px solid rgba(255, 255, 255, 0.08);
}
}

&__item {
margin: 2px 0;
}

&__dir {
font-size: 13px;
font-weight: 600;
color: var(--rp-c-text-2);
padding: 4px 0;

&--active {
color: var(--rp-c-text-1);
}
}

&__file {
width: 100%;
text-align: left;
border: none;
background: none;
padding: 6px 8px;
border-radius: var(--rp-radius-small);
color: var(--rp-c-text-1);
font-size: 13px;
line-height: 1.4;
cursor: pointer;
transition:
background-color 0.2s ease,
color 0.2s ease;

&:hover,
&:focus {
background-color: var(--rp-c-brand-tint);
color: var(--rp-c-text-1);
}

&:focus-visible {
outline: 2px solid var(--rp-c-brand);
outline-offset: 2px;
}

&--active {
background-color: var(--rp-c-brand-tint);
color: var(--rp-c-brand-dark);
font-weight: 600;
}
}

@media (max-width: 960px) {
flex-direction: column;

&__sidebar {
flex: none;
width: 100%;
border-right: none;
border-bottom: var(--rp-code-block-border);
}

&__content {
padding-top: 8px;
}
}
}
Loading
Loading