-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Show the JSX by default for small examples (#17831)
- Loading branch information
1 parent
5c70cfc
commit f8080a6
Showing
45 changed files
with
497 additions
and
349 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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export default function getJsxPreview(code, defaultCodeOpen) { | ||
/* 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) | ||
* everything until: | ||
* `\n </div>` (optional) | ||
* ` );\n}` or `;\n}` | ||
*/ | ||
let jsx = code.match( | ||
/export default .*(?:\n {2}return \(\n|\n {2}return )(?: {4}<div.*?>\n)?(.*?)(\n {4}<\/div>)?(\n {2}\);\n}|;\n})/s, | ||
); | ||
// Just the match, otherwise the full source if either no match or preview disabled, | ||
// so as not to break the Collapse transition. | ||
jsx = jsx && 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`, | ||
'', | ||
); | ||
} |
Oops, something went wrong.