-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:facebook/docusaurus into lex111/hig…
…hlight-code-line-built
- Loading branch information
Showing
20 changed files
with
539 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {type ComponentProps} from 'react'; | ||
import clsx from 'clsx'; | ||
import { | ||
usePrismTheme, | ||
getPrismCssVariables, | ||
ThemeClassNames, | ||
} from '@docusaurus/theme-common'; | ||
import styles from './styles.module.css'; | ||
|
||
export default function CodeBlockContainer<T extends 'div' | 'pre'>({ | ||
as: As, | ||
...props | ||
}: {as: T} & ComponentProps<T>): JSX.Element { | ||
const prismTheme = usePrismTheme(); | ||
const prismCssVariables = getPrismCssVariables(prismTheme); | ||
return ( | ||
<As | ||
// Polymorphic components are hard to type, without `oneOf` generics | ||
{...(props as never)} | ||
style={prismCssVariables} | ||
className={clsx( | ||
props.className, | ||
styles.codeBlockContainer, | ||
ThemeClassNames.common.codeBlock, | ||
)} | ||
/> | ||
); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
.codeBlockContainer { | ||
background: var(--prism-background-color); | ||
color: var(--prism-color); | ||
margin-bottom: var(--ifm-leading); | ||
box-shadow: var(--ifm-global-shadow-lw); | ||
border-radius: var(--ifm-code-border-radius); | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/Element.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import Container from '@theme/CodeBlock/Container'; | ||
import clsx from 'clsx'; | ||
import type {Props} from '@theme/CodeBlock/Content/Element'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
// <pre> tags in markdown map to CodeBlocks. They may contain JSX children. When | ||
// the children is not a simple string, we just return a styled block without | ||
// actually highlighting. | ||
export default function CodeBlockJSX({ | ||
children, | ||
className, | ||
}: Props): JSX.Element { | ||
return ( | ||
<Container | ||
as="pre" | ||
tabIndex={0} | ||
className={clsx(styles.codeBlockStandalone, 'thin-scrollbar', className)}> | ||
<code className={styles.codeBlockLines}>{children}</code> | ||
</Container> | ||
); | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/String.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { | ||
useThemeConfig, | ||
parseCodeBlockTitle, | ||
parseLanguage, | ||
parseLines, | ||
containsLineNumbers, | ||
usePrismTheme, | ||
} from '@docusaurus/theme-common'; | ||
import clsx from 'clsx'; | ||
import Highlight, {defaultProps, type Language} from 'prism-react-renderer'; | ||
import Line from '@theme/CodeBlock/Line'; | ||
import CopyButton from '@theme/CodeBlock/CopyButton'; | ||
import Container from '@theme/CodeBlock/Container'; | ||
import type {Props} from '@theme/CodeBlock/Content/String'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
export default function CodeBlockString({ | ||
children, | ||
className: blockClassName = '', | ||
metastring, | ||
title: titleProp, | ||
showLineNumbers: showLineNumbersProp, | ||
language: languageProp, | ||
}: Props): JSX.Element { | ||
const { | ||
prism: {defaultLanguage}, | ||
} = useThemeConfig(); | ||
const language = | ||
languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage; | ||
const prismTheme = usePrismTheme(); | ||
|
||
// We still parse the metastring in case we want to support more syntax in the | ||
// future. Note that MDX doesn't strip quotes when parsing metastring: | ||
// "title=\"xyz\"" => title: "\"xyz\"" | ||
const title = parseCodeBlockTitle(metastring) || titleProp; | ||
|
||
const {highlightLines, code} = parseLines(children, metastring, language); | ||
const showLineNumbers = | ||
showLineNumbersProp || containsLineNumbers(metastring); | ||
|
||
return ( | ||
<Container | ||
as="div" | ||
className={clsx( | ||
blockClassName, | ||
language && | ||
!blockClassName.includes(`language-${language}`) && | ||
`language-${language}`, | ||
)}> | ||
{title && <div className={styles.codeBlockTitle}>{title}</div>} | ||
<div className={styles.codeBlockContent}> | ||
<Highlight | ||
{...defaultProps} | ||
theme={prismTheme} | ||
code={code} | ||
language={(language ?? 'text') as Language}> | ||
{({className, tokens, getLineProps, getTokenProps}) => ( | ||
<pre | ||
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */ | ||
tabIndex={0} | ||
className={clsx(className, styles.codeBlock, 'thin-scrollbar')}> | ||
<code | ||
className={clsx( | ||
styles.codeBlockLines, | ||
showLineNumbers && styles.codeBlockLinesWithNumbering, | ||
)}> | ||
{tokens.map((line, i) => ( | ||
<Line | ||
key={i} | ||
line={line} | ||
getLineProps={getLineProps} | ||
getTokenProps={getTokenProps} | ||
highlight={highlightLines.includes(i)} | ||
showLineNumbers={showLineNumbers} | ||
/> | ||
))} | ||
</code> | ||
</pre> | ||
)} | ||
</Highlight> | ||
<CopyButton code={code} /> | ||
</div> | ||
</Container> | ||
); | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
.codeBlockContent { | ||
position: relative; | ||
/* rtl:ignore */ | ||
direction: ltr; | ||
border-radius: inherit; | ||
} | ||
|
||
.codeBlockTitle { | ||
border-bottom: 1px solid var(--ifm-color-emphasis-300); | ||
font-size: var(--ifm-code-font-size); | ||
font-weight: 500; | ||
padding: 0.75rem var(--ifm-pre-padding); | ||
border-top-left-radius: inherit; | ||
border-top-right-radius: inherit; | ||
} | ||
|
||
.codeBlock { | ||
--ifm-pre-background: var(--prism-background-color); | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
.codeBlockTitle + .codeBlockContent .codeBlock { | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
} | ||
|
||
.codeBlockStandalone { | ||
padding: 0; | ||
} | ||
|
||
.codeBlockLines { | ||
font: inherit; | ||
/* rtl:ignore */ | ||
float: left; | ||
min-width: 100%; | ||
padding: var(--ifm-pre-padding); | ||
} | ||
|
||
.codeBlockLinesWithNumbering { | ||
display: table; | ||
padding: var(--ifm-pre-padding) 0; | ||
} | ||
|
||
@media print { | ||
.codeBlockLines { | ||
white-space: pre-wrap; | ||
} | ||
} |
Oops, something went wrong.