Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add typescript support for live code blocks (#2824) #3984

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
items: [{label: 'Twitter', to: 'https://twitter.com/docusaurus'}],
},
],
copyright: 'Copyright © 2020 Facebook Inc.',
copyright: `Copyright © ${new Date().getFullYear()} Facebook Inc.`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test broke it's a happy new year.

logo: {src: 'img/docusaurus_monochrome.svg'},
},
algolia: {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-live-codeblock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "MIT",
"dependencies": {
"@babel/standalone": "^7.12.12",
"@docusaurus/core": "2.0.0-alpha.70",
"@philpl/buble": "^0.19.7",
"clsx": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,55 @@ import clsx from 'clsx';

import styles from './styles.module.css';

function Playground({children, theme, transformCode, ...props}) {
Copy link
Contributor Author

@9oelM 9oelM Jan 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed transformCode prop. Never coming down from parent components.
Also, got rid of ...props because:

  1. this component is not written in Typescript, so it's hard to know what's inside ...props
  2. other keys that exist in props are live and metastring, which are not used here. Therefore excluding them.

const typescriptExtensions = ['ts', 'tsx'];
const transformJs = (code) => `${code};`;

function Playground({children, theme, className, scope}) {
const [tsTranspileError, setTsTranspileError] = React.useState(null);
const transformTs = React.useCallback((code) => {
try {
// eslint-disable-next-line global-require
const {code: transformed} = require('@babel/standalone').transform(code, {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required inside here in the hope that it will dynamically import it only if there is a code block written in Typescript... but I'm not fully sure if this hack works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like it does work?

filename: 'transformedCode.ts',
presets: [
'react',
[
'typescript',
{
isTSX: true,
allExtensions: true,
},
],
],
});
setTsTranspileError(null);

return transformed;
} catch (e) {
setTsTranspileError(e.message);
}

return '() => null;';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case if something goes wrong in the transpilation, render nothing. Theoretically it should never reach here.

}, []);

const isTypescriptCode = React.useMemo(
() =>
typescriptExtensions.some((extension) => className.endsWith(extension)),
[className],
);

const transformCode = React.useMemo(() => {
return isTypescriptCode ? transformTs : transformJs;
}, [isTypescriptCode, transformTs]);

return (
<LiveProvider
scope={scope}
code={children.replace(/\n$/, '')}
transformCode={transformCode || ((code) => `${code};`)}
transformCode={transformCode}
theme={theme}
{...props}>
language={isTypescriptCode ? 'typescript' : 'javascript'}
className={className}>
<div
className={clsx(
styles.playgroundHeader,
Expand All @@ -35,7 +77,7 @@ function Playground({children, theme, transformCode, ...props}) {
</div>
<div className={styles.playgroundPreview}>
<LivePreview />
<LiveError />
{tsTranspileError ? <pre>{tsTranspileError}</pre> : <LiveError />}
</div>
</LiveProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ function Clock(props) {
}
```

Typescript is also supported. Simply replace `jsx live` with `tsx live`, then you are good to go.

```tsx live
() => {
const [date, setDate] = useState<Readonly<Date>>(new Date());
useEffect(() => {
var timerID: number = setInterval(() => tick(), 1000);

return function cleanup() {
clearInterval(timerID);
};
}, []);

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

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

:::caution react-live and imports

It is not possible to import components directly from the react-live code editor, you have to define available imports upfront.
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,11 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/standalone@^7.12.12":
version "7.12.12"
resolved "https://registry.yarnpkg.com/@babel/standalone/-/standalone-7.12.12.tgz#f858ab1c76d9c4c23fe0783a0330ad37755f0176"
integrity sha512-sHuNDN9NvPHsDAmxPD3RpsIeqCoFSW+ySa6+3teInrYe9y0Gn5swLQ2ZE7Zk6L8eBBESZM2ob1l98qWauQfDMA==

"@babel/template@^7.10.4", "@babel/template@^7.7.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.4.tgz#3251996c4200ebc71d1a8fc405fba940f36ba278"
Expand Down