Skip to content

refactor(theme-common, theme-classic, theme-live-codeblock): Parse metastring into metaOptions and use it across components #11019

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

Closed
wants to merge 7 commits into from
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ declare module '@theme/BlogLayout' {

declare module '@theme/CodeBlock' {
import type {ReactNode} from 'react';
import type {CodeBlockMetaOptions} from '@docusaurus/theme-common/internal';

export interface Props {
readonly children: ReactNode;
Expand All @@ -413,6 +414,7 @@ declare module '@theme/CodeBlock' {
readonly title?: ReactNode;
readonly language?: string;
readonly showLineNumbers?: boolean | number;
readonly metaOptions?: CodeBlockMetaOptions;
}

export default function CodeBlock(props: Props): ReactNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import React, {type ReactNode} from 'react';
import clsx from 'clsx';
import {useThemeConfig, usePrismTheme} from '@docusaurus/theme-common';
import {
parseCodeBlockTitle,
parseCodeBlockMetaOptions,
parseLanguage,
parseLines,
getCodeBlockTitle,
getLineNumbersStart,
useCodeWordWrap,
} from '@docusaurus/theme-common/internal';
Expand Down Expand Up @@ -39,6 +40,7 @@ export default function CodeBlockString({
title: titleProp,
showLineNumbers: showLineNumbersProp,
language: languageProp,
metaOptions: metaOptionsProp,
}: Props): ReactNode {
const {
prism: {defaultLanguage, magicComments},
Expand All @@ -51,10 +53,15 @@ export default function CodeBlockString({
const wordWrap = useCodeWordWrap();
const isBrowser = useIsBrowser();

const metaOptions = parseCodeBlockMetaOptions(metastring, metaOptionsProp);

// 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 = getCodeBlockTitle({
titleProp,
metaOptions,
});

const {lineClassNames, code} = parseLines(children, {
metastring,
Expand All @@ -63,7 +70,7 @@ export default function CodeBlockString({
});
const lineNumbersStart = getLineNumbersStart({
showLineNumbers: showLineNumbersProp,
metastring,
metaOptions,
});

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-theme-common/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export {ColorModeProvider} from './contexts/colorMode';
export {useAlternatePageUtils} from './utils/useAlternatePageUtils';

export {
parseCodeBlockTitle,
parseCodeBlockMetaOptions,
parseLanguage,
parseLines,
getCodeBlockTitle,
getLineNumbersStart,
type CodeBlockMetaOptions,
} from './utils/codeBlockUtils';

export {DEFAULT_SEARCH_TAG} from './utils/searchUtils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getLineNumbersStart handles metadata combined with other options set as flag 1`] = `1`;
exports[`getCodeBlockTitle returns option with empty prop 1`] = `"Option"`;

exports[`getLineNumbersStart handles metadata combined with other options set with number 1`] = `10`;
exports[`getCodeBlockTitle returns option with filled prop 1`] = `"Option"`;

exports[`getLineNumbersStart handles metadata standalone set as flag 1`] = `1`;
exports[`getCodeBlockTitle returns option with undefined prop 1`] = `"Option"`;

exports[`getLineNumbersStart handles metadata standalone set with number 1`] = `10`;
exports[`getCodeBlockTitle returns titleProp with empty options 1`] = `"Prop"`;

exports[`getLineNumbersStart handles prop combined with metastring set to false 1`] = `undefined`;
exports[`getCodeBlockTitle returns titleProp with empty string on option 1`] = `"Prop"`;

exports[`getLineNumbersStart handles prop combined with metastring set to number 1`] = `10`;
exports[`getCodeBlockTitle with nothing set 1`] = `undefined`;

exports[`getLineNumbersStart handles prop combined with metastring set to true 1`] = `1`;
exports[`getLineNumbersStart from metastring parses flags as 1 1`] = `1`;

exports[`getLineNumbersStart handles prop standalone set to false 1`] = `undefined`;
exports[`getLineNumbersStart from metastring parses value 1`] = `10`;

exports[`getLineNumbersStart handles prop standalone set to number 1`] = `10`;
exports[`getLineNumbersStart with parsed metaoption handles metadata combined with other options set as flag 1`] = `1`;

exports[`getLineNumbersStart handles prop standalone set to true 1`] = `1`;
exports[`getLineNumbersStart with parsed metaoption handles metadata combined with other options set with number 1`] = `10`;

exports[`getLineNumbersStart with nothing set 1`] = `undefined`;
exports[`getLineNumbersStart with parsed metaoption handles metadata standalone set as flag 1`] = `1`;

exports[`getLineNumbersStart with nothing set 2`] = `undefined`;
exports[`getLineNumbersStart with parsed metaoption handles metadata standalone set with number 1`] = `10`;

exports[`getLineNumbersStart with parsed metaoption handles prop combined with metaoptions set to false 1`] = `undefined`;

exports[`getLineNumbersStart with parsed metaoption handles prop combined with metaoptions set to number 1`] = `10`;

exports[`getLineNumbersStart with parsed metaoption handles prop combined with metaoptions set to true 1`] = `1`;

exports[`getLineNumbersStart with parsed metaoption handles prop standalone set to false 1`] = `undefined`;

exports[`getLineNumbersStart with parsed metaoption handles prop standalone set to number 1`] = `10`;

exports[`getLineNumbersStart with parsed metaoption handles prop standalone set to true 1`] = `1`;

exports[`getLineNumbersStart with parsed metaoption with nothing set 1`] = `undefined`;

exports[`getLineNumbersStart with parsed metaoption with nothing set 2`] = `undefined`;

exports[`parseLines does not parse content with metastring 1`] = `
{
Expand Down
Loading
Loading