-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
'', | ||
); | ||
} |