-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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-common, theme-classic): Improve CodeBlock Extensibility #11011
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ import React, {type ReactNode} from 'react'; | |
import clsx from 'clsx'; | ||
import {useThemeConfig, usePrismTheme} from '@docusaurus/theme-common'; | ||
import { | ||
parseCodeBlockTitle, | ||
parseLanguage, | ||
parseLines, | ||
getLineNumbersStart, | ||
|
@@ -51,19 +50,19 @@ export default function CodeBlockString({ | |
const wordWrap = useCodeWordWrap(); | ||
const isBrowser = useIsBrowser(); | ||
|
||
// 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 {lineClassNames, code} = parseLines(children, { | ||
const {meta, code} = parseLines(children, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why Can't we provide the meta as input? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be changed. The decision behind this was:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not sure to understand, this method's responsibility is not really parsing the metastring, but parsing lines magic comments.
I'm not sure about the "Metadata Magic Comments" feature. Even if we eventually introduce this, we'll only do this later once everything is in a better shape, and your design should be researched/challenged. It's always possible to refactor again later once we are sure we want this feature, but for now please ignore it. |
||
metastring, | ||
language, | ||
magicComments, | ||
}); | ||
|
||
// Note that MDX doesn't strip quotes when parsing metastring: | ||
// "title=\"xyz\"" => title: "\"xyz\"" | ||
const title = meta.options.title ?? titleProp; | ||
|
||
const lineNumbersStart = getLineNumbersStart({ | ||
showLineNumbers: showLineNumbersProp, | ||
metastring, | ||
meta, | ||
}); | ||
|
||
return ( | ||
|
@@ -105,7 +104,8 @@ export default function CodeBlockString({ | |
line={line} | ||
getLineProps={getLineProps} | ||
getTokenProps={getTokenProps} | ||
classNames={lineClassNames[i]} | ||
classNames={meta.lineClassNames[i]} | ||
meta={meta} | ||
showLineNumbers={lineNumbersStart !== undefined} | ||
/> | ||
))} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import React, {type ReactNode} from 'react'; | ||
import type {Props} from '@theme/CodeBlock/Token'; | ||
|
||
export default function CodeBlockToken({output}: Props): ReactNode { | ||
return <span {...output} />; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TokenOutputProps
props are directly applicable to spansI'd prefer to have
Eventually, we could introduce a React code block context to provide the meta to all the subtree. This makes it easier to reduce coupling between code block components, by avoiding passing global context through props (less likely to break on swizzle)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this before but decided not to inherit but composite because:
meta
should not be applied to span. Hence you end up with something similarconst {meta, rest} = props; ... (<span {...rest} />)
. (I noticed that CodeBlockToken has a bug not having<span {...props.output} />
like it should and<CodeBlockToken />
not receivingmeta
Generally I'm fine with the change, just wanted to be sure we consider those aspects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awkward to me to have
{...props.output}
, and why we have to name a thingoutput
in the first place. The component shouldn't be designed according to names chosen by a third-party library, it must make sense in isolation. Would you use the name "output" if you weren't using react-prism-renderer for example, and were simply designing a standalone code block token component?If meta is accessible through context, there's no need to
{meta, ...rest}
because meta is not a prop.