This repository has been archived by the owner on Feb 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add code snippet component
- Loading branch information
Showing
8 changed files
with
144 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,17 @@ | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import Container from '@theme/CodeBlock/Container'; | ||
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}) { | ||
return ( | ||
<Container | ||
as="pre" | ||
tabIndex={0} | ||
className={clsx(styles.codeBlockStandalone, 'thin-scrollbar', className)}> | ||
<code className={styles.codeBlockLines}>{children}</code> | ||
</Container> | ||
); | ||
} |
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,41 @@ | ||
import React from 'react'; | ||
import { useThemeConfig } from '@docusaurus/theme-common'; | ||
import { | ||
parseCodeBlockTitle, | ||
parseLanguage, | ||
} from '@docusaurus/theme-common/internal'; | ||
import { CodeSnippet } from '@bsahitya/td-doc-design'; | ||
|
||
// Prism languages are always lowercase | ||
// We want to fail-safe and allow both "php" and "PHP" | ||
// See https://github.com/facebook/docusaurus/issues/9012 | ||
function normalizeLanguage(language) { | ||
return language?.toLowerCase(); | ||
} | ||
export default function CodeBlockString({ | ||
children, | ||
className: blockClassName = '', | ||
metastring, | ||
title: titleProp, | ||
language: languageProp, | ||
}) { | ||
const { | ||
prism: { defaultLanguage }, | ||
} = useThemeConfig(); | ||
const language = normalizeLanguage( | ||
languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage | ||
); | ||
|
||
const title = parseCodeBlockTitle(metastring) || titleProp; | ||
|
||
return ( | ||
<CodeSnippet | ||
key={title} | ||
label={title} | ||
maxHeight={0} | ||
language={language || 'text'} | ||
content={children} | ||
hideHeader={!title} | ||
></CodeSnippet> | ||
); | ||
} |
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,18 @@ | ||
cv-code-snippet { | ||
background-color: var(--cv-theme-surface-container); | ||
--mdc-theme-surface-canvas: var(--cv-theme-surface-container); | ||
margin-bottom: var(--ifm-leading); | ||
} | ||
|
||
.codeBlockStandalone { | ||
padding: 0; | ||
background-color: var(--cv-theme-surface-container); | ||
} | ||
|
||
.codeBlockLines { | ||
font: inherit; | ||
/* rtl:ignore */ | ||
float: left; | ||
min-width: 100%; | ||
padding: var(--ifm-pre-padding); | ||
} |
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,32 @@ | ||
import React, { isValidElement } from 'react'; | ||
import useIsBrowser from '@docusaurus/useIsBrowser'; | ||
import ElementContent from '@theme/CodeBlock/Content/Element'; | ||
import StringContent from '@theme/CodeBlock/Content/String'; | ||
/** | ||
* Best attempt to make the children a plain string so it is copyable. If there | ||
* are react elements, we will not be able to copy the content, and it will | ||
* return `children` as-is; otherwise, it concatenates the string children | ||
* together. | ||
*/ | ||
function maybeStringifyChildren(children) { | ||
if (React.Children.toArray(children).some((el) => isValidElement(el))) { | ||
return children; | ||
} | ||
// The children is now guaranteed to be one/more plain strings | ||
return Array.isArray(children) ? children.join('') : children; | ||
} | ||
export default function CodeBlock({ children: rawChildren, ...props }) { | ||
// The Prism theme on SSR is always the default theme but the site theme can | ||
// be in a different mode. React hydration doesn't update DOM styles that come | ||
// from SSR. Hence force a re-render after mounting to apply the current | ||
// relevant styles. | ||
const isBrowser = useIsBrowser(); | ||
const children = maybeStringifyChildren(rawChildren); | ||
const CodeBlockComp = | ||
typeof children === 'string' ? StringContent : ElementContent; | ||
return ( | ||
<CodeBlockComp key={String(isBrowser)} {...props}> | ||
{children} | ||
</CodeBlockComp> | ||
); | ||
} |