mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(theme): make Prism code block language / additionalLanguages case insensitive (#9183)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
b96004e77e
commit
1e40943176
5 changed files with 61 additions and 7 deletions
|
@ -38,8 +38,8 @@ describe('themeConfig', () => {
|
||||||
prism: {
|
prism: {
|
||||||
theme,
|
theme,
|
||||||
darkTheme,
|
darkTheme,
|
||||||
defaultLanguage: 'javascript',
|
defaultLanguage: 'javaSCRIPT',
|
||||||
additionalLanguages: ['kotlin', 'java'],
|
additionalLanguages: ['koTLin', 'jaVa'],
|
||||||
magicComments: [
|
magicComments: [
|
||||||
{
|
{
|
||||||
className: 'theme-code-block-highlighted-line',
|
className: 'theme-code-block-highlighted-line',
|
||||||
|
@ -126,6 +126,12 @@ describe('themeConfig', () => {
|
||||||
expect(testValidateThemeConfig(userConfig)).toEqual({
|
expect(testValidateThemeConfig(userConfig)).toEqual({
|
||||||
...DEFAULT_CONFIG,
|
...DEFAULT_CONFIG,
|
||||||
...userConfig,
|
...userConfig,
|
||||||
|
prism: {
|
||||||
|
...userConfig.prism,
|
||||||
|
// Modified/normalized values
|
||||||
|
defaultLanguage: 'javascript',
|
||||||
|
additionalLanguages: ['kotlin', 'java'],
|
||||||
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -313,6 +313,10 @@ const LogoSchema = Joi.object({
|
||||||
className: Joi.string(),
|
className: Joi.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Normalize prism language to lowercase
|
||||||
|
// See https://github.com/facebook/docusaurus/issues/9012
|
||||||
|
const PrismLanguage = Joi.string().custom((val) => val.toLowerCase());
|
||||||
|
|
||||||
export const ThemeConfigSchema = Joi.object<ThemeConfig>({
|
export const ThemeConfigSchema = Joi.object<ThemeConfig>({
|
||||||
// TODO temporary (@alpha-58)
|
// TODO temporary (@alpha-58)
|
||||||
// @ts-expect-error: forbidden
|
// @ts-expect-error: forbidden
|
||||||
|
@ -385,9 +389,9 @@ export const ThemeConfigSchema = Joi.object<ThemeConfig>({
|
||||||
plain: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
|
plain: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
|
||||||
styles: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
|
styles: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
|
||||||
}),
|
}),
|
||||||
defaultLanguage: Joi.string(),
|
defaultLanguage: PrismLanguage,
|
||||||
additionalLanguages: Joi.array()
|
additionalLanguages: Joi.array()
|
||||||
.items(Joi.string())
|
.items(PrismLanguage)
|
||||||
.default(DEFAULT_CONFIG.prism.additionalLanguages),
|
.default(DEFAULT_CONFIG.prism.additionalLanguages),
|
||||||
magicComments: Joi.array()
|
magicComments: Joi.array()
|
||||||
.items(
|
.items(
|
||||||
|
|
|
@ -24,6 +24,13 @@ import type {Props} from '@theme/CodeBlock/Content/String';
|
||||||
|
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
|
// 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: string | undefined): string | undefined {
|
||||||
|
return language?.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
export default function CodeBlockString({
|
export default function CodeBlockString({
|
||||||
children,
|
children,
|
||||||
className: blockClassName = '',
|
className: blockClassName = '',
|
||||||
|
@ -35,8 +42,10 @@ export default function CodeBlockString({
|
||||||
const {
|
const {
|
||||||
prism: {defaultLanguage, magicComments},
|
prism: {defaultLanguage, magicComments},
|
||||||
} = useThemeConfig();
|
} = useThemeConfig();
|
||||||
const language =
|
const language = normalizeLanguage(
|
||||||
languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage;
|
languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage,
|
||||||
|
);
|
||||||
|
|
||||||
const prismTheme = usePrismTheme();
|
const prismTheme = usePrismTheme();
|
||||||
const wordWrap = useCodeWordWrap();
|
const wordWrap = useCodeWordWrap();
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,41 @@ See:
|
||||||
- https://github.com/facebook/docusaurus/pull/3749
|
- https://github.com/facebook/docusaurus/pull/3749
|
||||||
- https://github.com/facebook/docusaurus/pull/6177
|
- https://github.com/facebook/docusaurus/pull/6177
|
||||||
|
|
||||||
|
## Code block prism language tests
|
||||||
|
|
||||||
|
Code block with/without the good prism language case(lower or upper) in `additionalLanguages[]`
|
||||||
|
|
||||||
|
```php title="php"
|
||||||
|
<?php
|
||||||
|
$x=15;
|
||||||
|
$y=30;
|
||||||
|
$z=$x+$y;
|
||||||
|
echo "Sum: ",$z;
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
|
||||||
|
```PHP title="PHP"
|
||||||
|
<?php
|
||||||
|
$x=15;
|
||||||
|
$y=30;
|
||||||
|
$z=$x+$y;
|
||||||
|
echo "Sum: ",$z;
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
|
||||||
|
```pHp title="pHp"
|
||||||
|
<?php
|
||||||
|
$x=15;
|
||||||
|
$y=30;
|
||||||
|
$z=$x+$y;
|
||||||
|
echo "Sum: ",$z;
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
|
||||||
|
See:
|
||||||
|
|
||||||
|
- https://github.com/facebook/docusaurus/pull/9183
|
||||||
|
|
||||||
## `pre`
|
## `pre`
|
||||||
|
|
||||||
### `pre > string`
|
### `pre > string`
|
||||||
|
|
|
@ -461,7 +461,7 @@ module.exports = async function createConfigAsync() {
|
||||||
content: `⭐️ If you like Docusaurus, give it a star on <a target="_blank" rel="noopener noreferrer" href="https://github.com/facebook/docusaurus">GitHub</a> and follow us on <a target="_blank" rel="noopener noreferrer" href="https://twitter.com/docusaurus">Twitter ${TwitterSvg}</a>`,
|
content: `⭐️ If you like Docusaurus, give it a star on <a target="_blank" rel="noopener noreferrer" href="https://github.com/facebook/docusaurus">GitHub</a> and follow us on <a target="_blank" rel="noopener noreferrer" href="https://twitter.com/docusaurus">Twitter ${TwitterSvg}</a>`,
|
||||||
},
|
},
|
||||||
prism: {
|
prism: {
|
||||||
additionalLanguages: ['java', 'latex'],
|
additionalLanguages: ['java', 'latex', 'PHp'],
|
||||||
magicComments: [
|
magicComments: [
|
||||||
{
|
{
|
||||||
className: 'theme-code-block-highlighted-line',
|
className: 'theme-code-block-highlighted-line',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue