Skip to content

Commit

Permalink
strip wrapper divs, fix hideHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes committed Oct 17, 2019
1 parent 9b92a37 commit f2619bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
3 changes: 2 additions & 1 deletion docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ function Demo(props) {
const match = useMediaQuery(theme => theme.breakpoints.up('sm'));

const jsx = getJsxPreview(demoData.raw || '', demoOptions);
const showPreview = jsx !== demoData.raw && jsx.split(/\n/).length <= 20;
const showPreview =
!demoOptions.hideHeader && jsx !== demoData.raw && jsx.split(/\n/).length <= 20;

let showCodeLabel;
if (codeOpen) {
Expand Down
49 changes: 26 additions & 23 deletions docs/src/modules/utils/getJsxPreview.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
// docs/pages/components/material-icons.js doesn't provide the source
export default function getJsxPreview(code, demoOptions) {
/* regex matches the content of the return statement in the default export:
*
* `export default.*`
* `\n return (\n` or `\n return `
*
* everything (not greedy), until:
*
* ` );\n}` or `;\n}`
*/
let jsx = code.match(
/export default .*(\n {2}return \(\n|\n {2}return )(.*?)(\n {2}\);\n}|;\n})/s,
);
/* Just the match, otherwise the full source if no match or disabled,
/* The regex matches the content of the return statement in the default export,
* stripping any wrapper divs:
*
* `export default.*`
* `\n return (\n` or `\n return `
* ` <div.*>\n` (optional)
* ` <div.*>\n` (optional)
* everything until:
* `\n </div>` (optional)
* `\n </div>` (optional)
* ` );\n}` or `;\n}`
*/
let jsx = code.match(
/export default .*(?:\n {2}return \(\n|\n {2}return )(?: {4}<div.*?>\n)?(?: {6}<div.*?>\n)?(.*?)(\n {6}<\/div>)?(\n {4}<\/div>)?(\n {2}\);\n}|;\n})/s,
);
/* Just the match, otherwise the full source if no match or disabled,
so as not to break the Collapse transition. */
jsx = jsx && demoOptions.defaultCodeOpen !== false ? jsx[2] : code;
jsx = jsx && demoOptions.defaultCodeOpen !== false ? jsx[1] : code;

// Remove leading spaces from each line
return jsx.split(/\n/).reduce(
(acc, line) =>
`${acc}${line.slice(
// Number of leading spaces on the first line
jsx.match(/^ */)[0].length,
)}\n`,
'',
);
// Remove leading spaces from each line
return jsx.split(/\n/).reduce(
(acc, line) =>
`${acc}${line.slice(
// Number of leading spaces on the first line
jsx.match(/^ */)[0].length,
)}\n`,
'',
);
}

0 comments on commit f2619bc

Please sign in to comment.