-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): Add playgroundPosition config for live codeblock (#4328)
* docs(v2): Add configuration parameter to allow putting Result before Editor in @docusaurus/theme-live-codeblock * update doc * refactor as playgroundPosition Co-authored-by: slorber <lorber.sebastien@gmail.com>
- Loading branch information
Showing
9 changed files
with
214 additions
and
38 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
91 changes: 91 additions & 0 deletions
91
packages/docusaurus-theme-live-codeblock/src/__tests__/validateThemeConfig.test.js
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,91 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
const {validateThemeConfig, DEFAULT_CONFIG} = require('../validateThemeConfig'); | ||
|
||
function testValidateThemeConfig(themeConfig) { | ||
function validate(schema, cfg) { | ||
const {value, error} = schema.validate(cfg, { | ||
convert: false, | ||
}); | ||
if (error) { | ||
throw error; | ||
} | ||
return value; | ||
} | ||
|
||
return validateThemeConfig({validate, themeConfig}); | ||
} | ||
|
||
describe('validateThemeConfig', () => { | ||
test('undefined config', () => { | ||
const liveCodeBlock = undefined; | ||
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({ | ||
liveCodeBlock: { | ||
...DEFAULT_CONFIG, | ||
}, | ||
}); | ||
}); | ||
|
||
test('unexist config', () => { | ||
expect(testValidateThemeConfig({})).toEqual({ | ||
liveCodeBlock: { | ||
...DEFAULT_CONFIG, | ||
}, | ||
}); | ||
}); | ||
|
||
test('empty config', () => { | ||
const liveCodeBlock = {}; | ||
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({ | ||
liveCodeBlock: { | ||
...DEFAULT_CONFIG, | ||
}, | ||
}); | ||
}); | ||
|
||
test('playgroundPosition top', () => { | ||
const liveCodeBlock = { | ||
playgroundPosition: 'top', | ||
}; | ||
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({ | ||
liveCodeBlock: { | ||
...DEFAULT_CONFIG, | ||
...liveCodeBlock, | ||
}, | ||
}); | ||
}); | ||
|
||
test('playgroundPosition bottom', () => { | ||
const liveCodeBlock = { | ||
playgroundPosition: 'bottom', | ||
}; | ||
expect(testValidateThemeConfig({liveCodeBlock})).toEqual({ | ||
liveCodeBlock: { | ||
...DEFAULT_CONFIG, | ||
...liveCodeBlock, | ||
}, | ||
}); | ||
}); | ||
|
||
test('playgroundPosition invalid string', () => { | ||
const liveCodeBlock = {playgroundPosition: 'invalid'}; | ||
expect(() => | ||
testValidateThemeConfig({liveCodeBlock}), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"\\"liveCodeBlock.playgroundPosition\\" must be one of [top, bottom]"`, | ||
); | ||
}); | ||
test('playgroundPosition invalid boolean', () => { | ||
const liveCodeBlock = {playgroundPosition: true}; | ||
expect(() => | ||
testValidateThemeConfig({liveCodeBlock}), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"\\"liveCodeBlock.playgroundPosition\\" must be one of [top, bottom]"`, | ||
); | ||
}); | ||
}); |
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
28 changes: 28 additions & 0 deletions
28
packages/docusaurus-theme-live-codeblock/src/validateThemeConfig.js
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,28 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
const Joi = require('joi'); | ||
|
||
const DEFAULT_CONFIG = { | ||
playgroundPosition: 'bottom', | ||
}; | ||
exports.DEFAULT_CONFIG = DEFAULT_CONFIG; | ||
|
||
const Schema = Joi.object({ | ||
liveCodeBlock: Joi.object({ | ||
playgroundPosition: Joi.string() | ||
.equal('top', 'bottom') | ||
.default(DEFAULT_CONFIG.playgroundPosition), | ||
}) | ||
.label('themeConfig.liveCodeBlock') | ||
.default(DEFAULT_CONFIG), | ||
}); | ||
exports.Schema = Schema; | ||
|
||
exports.validateThemeConfig = function ({validate, themeConfig}) { | ||
return validate(Schema, themeConfig); | ||
}; |
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