Skip to content

Commit

Permalink
Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed Feb 28, 2021
1 parent 0ab998f commit 711b78b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default function CodeBlock({
code={code}
language={language}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
<>
<div className={styles.codeBlockContainer}>
{codeBlockTitle && (
<div style={style} className={styles.codeBlockTitle}>
{codeBlockTitle}
Expand Down Expand Up @@ -262,7 +262,7 @@ export default function CodeBlock({
)}
</button>
</div>
</>
</div>
)}
</Highlight>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

.codeBlockContainer {
margin-bottom: var(--ifm-leading);
}

.codeBlockContent {
position: relative;
/*rtl:ignore*/
direction: ltr;
margin-bottom: var(--ifm-leading);
}

.codeBlockTitle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const MDXComponents: MDXComponentsObject = {
return children;
},
a: (props) => <Link {...props} />,
pre: (props) => <div className="mdxCodeBlock" {...props} />,
pre: (props: any) => {
const {children} = props;
return <CodeBlock {...children?.props} />;
},
h1: Heading('h1'),
h2: Heading('h2'),
h3: Heading('h3'),
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ declare module '@theme/MDXComponents' {
export type MDXComponentsObject = {
readonly code: typeof CodeBlock;
readonly a: (props: ComponentProps<'a'>) => JSX.Element;
readonly pre: (props: ComponentProps<'div'>) => JSX.Element;
readonly pre: typeof CodeBlock;
readonly h1: (props: ComponentProps<'h1'>) => JSX.Element;
readonly h2: (props: ComponentProps<'h2'>) => JSX.Element;
readonly h3: (props: ComponentProps<'h3'>) => JSX.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,40 @@ import styles from './styles.module.css';

export default function Playground({children, theme, transformCode, ...props}) {
return (
<LiveProvider
code={children.replace(/\n$/, '')}
transformCode={transformCode || ((code) => `${code};`)}
theme={theme}
{...props}>
<div
className={clsx(
styles.playgroundHeader,
styles.playgroundEditorHeader,
)}>
<Translate
id="theme.Playground.liveEditor"
description="The live editor label of the live codeblocks">
Live Editor
</Translate>
</div>
<LiveEditor className={styles.playgroundEditor} />
<div
className={clsx(
styles.playgroundHeader,
styles.playgroundPreviewHeader,
)}>
<Translate
id="theme.Playground.result"
description="The result label of the live codeblocks">
Result
</Translate>
</div>
<div className={styles.playgroundPreview}>
<LivePreview />
<LiveError />
</div>
</LiveProvider>
<div className={styles.playgroundContainer}>
<LiveProvider
code={children.replace(/\n$/, '')}
transformCode={transformCode || ((code) => `${code};`)}
theme={theme}
{...props}>
<div
className={clsx(
styles.playgroundHeader,
styles.playgroundEditorHeader,
)}>
<Translate
id="theme.Playground.liveEditor"
description="The live editor label of the live codeblocks">
Live Editor
</Translate>
</div>
<LiveEditor className={styles.playgroundEditor} />
<div
className={clsx(
styles.playgroundHeader,
styles.playgroundPreviewHeader,
)}>
<Translate
id="theme.Playground.result"
description="The result label of the live codeblocks">
Result
</Translate>
</div>
<div className={styles.playgroundPreview}>
<LivePreview />
<LiveError />
</div>
</LiveProvider>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

.playgroundContainer {
margin-bottom: var(--ifm-leading);
}

.playgroundHeader {
letter-spacing: 0.08rem;
padding: 0.75rem;
Expand All @@ -15,6 +19,8 @@
.playgroundEditorHeader {
background: var(--ifm-color-emphasis-600);
color: var(--ifm-color-content-inverse);
border-top-left-radius: var(--ifm-global-radius);
border-top-right-radius: var(--ifm-global-radius);
}

.playgroundPreviewHeader {
Expand All @@ -23,7 +29,8 @@
}

.playgroundEditor {
font-family: var(--ifm-font-family-monospace) !important;
font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
var(--ifm-font-family-monospace) !important;
/*rtl:ignore*/
direction: ltr;
}
Expand All @@ -32,6 +39,5 @@
border: 1px solid var(--ifm-color-emphasis-200);
border-bottom-left-radius: var(--ifm-global-radius);
border-bottom-right-radius: var(--ifm-global-radius);
position: relative;
padding: 1rem;
}
61 changes: 61 additions & 0 deletions website/src/pages/examples/markdownPageExample.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,64 @@ import MyComponentSource from '!!raw-loader!@site/src/pages/examples/\_myCompone
<CodeBlock className="language-jsx">{MyComponentSource}</CodeBlock>

</BrowserWindow>

---

## Code block test

```js title="Title"
function Clock(props) {
const [date, setDate] = useState(new Date());
useEffect(() => {
var timerID = setInterval(() => tick(), 1000);

return function cleanup() {
clearInterval(timerID);
};
});

function tick() {
setDate(new Date());
}

return (
<div>
<h2>It is {date.toLocaleTimeString()}.</h2>
// highlight-start
{/* prettier-ignore */}
long long long long long long long long long long long long line
{/* prettier-ignore */}
// highlight-end
</div>
);
}
```

```jsx live
function Clock(props) {
const [date, setDate] = useState(new Date());
useEffect(() => {
var timerID = setInterval(() => tick(), 1000);

return function cleanup() {
clearInterval(timerID);
};
});

function tick() {
setDate(new Date());
}

return (
<div>
<h2>It is {date.toLocaleTimeString()}.</h2>
</div>
);
}
```

<CodeBlock className="language-yaml" title="test">
test
</CodeBlock>

<code>test</code>

0 comments on commit 711b78b

Please sign in to comment.