Skip to content

Commit

Permalink
add code title expression
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Mar 14, 2024
1 parent df80b9e commit b625c3c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
7 changes: 1 addition & 6 deletions docs/pages/experiments/docs/codeblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ npm install @mui/data-grid-premium

## With header path

<div class="codeblock-with-header">
<p class="header">PlainCssSliderDeep1.js</p>

```jsx
```jsx title="PlainCssSliderDeep1.js"
import * as React from 'react';
import Slider from '@mui/material/Slider';
import './PlainCssSliderDeep1.css';
Expand All @@ -62,5 +59,3 @@ export default function PlainCssSliderDeep1() {
);
}
```

</div>
51 changes: 29 additions & 22 deletions docs/src/modules/components/MarkdownElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@ const Root = styled('div')(
position: 'relative',
// Font size reset to fix a bug with Safari 16.0 when letterSpacing is set
fontSize: 10,
'&:has(.MuiCode-title)': {
border: `1px solid var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[700]})`,
borderRadius: theme.shape.borderRadius,
overflow: 'clip',
'& .MuiCode-copy': {
top: '56px',
},
},
},
'& .MuiCode-copy-container': {
// This container is only used in demo and highlight code
Expand Down Expand Up @@ -601,29 +609,28 @@ const Root = styled('div')(
marginBottom: theme.spacing(1),
},
},
'& .codeblock-with-header': {
border: `1px solid var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[700]})`,
borderRadius: theme.shape.borderRadius,
overflow: 'clip',
' pre': { margin: 0, border: 'none', borderRadius: 0 },
'& .header': {
'& .MuiCode-title': {
margin: 0,
padding: theme.spacing(1.5),
display: 'flex',
alignItems: 'center',
gap: theme.spacing(1),
borderBottom: `1px solid var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[700]})`,
borderColor: `var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[700]})`,
backgroundColor: `var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[900]})`,
fontFamily: theme.typography.fontFamilyCode,
fontSize: theme.typography.pxToRem(12),
fontWeight: lightTheme.typography.fontWeightBold,
color: `var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.grey[200]})`,
'::before': {
content: `url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M13.3333 3.99996H8L7.06 3.05996C6.80667 2.80663 6.46667 2.66663 6.11334 2.66663H2.66667C1.93334 2.66663 1.34 3.26663 1.34 3.99996L1.33334 12C1.33334 12.7333 1.93334 13.3333 2.66667 13.3333H13.3333C14.0667 13.3333 14.6667 12.7333 14.6667 12V5.33329C14.6667 4.59996 14.0667 3.99996 13.3333 3.99996ZM12.6667 12H3.33334C2.96667 12 2.66667 11.7 2.66667 11.3333V5.99996C2.66667 5.63329 2.96667 5.33329 3.33334 5.33329H12.6667C13.0333 5.33329 13.3333 5.63329 13.3333 5.99996V11.3333C13.3333 11.7 13.0333 12 12.6667 12Z' fill='%2399CCF3'/%3E%3C/svg%3E%0A");`,
width: '16px',
height: '16px',
},
'& + pre': {
margin: 0,
padding: theme.spacing(1.5),
display: 'flex',
alignItems: 'center',
gap: theme.spacing(1),
borderBottom: `1px solid var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[700]})`,
borderColor: `var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[700]})`,
backgroundColor: `var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.primaryDark[900]})`,
fontFamily: theme.typography.fontFamilyCode,
fontSize: theme.typography.pxToRem(12),
fontWeight: lightTheme.typography.fontWeightBold,
color: `var(--muidocs-palette-primaryDark-700, ${lightTheme.palette.grey[200]})`,
'::before': {
content: `url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M13.3333 3.99996H8L7.06 3.05996C6.80667 2.80663 6.46667 2.66663 6.11334 2.66663H2.66667C1.93334 2.66663 1.34 3.26663 1.34 3.99996L1.33334 12C1.33334 12.7333 1.93334 13.3333 2.66667 13.3333H13.3333C14.0667 13.3333 14.6667 12.7333 14.6667 12V5.33329C14.6667 4.59996 14.0667 3.99996 13.3333 3.99996ZM12.6667 12H3.33334C2.96667 12 2.66667 11.7 2.66667 11.3333V5.99996C2.66667 5.63329 2.96667 5.33329 3.33334 5.33329H12.6667C13.0333 5.33329 13.3333 5.63329 13.3333 5.99996V11.3333C13.3333 11.7 13.0333 12 12.6667 12Z' fill='%2399CCF3'/%3E%3C/svg%3E%0A");`,
width: '16px',
height: '16px',
},
border: 'none',
borderRadius: 0,
},
},
}),
Expand Down
3 changes: 2 additions & 1 deletion packages/markdown/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ function createRender(context) {
renderer.code = (code, infostring, escaped) => {
// https://github.com/markedjs/marked/blob/30e90e5175700890e6feb1836c57b9404c854466/src/Renderer.js#L15
const lang = (infostring || '').match(/\S*/)[0];
const title = (infostring || '').match(/title="([^"]*)"/)?.[1];
const out = prism(code, lang);
if (out != null && out !== code) {
escaped = true;
Expand All @@ -407,7 +408,7 @@ function createRender(context) {
return `<pre><code>${escaped ? code : escape(code, true)}</code></pre>\n`;
}

return `<div class="MuiCode-root"><pre><code class="language-${escape(lang, true)}">${
return `<div class="MuiCode-root">${title ? `<div class="MuiCode-title">${title}</div>` : ''}<pre><code class="language-${escape(lang, true)}">${
escaped ? code : escape(code, true)
}</code></pre>${[
'<button data-ga-event-category="code" data-ga-event-action="copy-click" aria-label="Copy the code" class="MuiCode-copy">',
Expand Down

0 comments on commit b625c3c

Please sign in to comment.