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(theme): add option to use language as title for code blocks #10809

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions packages/docusaurus-theme-classic/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export const DEFAULT_CONFIG: ThemeConfig = {
minHeadingLevel: 2,
maxHeadingLevel: 3,
},
codeBlock: {
useLanguageAsTitle: false,
},
Comment on lines +81 to +83
Copy link
Collaborator

Choose a reason for hiding this comment

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

CI fails because defining DEFAULT_CONFIG is not enough, you also have to declare a Joi schema and define defaults for themeConfig.codeBlock and themeConfig.codeBlock.useLanguageAsTitle otherwise users providing themeConfig: {} will not have these fields by default.

};

const NavbarItemPosition = Joi.string().equal('left', 'right').default('left');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function CodeBlockString({
}: Props): ReactNode {
const {
prism: {defaultLanguage, magicComments},
codeBlock: {useLanguageAsTitle},
} = useThemeConfig();
const language = normalizeLanguage(
languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export type TableOfContents = {
maxHeadingLevel: number;
};

export type CodeBlock = {
useLanguageAsTitle: boolean;
};

// Theme config after validation/normalization
export type ThemeConfig = {
docs: {
Expand Down Expand Up @@ -132,6 +136,7 @@ export type ThemeConfig = {
image?: string;
metadata: {[key: string]: string}[];
tableOfContents: TableOfContents;
codeBlock: CodeBlock;
};

// User-provided theme config, unnormalized
Expand Down
1 change: 1 addition & 0 deletions website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ export default async function createConfigAsync() {
],

themeConfig: {
codeBlock: {useLanguageAsTitle: true},
liveCodeBlock: {
playgroundPosition: 'bottom',
},
Expand Down
Loading