diff --git a/packages/docusaurus-theme-classic/src/options.ts b/packages/docusaurus-theme-classic/src/options.ts index 6827c069bcbb..289f4e778d9e 100644 --- a/packages/docusaurus-theme-classic/src/options.ts +++ b/packages/docusaurus-theme-classic/src/options.ts @@ -78,6 +78,9 @@ export const DEFAULT_CONFIG: ThemeConfig = { minHeadingLevel: 2, maxHeadingLevel: 3, }, + codeBlock: { + useLanguageAsTitle: false, + }, }; const NavbarItemPosition = Joi.string().equal('left', 'right').default('left'); diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/String.tsx b/packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/String.tsx index 101effde4366..abfa8a3c8d36 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/String.tsx +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/String.tsx @@ -41,6 +41,7 @@ export default function CodeBlockString({ }: Props): ReactNode { const { prism: {defaultLanguage, magicComments}, + codeBlock: {useLanguageAsTitle}, } = useThemeConfig(); const language = normalizeLanguage( languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage, @@ -52,7 +53,10 @@ export default function CodeBlockString({ // We still parse the metastring in case we want to support more syntax in the // future. Note that MDX doesn't strip quotes when parsing metastring: // "title=\"xyz\"" => title: "\"xyz\"" - const title = parseCodeBlockTitle(metastring) || titleProp; + const title = + parseCodeBlockTitle(metastring) || + titleProp || + (useLanguageAsTitle && language); const {lineClassNames, code} = parseLines(children, { metastring, diff --git a/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts b/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts index f9e13eaa1648..7c695f3fc26b 100644 --- a/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts +++ b/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts @@ -103,6 +103,10 @@ export type TableOfContents = { maxHeadingLevel: number; }; +export type CodeBlock = { + useLanguageAsTitle: boolean; +}; + // Theme config after validation/normalization export type ThemeConfig = { docs: { @@ -132,6 +136,7 @@ export type ThemeConfig = { image?: string; metadata: {[key: string]: string}[]; tableOfContents: TableOfContents; + codeBlock: CodeBlock; }; // User-provided theme config, unnormalized diff --git a/website/docusaurus.config.ts b/website/docusaurus.config.ts index c335b12735a2..6ae25461d20a 100644 --- a/website/docusaurus.config.ts +++ b/website/docusaurus.config.ts @@ -561,6 +561,7 @@ export default async function createConfigAsync() { ], themeConfig: { + codeBlock: {useLanguageAsTitle: true}, liveCodeBlock: { playgroundPosition: 'bottom', },