You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The thing is that it is a little cumbersome if this needs to be done on each codeblock individually. This proposal is that this setting will be stored globally to set it to the users needs.
Self-service
I'd be willing to do some initial work on this proposal myself.
Reacted by Peli de Halleux, Akmurat Saktagan, Gabriel T. Nardy (Syed), Adnan Rahić, Ahmadali Shafiee, Jan Molak, Ivan Cheban, Alex , Evan, Dmitry Dygalo and 13 more
We probably won't add a button (otherwise we need a "user control panel", which may not be a totally bad idea, like how MUI is doing it). But a global option seems worthwhile.
Why would you want to wrap the code? I've almost never wanted to wrap it.
Oh I am sorry. It seems this went sideways. What I meant is when the user toggles one it should toggle all others too (to be clear, I am also not a big fan of wrapping it - but some of our customers are now using the feature as it got shipped with docusaurus).
I would propose a new theme configuration. I think with these everything should be covered:
{
codeBlockOptions: {
// this identifies if the code block should be wrapped or not on the first page load
defaultAppearance: 'default' | 'wrapped'; // default: 'default'
// if the wrap state should be stored globally or not
enableWrapSync: boolean; // default: false
// enables or disables the visual representation of the code wrap
showWrap: boolean; // default: true
};
}
{
codeBlock: {
// Option to show copy button
showCopy: boolean; // default true
// Option to show wrap button
showWrap: boolean; // default true
// Option to control default wrapping
defaultWrap: boolean; // default false;
}
}
IMHO this would be an ideal config setup for code blocks. Since these options would be in docusaurus.config.js, it can be assumed that these options are global. They could be overridden on a per instance basis by passing options to the CodeBlock component or a per page basis with a frontmatter value.
I noticed in #7769 it was suggested that this could be enabled/forced for a single code block. I have the odd code block where it makes sense to have it forced on, or at least enabled by default.
So this syntax (or similar) would be great for me:
However, most snippets are far better with word wrap disabled, so I wouldn't really want to change the default for the whole site, or to even remember it page by page.
Reacted by Daniel Di Sarli, Vipul Gupta, platformlead, Gyanendra Mishra, Bob van den Hoogen, Nick, X, Michal Srb, Milan Holemans, Jasey Waegebaert and 16 more
We started working on this in #9349, and a few questions arise because there are orthogonal needs
Should we always sync and persist in localStorage at the same time, or are these different features? IE could we sync the setting between code blocks without persisting the last choice, and vice versa?
Is sync/persistence opt-in or always enabled?
Should sync and/or persistence be enabled on a case-by-case basis, similar to how tabs work?
Do we need both a global site setting, and a code block meta-string param
If a global and/or meta string value is provided, and a localStorage value exist, what should be the value we use? We have 3 sources available to provide that value and need to figure a rule here.
As you can see, there are still design elements to discuss.
On the technical side, there are also important UX details to consider:
We need to ensure there's no layout shifts due to the sync when we click on the code wrap button (similar case to tabs we had in the past: the button position shifts and does not stay under user cursor)
We need to ensure a good React hydration experience. We don't want code blocks to initially appear with scrollbars and then shift to wrapping, producing layout shifts
As a workaround, I wrap the code block in a div with a custom class:
<divclassName="wrapcode">
```Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/to/graphile.config.tsrequire() of ES modules is not supported.require() of /path/to/graphile.config.ts from /path/to/node_modules/graphile-config/dist/loadConfig.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.Instead change the requiring code to use import(), or remove "type": "module" from /path/to/package.json.```
</div>
and added the wrapcode class to my src/css/custom.css:
I see that now Docusaurus code blocks have this code wrapping feature out-of-the-box: there's a Toggle word wrap button in the code blocks with long lines of code. Please close this issue.
Reacted by Jonathan Chappelow, Zena, Wulu, sakurawald and OhYee
@ivancheban yes, the spirit of this ticket as I understand (and I share need the for fwiw) is to enable a way (most likely some kind of param passed when defining the markdown body itself) to have that Toggle word wrap on by default
There is no current known way to do this without writing a custom css class like @benjie demo'd above, ergo ticket should stay OPEN imo
Reacted by Jonathan Chappelow, Gabriel T. Nardy (Syed), Tony Meehan, revofire, Zena and Albert Vila Calvo
We would also benefit from at least a meta tag, e.g., "wordWrap=true" or IMO better: "wrap=true" or just "wrap", as the odd codeBlock (mostly one-liner scripts) will be more readable that way. I don't think we personally would need a global config option for this.
If you want to implement a bare-bones, block-by-block toggleable default wrap attribute for code blocks right now while this is being worked on by the team:
Modify the src/theme/CodeBlock/Content/String.js file so that the final code looks like this:
importReact,{useEffect,useMemo}from'react';importclsxfrom'clsx';import{useThemeConfig,usePrismTheme}from'@docusaurus/theme-common';import{parseCodeBlockTitle,parseLanguage,parseLines,containsLineNumbers,useCodeWordWrap,}from'@docusaurus/theme-common/internal';import{Highlight}from'prism-react-renderer';importLinefrom'@theme/CodeBlock/Line';importCopyButtonfrom'@theme/CodeBlock/CopyButton';importWordWrapButtonfrom'@theme/CodeBlock/WordWrapButton';importContainerfrom'@theme/CodeBlock/Container';importstylesfrom'./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/9012functionnormalizeLanguage(language){returnlanguage?.toLowerCase();}exportdefaultfunctionCodeBlockString({
children,className: blockClassName='',
metastring,title: titleProp,showLineNumbers: showLineNumbersProp,language: languageProp,}){const{prism: {defaultLanguage, magicComments},}=useThemeConfig();constlanguage=normalizeLanguage(languageProp??parseLanguage(blockClassName)??defaultLanguage,);constprismTheme=usePrismTheme();constwordWrap=useCodeWordWrap();// Check if wrap is specified in metastring or classNameconstshouldWrapByDefault=useMemo(()=>{// Check metastring for 'wrap'if(metastring?.includes('wrap')){returntrue;}// Check if language has 'wrap' suffix (e.g., bash-wrap, js-wrap)constlanguageMatch=blockClassName?.match(/language-(\w+)-?wrap/);if(languageMatch){returntrue;}returnfalse;},[metastring,blockClassName]);// Apply word wrap after initial render if neededuseEffect(()=>{if(shouldWrapByDefault&&!wordWrap.isEnabled){wordWrap.toggle();}},[shouldWrapByDefault,wordWrap]);// 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\""consttitle=parseCodeBlockTitle(metastring)||titleProp;const{lineClassNames, code}=parseLines(children,{
metastring,
language,
magicComments,});constshowLineNumbers=showLineNumbersProp??containsLineNumbers(metastring);return(<Containeras="div"className={clsx(blockClassName,language&&!blockClassName.includes(`language-${language}`)&&`language-${language}`,)}>{title&&<divclassName={styles.codeBlockTitle}>{title}</div>}<divclassName={styles.codeBlockContent}><Highlighttheme={prismTheme}code={code}language={language??'text'}>{({className, style, tokens, getLineProps, getTokenProps})=>(<pre/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */tabIndex={0}ref={wordWrap.codeBlockRef}className={clsx(className,styles.codeBlock,'thin-scrollbar')}style={style}><codeclassName={clsx(styles.codeBlockLines,showLineNumbers&&styles.codeBlockLinesWithNumbering,)}>{tokens.map((line,i)=>(<Linekey={i}line={line}getLineProps={getLineProps}getTokenProps={getTokenProps}classNames={lineClassNames[i]}showLineNumbers={showLineNumbers}/>))}</code></pre>)}</Highlight><divclassName={styles.buttonGroup}>{(wordWrap.isEnabled||wordWrap.isCodeScrollable)&&(<WordWrapButtonclassName={styles.codeButton}onClick={()=>wordWrap.toggle()}isEnabled={wordWrap.isEnabled}/>)}<CopyButtonclassName={styles.codeButton}code={code}/></div></div></Container>);}
Now you can use the wrap modifier in your markdown code blocks:
```bash wrap
This command will be word-wrapped by default
some_very_long_command --with-many-parameters --and-options
...
```
This implementation:
Doesn't modify any Docusaurus internals
Is (mostly) upgrade-friendly
Still allows users to toggle word wrap on/off with the button
Do note that this probably wouldn't be the way to go for the official implementation. Also note that this approach might have your content "jump" if you've got any codeblocks over the fold.
Activity
Josh-Cena commentedon Aug 1, 2022
We probably won't add a button (otherwise we need a "user control panel", which may not be a totally bad idea, like how MUI is doing it). But a global option seems worthwhile.
Why would you want to wrap the code? I've almost never wanted to wrap it.
JPeer264 commentedon Aug 1, 2022
Oh I am sorry. It seems this went sideways. What I meant is when the user toggles one it should toggle all others too (to be clear, I am also not a big fan of wrapping it - but some of our customers are now using the feature as it got shipped with docusaurus).
Basically only this line would be touched: https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-theme-common/src/hooks/useCodeWordWrap.ts#L61
E.g.:
Josh-Cena commentedon Aug 1, 2022
Oh, I see your point. Would you be happy with a theme option that syncs the wrap state between all code blocks?
JPeer264 commentedon Aug 1, 2022
Of course that would be ideal.
slorber commentedon Aug 9, 2022
Others looking for a similar feature:
@jaredcbaum in #5468 (comment)
@jgoux in #7769
Please help us decide what should be done so that all of you are satisfied ;)
JPeer264 commentedon Aug 25, 2022
I would propose a new theme configuration. I think with these everything should be covered:
jaredcbaum commentedon Aug 29, 2022
IMHO this would be an ideal config setup for code blocks. Since these options would be in
docusaurus.config.js, it can be assumed that these options are global. They could be overridden on a per instance basis by passing options to the CodeBlock component or a per page basis with a frontmatter value.JPeer264 commentedon Aug 29, 2022
I like that! The only thing I miss is the localStorage/global sync (which was the origin of this issue).
paulboocock commentedon Sep 6, 2022
I noticed in #7769 it was suggested that this could be enabled/forced for a single code block. I have the odd code block where it makes sense to have it forced on, or at least enabled by default.
So this syntax (or similar) would be great for me:
Example: https://docs.snowplow.io/docs/collecting-data/collecting-from-own-applications/javascript-trackers/javascript-tracker/javascript-tracker-v3/tracker-setup/loading/
I could also add some linebreaks 🤷
However, most snippets are far better with word wrap disabled, so I wouldn't really want to change the default for the whole site, or to even remember it page by page.
gtnardy commentedon Jun 14, 2023
4 remaining items
tarakta commentedon Sep 18, 2023
lathropcat commentedon Sep 18, 2023
RoksanaSkrzycka commentedon Sep 19, 2023
slorber commentedon Sep 28, 2023
We started working on this in #9349, and a few questions arise because there are orthogonal needs
As you can see, there are still design elements to discuss.
On the technical side, there are also important UX details to consider:
lvcarnevalli commentedon Jul 13, 2024
Hi everyone, do we have any updates on this 'wordWrap' feature?
benjie commentedon Dec 19, 2024
As a workaround, I wrap the code block in a
divwith a custom class:and added the
wrapcodeclass to mysrc/css/custom.css:Wrap code blocks
Wrap code blocks
ivancheban commentedon Dec 19, 2024
I see that now Docusaurus code blocks have this code wrapping feature out-of-the-box: there's a Toggle word wrap button in the code blocks with long lines of code. Please close this issue.

