@docusaurus/theme-live-codeblock 2.0.0-beta.12faed89d → 2.0.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/custom-buble.d.ts +9 -0
- package/lib/custom-buble.js +26 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +39 -0
- package/lib/theme/CodeBlock/index.js +25 -0
- package/lib/theme/Playground/index.js +110 -0
- package/lib/theme/Playground/styles.module.css +40 -0
- package/lib/theme/ReactLiveScope/index.js +16 -0
- package/lib/validateThemeConfig.d.ts +17 -0
- package/lib/validateThemeConfig.js +28 -0
- package/package.json +20 -8
- package/src/{custom-buble.js → custom-buble.ts} +21 -5
- package/src/deps.d.ts +19 -0
- package/src/index.ts +43 -0
- package/src/theme/CodeBlock/index.js +2 -2
- package/src/theme/Playground/index.js +30 -6
- package/src/theme/Playground/styles.module.css +4 -8
- package/src/{validateThemeConfig.js → validateThemeConfig.ts} +12 -5
- package/src/__tests__/validateThemeConfig.test.js +0 -91
- package/src/index.js +0 -33
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import { features as bubleFeatures, TransformOptions, TransformOutput } from '@philpl/buble';
|
|
8
|
+
export { bubleFeatures as features };
|
|
9
|
+
export declare function transform(source: string, options: TransformOptions): TransformOutput;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.transform = exports.features = void 0;
|
|
10
|
+
// fork of Buble which removes Buble's large dependency and weighs in
|
|
11
|
+
// at a smaller size of ~51kB
|
|
12
|
+
// https://github.com/FormidableLabs/react-live#what-bundle-size-can-i-expect
|
|
13
|
+
const buble_1 = require("@philpl/buble");
|
|
14
|
+
Object.defineProperty(exports, "features", { enumerable: true, get: function () { return buble_1.features; } });
|
|
15
|
+
function transform(source, options) {
|
|
16
|
+
return (0, buble_1.transform)(source, {
|
|
17
|
+
...options,
|
|
18
|
+
transforms: {
|
|
19
|
+
asyncAwait: false,
|
|
20
|
+
classes: false,
|
|
21
|
+
getterSetter: false,
|
|
22
|
+
...options.transforms,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports.transform = transform;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import { DocusaurusContext, Plugin } from '@docusaurus/types';
|
|
8
|
+
export default function theme(context: DocusaurusContext): Plugin;
|
|
9
|
+
export { validateThemeConfig } from './validateThemeConfig';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.validateThemeConfig = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
12
|
+
const theme_translations_1 = require("@docusaurus/theme-translations");
|
|
13
|
+
function theme(context) {
|
|
14
|
+
const { i18n: { currentLocale }, } = context;
|
|
15
|
+
return {
|
|
16
|
+
name: 'docusaurus-theme-live-codeblock',
|
|
17
|
+
getThemePath() {
|
|
18
|
+
return path_1.default.resolve(__dirname, './theme');
|
|
19
|
+
},
|
|
20
|
+
getDefaultCodeTranslationMessages() {
|
|
21
|
+
return (0, theme_translations_1.readDefaultCodeTranslationMessages)({
|
|
22
|
+
locale: currentLocale,
|
|
23
|
+
name: 'theme-live-codeblock',
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
configureWebpack() {
|
|
27
|
+
return {
|
|
28
|
+
resolve: {
|
|
29
|
+
alias: {
|
|
30
|
+
buble: path_1.default.resolve(__dirname, './custom-buble.js'),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.default = theme;
|
|
38
|
+
var validateThemeConfig_1 = require("./validateThemeConfig");
|
|
39
|
+
Object.defineProperty(exports, "validateThemeConfig", { enumerable: true, get: function () { return validateThemeConfig_1.validateThemeConfig; } });
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React from 'react';
|
|
9
|
+
import Playground from '@theme/Playground';
|
|
10
|
+
import ReactLiveScope from '@theme/ReactLiveScope';
|
|
11
|
+
import CodeBlock from '@theme-init/CodeBlock';
|
|
12
|
+
|
|
13
|
+
const withLiveEditor = (Component) => {
|
|
14
|
+
function WrappedComponent(props) {
|
|
15
|
+
if (props.live) {
|
|
16
|
+
return <Playground scope={ReactLiveScope} {...props} />;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return <Component {...props} />;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return WrappedComponent;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default withLiveEditor(CodeBlock);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as React from 'react';
|
|
9
|
+
import {LiveProvider, LiveEditor, LiveError, LivePreview} from 'react-live';
|
|
10
|
+
import clsx from 'clsx';
|
|
11
|
+
import Translate from '@docusaurus/Translate';
|
|
12
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
13
|
+
import BrowserOnly from '@docusaurus/BrowserOnly';
|
|
14
|
+
import usePrismTheme from '@theme/hooks/usePrismTheme';
|
|
15
|
+
import styles from './styles.module.css';
|
|
16
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
17
|
+
|
|
18
|
+
function Header({children}) {
|
|
19
|
+
return <div className={clsx(styles.playgroundHeader)}>{children}</div>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function LivePreviewLoader() {
|
|
23
|
+
// Is it worth improving/translating?
|
|
24
|
+
return <div>Loading...</div>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ResultWithHeader() {
|
|
28
|
+
return (
|
|
29
|
+
<>
|
|
30
|
+
<Header>
|
|
31
|
+
<Translate
|
|
32
|
+
id="theme.Playground.result"
|
|
33
|
+
description="The result label of the live codeblocks">
|
|
34
|
+
Result
|
|
35
|
+
</Translate>
|
|
36
|
+
</Header>
|
|
37
|
+
{/* https://github.com/facebook/docusaurus/issues/5747 */}
|
|
38
|
+
<div className={styles.playgroundPreview}>
|
|
39
|
+
<BrowserOnly fallback={<LivePreviewLoader />}>
|
|
40
|
+
{() => (
|
|
41
|
+
<>
|
|
42
|
+
<LivePreview />
|
|
43
|
+
<LiveError />
|
|
44
|
+
</>
|
|
45
|
+
)}
|
|
46
|
+
</BrowserOnly>
|
|
47
|
+
</div>
|
|
48
|
+
</>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function ThemedLiveEditor() {
|
|
53
|
+
const isBrowser = useIsBrowser();
|
|
54
|
+
return (
|
|
55
|
+
<LiveEditor
|
|
56
|
+
// We force remount the editor on hydration,
|
|
57
|
+
// otherwise dark prism theme is not applied
|
|
58
|
+
key={isBrowser}
|
|
59
|
+
className={styles.playgroundEditor}
|
|
60
|
+
/>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function EditorWithHeader() {
|
|
65
|
+
return (
|
|
66
|
+
<>
|
|
67
|
+
<Header>
|
|
68
|
+
<Translate
|
|
69
|
+
id="theme.Playground.liveEditor"
|
|
70
|
+
description="The live editor label of the live codeblocks">
|
|
71
|
+
Live Editor
|
|
72
|
+
</Translate>
|
|
73
|
+
</Header>
|
|
74
|
+
<ThemedLiveEditor />
|
|
75
|
+
</>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default function Playground({children, transformCode, ...props}) {
|
|
80
|
+
const {
|
|
81
|
+
siteConfig: {
|
|
82
|
+
themeConfig: {
|
|
83
|
+
liveCodeBlock: {playgroundPosition},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
} = useDocusaurusContext();
|
|
87
|
+
const prismTheme = usePrismTheme();
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div className={styles.playgroundContainer}>
|
|
91
|
+
<LiveProvider
|
|
92
|
+
code={children.replace(/\n$/, '')}
|
|
93
|
+
transformCode={transformCode || ((code) => `${code};`)}
|
|
94
|
+
theme={prismTheme}
|
|
95
|
+
{...props}>
|
|
96
|
+
{playgroundPosition === 'top' ? (
|
|
97
|
+
<>
|
|
98
|
+
<ResultWithHeader />
|
|
99
|
+
<EditorWithHeader />
|
|
100
|
+
</>
|
|
101
|
+
) : (
|
|
102
|
+
<>
|
|
103
|
+
<EditorWithHeader />
|
|
104
|
+
<ResultWithHeader />
|
|
105
|
+
</>
|
|
106
|
+
)}
|
|
107
|
+
</LiveProvider>
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
.playgroundContainer {
|
|
9
|
+
margin-bottom: var(--ifm-leading);
|
|
10
|
+
border-radius: var(--ifm-global-radius);
|
|
11
|
+
box-shadow: var(--ifm-global-shadow-lw);
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.playgroundHeader {
|
|
16
|
+
letter-spacing: 0.08rem;
|
|
17
|
+
padding: 0.75rem;
|
|
18
|
+
text-transform: uppercase;
|
|
19
|
+
font-weight: bold;
|
|
20
|
+
background: var(--ifm-color-emphasis-200);
|
|
21
|
+
color: var(--ifm-color-content);
|
|
22
|
+
font-size: var(--ifm-code-font-size);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.playgroundHeader:first-of-type {
|
|
26
|
+
background: var(--ifm-color-emphasis-600);
|
|
27
|
+
color: var(--ifm-color-content-inverse);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.playgroundEditor {
|
|
31
|
+
font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
|
|
32
|
+
var(--ifm-font-family-monospace) !important;
|
|
33
|
+
/*rtl:ignore*/
|
|
34
|
+
direction: ltr;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.playgroundPreview {
|
|
38
|
+
padding: 1rem;
|
|
39
|
+
background-color: var(--ifm-pre-background);
|
|
40
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React from 'react';
|
|
9
|
+
|
|
10
|
+
// Add react-live imports you need here
|
|
11
|
+
const ReactLiveScope = {
|
|
12
|
+
React,
|
|
13
|
+
...React,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default ReactLiveScope;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import { Joi } from '@docusaurus/utils-validation';
|
|
8
|
+
import type { ThemeConfig, Validate, ValidationResult } from '@docusaurus/types';
|
|
9
|
+
declare const DEFAULT_CONFIG: {
|
|
10
|
+
playgroundPosition: string;
|
|
11
|
+
};
|
|
12
|
+
declare const Schema: Joi.ObjectSchema<any>;
|
|
13
|
+
declare function validateThemeConfig({ validate, themeConfig, }: {
|
|
14
|
+
validate: Validate<ThemeConfig>;
|
|
15
|
+
themeConfig: ThemeConfig;
|
|
16
|
+
}): ValidationResult<ThemeConfig>;
|
|
17
|
+
export { DEFAULT_CONFIG, Schema, validateThemeConfig };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.validateThemeConfig = exports.Schema = exports.DEFAULT_CONFIG = void 0;
|
|
10
|
+
const utils_validation_1 = require("@docusaurus/utils-validation");
|
|
11
|
+
const DEFAULT_CONFIG = {
|
|
12
|
+
playgroundPosition: 'bottom',
|
|
13
|
+
};
|
|
14
|
+
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
|
|
15
|
+
const Schema = utils_validation_1.Joi.object({
|
|
16
|
+
liveCodeBlock: utils_validation_1.Joi.object({
|
|
17
|
+
playgroundPosition: utils_validation_1.Joi.string()
|
|
18
|
+
.equal('top', 'bottom')
|
|
19
|
+
.default(DEFAULT_CONFIG.playgroundPosition),
|
|
20
|
+
})
|
|
21
|
+
.label('themeConfig.liveCodeBlock')
|
|
22
|
+
.default(DEFAULT_CONFIG),
|
|
23
|
+
});
|
|
24
|
+
exports.Schema = Schema;
|
|
25
|
+
function validateThemeConfig({ validate, themeConfig, }) {
|
|
26
|
+
return validate(Schema, themeConfig);
|
|
27
|
+
}
|
|
28
|
+
exports.validateThemeConfig = validateThemeConfig;
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/theme-live-codeblock",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.14",
|
|
4
4
|
"description": "Docusaurus live code block component.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc && node copyUntypedFiles.js",
|
|
11
|
+
"watch": "node copyUntypedFiles.js && tsc --watch"
|
|
12
|
+
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
15
|
"url": "https://github.com/facebook/docusaurus.git",
|
|
@@ -13,20 +17,28 @@
|
|
|
13
17
|
},
|
|
14
18
|
"license": "MIT",
|
|
15
19
|
"dependencies": {
|
|
16
|
-
"@docusaurus/core": "2.0.0-beta.
|
|
17
|
-
"@docusaurus/
|
|
20
|
+
"@docusaurus/core": "2.0.0-beta.14",
|
|
21
|
+
"@docusaurus/theme-translations": "2.0.0-beta.14",
|
|
22
|
+
"@docusaurus/utils": "2.0.0-beta.14",
|
|
23
|
+
"@docusaurus/utils-validation": "2.0.0-beta.14",
|
|
18
24
|
"@philpl/buble": "^0.19.7",
|
|
19
25
|
"clsx": "^1.1.1",
|
|
20
|
-
"
|
|
26
|
+
"fs-extra": "^10.0.0",
|
|
27
|
+
"parse-numeric-range": "^1.3.0",
|
|
21
28
|
"prism-react-renderer": "^1.2.1",
|
|
22
|
-
"react-live": "
|
|
29
|
+
"react-live": "2.2.3",
|
|
30
|
+
"tslib": "^2.3.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@docusaurus/types": "2.0.0-beta.14",
|
|
34
|
+
"@types/buble": "^0.20.1"
|
|
23
35
|
},
|
|
24
36
|
"peerDependencies": {
|
|
25
37
|
"react": "^16.8.4 || ^17.0.0",
|
|
26
38
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
27
39
|
},
|
|
28
40
|
"engines": {
|
|
29
|
-
"node": ">=
|
|
41
|
+
"node": ">=14"
|
|
30
42
|
},
|
|
31
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "c4824a8937d8f1aa0806667749cbc74058e2b294"
|
|
32
44
|
}
|
|
@@ -8,13 +8,29 @@
|
|
|
8
8
|
// fork of Buble which removes Buble's large dependency and weighs in
|
|
9
9
|
// at a smaller size of ~51kB
|
|
10
10
|
// https://github.com/FormidableLabs/react-live#what-bundle-size-can-i-expect
|
|
11
|
-
|
|
11
|
+
import {
|
|
12
|
+
transform as bubleTransform,
|
|
13
|
+
features as bubleFeatures,
|
|
14
|
+
TransformOptions,
|
|
15
|
+
TransformOutput,
|
|
16
|
+
} from '@philpl/buble';
|
|
12
17
|
|
|
13
18
|
// This file is designed to mimic what's written in
|
|
14
19
|
// https://github.com/kitten/buble/blob/mini/src/index.js, with custom transforms options,
|
|
15
20
|
// so that webpack can consume it correctly.
|
|
16
|
-
|
|
21
|
+
export {bubleFeatures as features};
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
export function transform(
|
|
24
|
+
source: string,
|
|
25
|
+
options: TransformOptions,
|
|
26
|
+
): TransformOutput {
|
|
27
|
+
return bubleTransform(source, {
|
|
28
|
+
...options,
|
|
29
|
+
transforms: {
|
|
30
|
+
asyncAwait: false,
|
|
31
|
+
classes: false,
|
|
32
|
+
getterSetter: false,
|
|
33
|
+
...options.transforms,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
package/src/deps.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare module '@philpl/buble' {
|
|
9
|
+
import type {TransformOptions as OriginalTransformOptions} from 'buble';
|
|
10
|
+
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
11
|
+
export * from 'buble';
|
|
12
|
+
export const features: string[];
|
|
13
|
+
export interface TransformOptions extends OriginalTransformOptions {
|
|
14
|
+
transforms?: OriginalTransformOptions['transforms'] & {
|
|
15
|
+
asyncAwait?: boolean;
|
|
16
|
+
getterSetter?: boolean;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
|
|
10
|
+
import {DocusaurusContext, Plugin} from '@docusaurus/types';
|
|
11
|
+
|
|
12
|
+
export default function theme(context: DocusaurusContext): Plugin {
|
|
13
|
+
const {
|
|
14
|
+
i18n: {currentLocale},
|
|
15
|
+
} = context;
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
name: 'docusaurus-theme-live-codeblock',
|
|
19
|
+
|
|
20
|
+
getThemePath() {
|
|
21
|
+
return path.resolve(__dirname, './theme');
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
getDefaultCodeTranslationMessages() {
|
|
25
|
+
return readDefaultCodeTranslationMessages({
|
|
26
|
+
locale: currentLocale,
|
|
27
|
+
name: 'theme-live-codeblock',
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
configureWebpack() {
|
|
32
|
+
return {
|
|
33
|
+
resolve: {
|
|
34
|
+
alias: {
|
|
35
|
+
buble: path.resolve(__dirname, './custom-buble.js'),
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {validateThemeConfig} from './validateThemeConfig';
|
|
@@ -11,13 +11,13 @@ import ReactLiveScope from '@theme/ReactLiveScope';
|
|
|
11
11
|
import CodeBlock from '@theme-init/CodeBlock';
|
|
12
12
|
|
|
13
13
|
const withLiveEditor = (Component) => {
|
|
14
|
-
|
|
14
|
+
function WrappedComponent(props) {
|
|
15
15
|
if (props.live) {
|
|
16
16
|
return <Playground scope={ReactLiveScope} {...props} />;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
return <Component {...props} />;
|
|
20
|
-
}
|
|
20
|
+
}
|
|
21
21
|
|
|
22
22
|
return WrappedComponent;
|
|
23
23
|
};
|
|
@@ -10,13 +10,20 @@ import {LiveProvider, LiveEditor, LiveError, LivePreview} from 'react-live';
|
|
|
10
10
|
import clsx from 'clsx';
|
|
11
11
|
import Translate from '@docusaurus/Translate';
|
|
12
12
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
13
|
+
import BrowserOnly from '@docusaurus/BrowserOnly';
|
|
13
14
|
import usePrismTheme from '@theme/hooks/usePrismTheme';
|
|
14
15
|
import styles from './styles.module.css';
|
|
16
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
15
17
|
|
|
16
18
|
function Header({children}) {
|
|
17
19
|
return <div className={clsx(styles.playgroundHeader)}>{children}</div>;
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
function LivePreviewLoader() {
|
|
23
|
+
// Is it worth improving/translating?
|
|
24
|
+
return <div>Loading...</div>;
|
|
25
|
+
}
|
|
26
|
+
|
|
20
27
|
function ResultWithHeader() {
|
|
21
28
|
return (
|
|
22
29
|
<>
|
|
@@ -27,14 +34,33 @@ function ResultWithHeader() {
|
|
|
27
34
|
Result
|
|
28
35
|
</Translate>
|
|
29
36
|
</Header>
|
|
37
|
+
{/* https://github.com/facebook/docusaurus/issues/5747 */}
|
|
30
38
|
<div className={styles.playgroundPreview}>
|
|
31
|
-
<
|
|
32
|
-
|
|
39
|
+
<BrowserOnly fallback={<LivePreviewLoader />}>
|
|
40
|
+
{() => (
|
|
41
|
+
<>
|
|
42
|
+
<LivePreview />
|
|
43
|
+
<LiveError />
|
|
44
|
+
</>
|
|
45
|
+
)}
|
|
46
|
+
</BrowserOnly>
|
|
33
47
|
</div>
|
|
34
48
|
</>
|
|
35
49
|
);
|
|
36
50
|
}
|
|
37
51
|
|
|
52
|
+
function ThemedLiveEditor() {
|
|
53
|
+
const isBrowser = useIsBrowser();
|
|
54
|
+
return (
|
|
55
|
+
<LiveEditor
|
|
56
|
+
// We force remount the editor on hydration,
|
|
57
|
+
// otherwise dark prism theme is not applied
|
|
58
|
+
key={isBrowser}
|
|
59
|
+
className={styles.playgroundEditor}
|
|
60
|
+
/>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
38
64
|
function EditorWithHeader() {
|
|
39
65
|
return (
|
|
40
66
|
<>
|
|
@@ -45,14 +71,13 @@ function EditorWithHeader() {
|
|
|
45
71
|
Live Editor
|
|
46
72
|
</Translate>
|
|
47
73
|
</Header>
|
|
48
|
-
<
|
|
74
|
+
<ThemedLiveEditor />
|
|
49
75
|
</>
|
|
50
76
|
);
|
|
51
77
|
}
|
|
52
78
|
|
|
53
79
|
export default function Playground({children, transformCode, ...props}) {
|
|
54
80
|
const {
|
|
55
|
-
isClient,
|
|
56
81
|
siteConfig: {
|
|
57
82
|
themeConfig: {
|
|
58
83
|
liveCodeBlock: {playgroundPosition},
|
|
@@ -64,8 +89,7 @@ export default function Playground({children, transformCode, ...props}) {
|
|
|
64
89
|
return (
|
|
65
90
|
<div className={styles.playgroundContainer}>
|
|
66
91
|
<LiveProvider
|
|
67
|
-
|
|
68
|
-
code={isClient ? children.replace(/\n$/, '') : ''}
|
|
92
|
+
code={children.replace(/\n$/, '')}
|
|
69
93
|
transformCode={transformCode || ((code) => `${code};`)}
|
|
70
94
|
theme={prismTheme}
|
|
71
95
|
{...props}>
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
.playgroundContainer {
|
|
9
9
|
margin-bottom: var(--ifm-leading);
|
|
10
|
+
border-radius: var(--ifm-global-radius);
|
|
11
|
+
box-shadow: var(--ifm-global-shadow-lw);
|
|
12
|
+
overflow: hidden;
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
.playgroundHeader {
|
|
@@ -22,8 +25,6 @@
|
|
|
22
25
|
.playgroundHeader:first-of-type {
|
|
23
26
|
background: var(--ifm-color-emphasis-600);
|
|
24
27
|
color: var(--ifm-color-content-inverse);
|
|
25
|
-
border-top-left-radius: var(--ifm-global-radius);
|
|
26
|
-
border-top-right-radius: var(--ifm-global-radius);
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
.playgroundEditor {
|
|
@@ -34,11 +35,6 @@
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
.playgroundPreview {
|
|
37
|
-
border: 1px solid var(--ifm-color-emphasis-200);
|
|
38
38
|
padding: 1rem;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.playgroundPreview:last-of-type, .playgroundEditor:last-of-type {
|
|
42
|
-
border-bottom-left-radius: var(--ifm-global-radius);
|
|
43
|
-
border-bottom-right-radius: var(--ifm-global-radius);
|
|
39
|
+
background-color: var(--ifm-pre-background);
|
|
44
40
|
}
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
import {Joi} from '@docusaurus/utils-validation';
|
|
9
|
+
import type {ThemeConfig, Validate, ValidationResult} from '@docusaurus/types';
|
|
9
10
|
|
|
10
11
|
const DEFAULT_CONFIG = {
|
|
11
12
|
playgroundPosition: 'bottom',
|
|
12
13
|
};
|
|
13
|
-
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
|
|
14
14
|
|
|
15
15
|
const Schema = Joi.object({
|
|
16
16
|
liveCodeBlock: Joi.object({
|
|
@@ -21,8 +21,15 @@ const Schema = Joi.object({
|
|
|
21
21
|
.label('themeConfig.liveCodeBlock')
|
|
22
22
|
.default(DEFAULT_CONFIG),
|
|
23
23
|
});
|
|
24
|
-
exports.Schema = Schema;
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
function validateThemeConfig({
|
|
26
|
+
validate,
|
|
27
|
+
themeConfig,
|
|
28
|
+
}: {
|
|
29
|
+
validate: Validate<ThemeConfig>;
|
|
30
|
+
themeConfig: ThemeConfig;
|
|
31
|
+
}): ValidationResult<ThemeConfig> {
|
|
27
32
|
return validate(Schema, themeConfig);
|
|
28
|
-
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export {DEFAULT_CONFIG, Schema, validateThemeConfig};
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const {validateThemeConfig, DEFAULT_CONFIG} = require('../validateThemeConfig');
|
|
9
|
-
|
|
10
|
-
function testValidateThemeConfig(themeConfig) {
|
|
11
|
-
function validate(schema, cfg) {
|
|
12
|
-
const {value, error} = schema.validate(cfg, {
|
|
13
|
-
convert: false,
|
|
14
|
-
});
|
|
15
|
-
if (error) {
|
|
16
|
-
throw error;
|
|
17
|
-
}
|
|
18
|
-
return value;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return validateThemeConfig({validate, themeConfig});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
describe('validateThemeConfig', () => {
|
|
25
|
-
test('undefined config', () => {
|
|
26
|
-
const liveCodeBlock = undefined;
|
|
27
|
-
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({
|
|
28
|
-
liveCodeBlock: {
|
|
29
|
-
...DEFAULT_CONFIG,
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('unexist config', () => {
|
|
35
|
-
expect(testValidateThemeConfig({})).toEqual({
|
|
36
|
-
liveCodeBlock: {
|
|
37
|
-
...DEFAULT_CONFIG,
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test('empty config', () => {
|
|
43
|
-
const liveCodeBlock = {};
|
|
44
|
-
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({
|
|
45
|
-
liveCodeBlock: {
|
|
46
|
-
...DEFAULT_CONFIG,
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('playgroundPosition top', () => {
|
|
52
|
-
const liveCodeBlock = {
|
|
53
|
-
playgroundPosition: 'top',
|
|
54
|
-
};
|
|
55
|
-
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({
|
|
56
|
-
liveCodeBlock: {
|
|
57
|
-
...DEFAULT_CONFIG,
|
|
58
|
-
...liveCodeBlock,
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('playgroundPosition bottom', () => {
|
|
64
|
-
const liveCodeBlock = {
|
|
65
|
-
playgroundPosition: 'bottom',
|
|
66
|
-
};
|
|
67
|
-
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({
|
|
68
|
-
liveCodeBlock: {
|
|
69
|
-
...DEFAULT_CONFIG,
|
|
70
|
-
...liveCodeBlock,
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('playgroundPosition invalid string', () => {
|
|
76
|
-
const liveCodeBlock = {playgroundPosition: 'invalid'};
|
|
77
|
-
expect(() =>
|
|
78
|
-
testValidateThemeConfig({liveCodeBlock}),
|
|
79
|
-
).toThrowErrorMatchingInlineSnapshot(
|
|
80
|
-
`"\\"liveCodeBlock.playgroundPosition\\" must be one of [top, bottom]"`,
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
test('playgroundPosition invalid boolean', () => {
|
|
84
|
-
const liveCodeBlock = {playgroundPosition: true};
|
|
85
|
-
expect(() =>
|
|
86
|
-
testValidateThemeConfig({liveCodeBlock}),
|
|
87
|
-
).toThrowErrorMatchingInlineSnapshot(
|
|
88
|
-
`"\\"liveCodeBlock.playgroundPosition\\" must be one of [top, bottom]"`,
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
});
|
package/src/index.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const {validateThemeConfig} = require('./validateThemeConfig');
|
|
10
|
-
|
|
11
|
-
function theme() {
|
|
12
|
-
return {
|
|
13
|
-
name: 'docusaurus-theme-live-codeblock',
|
|
14
|
-
|
|
15
|
-
getThemePath() {
|
|
16
|
-
return path.resolve(__dirname, './theme');
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
configureWebpack() {
|
|
20
|
-
return {
|
|
21
|
-
resolve: {
|
|
22
|
-
alias: {
|
|
23
|
-
buble: path.resolve(__dirname, './custom-buble.js'),
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = theme;
|
|
32
|
-
|
|
33
|
-
theme.validateThemeConfig = validateThemeConfig;
|