aMacDawg commentedon Jan 28, 2025
@ivancheban yes, the spirit of this ticket as I understand (and I share need the for fwiw) is to enable a way (most likely some kind of param passed when defining the markdown body itself) to have that
Toggle word wrapon by defaultThere is no current known way to do this without writing a custom css class like @benjie demo'd above, ergo ticket should stay OPEN imo
ZenaMel commentedon May 9, 2025
We would also benefit from at least a meta tag, e.g., "wordWrap=true" or IMO better: "wrap=true" or just "wrap", as the odd codeBlock (mostly one-liner scripts) will be more readable that way. I don't think we personally would need a global config option for this.
ZenaMel commentedon May 13, 2025
If you want to implement a bare-bones, block-by-block toggleable default wrap attribute for code blocks right now while this is being worked on by the team:
Syntax this will allow:
```js wrap
...
```
src/theme/CodeBlock/Content/String.jsfile so that the final code looks like this:```bash wrap
This command will be word-wrapped by default
some_very_long_command --with-many-parameters --and-options
...
```
This implementation:
Do note that this probably wouldn't be the way to go for the official implementation. Also note that this approach might have your content "jump" if you've got any codeblocks over the fold.
sakurawald commentedon Oct 15, 2025
It's a surprise that the
"wordWrap=true"option didn't exists.