Skip to content

Commit

Permalink
Upgrade react-live (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding authored Sep 11, 2024
1 parent c8fa528 commit 7379927
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 219 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ LICENSE
**/public
.dockerignore
Dockerfile
Dockerfile.*
Dockerfile.*
.vercelignore
4 changes: 3 additions & 1 deletion docs/author/ui-components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ sidebar:

# {meta.title}

Todo...
```jsx live
<button>Hello, world!</button>
```
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"hoist-non-react-statics": "^3.3.2",
"lodash-es": "^4.17.21",
"prism-react-renderer": "^1.1.1",
"react-live": "^2.2.3",
"react-live": "^4.0.0",
"react-markdown": "^9.0.0",
"react-responsive-carousel": "3.2.10",
"react-table": "^7.8.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/Markdown/Pre/index.css.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { style } from '@vanilla-extract/css';
import { style, globalStyle } from '@vanilla-extract/css';
import { backgroundColor, foregroundColor, responsiveSprinkles } from '@jpmorganchase/mosaic-theme';

const root = style({
position: 'relative'
});

globalStyle(`${root} pre[class*=language-]`, {
textShadow: 'none'
});

export default {
button: style({
top: '0px',
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/__tests__/ReactLive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ReactLive } from '../ReactLive';
describe('GIVEN a ReactLive view', () =>
it('THEN it should render code in a live view', async () => {
// arrange
const { getAllByRole } = render(<ReactLive language="jsx">{'<h1>Hello World</h1>'}</ReactLive>);
const { container } = render(<ReactLive language="jsx">{'<h1>Hello World</h1>'}</ReactLive>);

// assert
await waitFor(() => {
Expand All @@ -16,7 +16,7 @@ describe('GIVEN a ReactLive view', () =>
// act
const showLiveCode = screen.getByText('Show Live Code');
fireEvent.click(showLiveCode);

// assert
expect(getAllByRole('textbox')[0]).toHaveAttribute('class', 'liveEditor');
expect(container.querySelector('pre')).toHaveAttribute('class', 'prism-code language-tsx');
expect(container.querySelector('pre')).toHaveAttribute('spellcheck', 'false');
}));
1 change: 0 additions & 1 deletion packages/open-api-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"clsx": "^2.0.0",
"deepmerge": "^2.0.1",
"lodash-es": "^4.17.21",
"react-live": "^2.2.3",
"react-markdown": "^9.0.0",
"use-memo-one": "^1.1.1",
"warning": "^3.0.0"
Expand Down
19 changes: 19 additions & 0 deletions packages/site-middleware/src/__tests__/compileMdx.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { compileMDX } from '../compileMdx.js';

describe('compileMdx', () => {
it('should handle codeblocks', async () => {
const result = await compileMDX('```jsx live\n<div>I will be live editable!</div>\n```');
expect(result.compiledSource).toContain(`{
live: true,
children: _jsxDEV(_components.code, {
className: "language-jsx",
children: "<div>I will be live editable!</div>\\n"
}, undefined, false, {
fileName: "<source.js>",
lineNumber: 1,
columnNumber: 1
}, this)
}`);
});
});
4 changes: 2 additions & 2 deletions packages/site-middleware/src/compileMdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export async function compileMDX(
) {
const mdxSource = await serialize(content, {
mdxOptions: {
rehypePlugins: [rehypeSlug, ...remarkPlugins],
remarkPlugins: [codeBlocks, remarkGfm, ...rehypePlugins]
rehypePlugins: [codeBlocks, rehypeSlug, ...remarkPlugins],
remarkPlugins: [remarkGfm, ...rehypePlugins]
},
parseFrontmatter
});
Expand Down
6 changes: 3 additions & 3 deletions packages/site-middleware/src/plugins/codeBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type MdxJsxFlowElementHast } from 'mdast-util-mdx-jsx';
import { propertiesToMdxJsxAttributes } from 'hast-util-properties-to-mdx-jsx-attributes';

/**
* Modified from: https://github.com/remcohaszing/remark-mdx-code-meta
* Modified from: https://github.com/remcohaszing/rehype-mdx-code-props/blob/main/src/rehype-mdx-code-props.ts
*
* Custom meta parser for codefences that have extra params. e.g.
* ```jsx filename="hello.jsx"
Expand All @@ -24,7 +24,7 @@ import { propertiesToMdxJsxAttributes } from 'hast-util-properties-to-mdx-jsx-at
*/
export const transformer: Transformer<Root> = ast => {
visitParents(ast, 'element', (node, ancestors) => {
if (node.tagName !== 'code' && node.tagName !== 'pre') {
if (node.tagName !== 'code') {
return;
}

Expand Down Expand Up @@ -53,7 +53,7 @@ export const transformer: Transformer<Root> = ast => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parent = ancestors.at(-2)!;

// // Limit eval to just basic strings that start with "meta."
// Limit eval to just basic strings that start with "meta."
// const isEval =
// /(^| )eval(="true"| |$)/.test(meta) && /^meta\.[a-z0-9_[\].$"']+$/i.test(child.value);
//
Expand Down
10 changes: 5 additions & 5 deletions packages/site/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const webpack = require('webpack');
module.exports = {
reactStrictMode: true,
swcMinify: true,
experimental: {
outputFileTracingExcludes: {
'*': ['**/.next/cache/webpack']
}
},
transpilePackages: [
'@jpmorganchase/mosaic-components',
'@jpmorganchase/mosaic-content-editor-plugin',
Expand Down Expand Up @@ -32,11 +37,6 @@ module.exports = {
]
},
webpack(config) {
// Swaps out Buble for a smaller version that removes the latest Regex spec features.
// See https://github.com/FormidableLabs/react-live#what-bundle-size-can-i-expect
config.plugins.push(
new webpack.NormalModuleReplacementPlugin(/^buble$/, require.resolve('@philpl/buble'))
);
// Required by MDX-JS
if (config.resolve.fallback) {
config.resolve.fallback.fs = false;
Expand Down
1 change: 0 additions & 1 deletion packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@jpmorganchase/mosaic-standard-generator": "^0.1.0-beta.85",
"@jpmorganchase/mosaic-store": "^0.1.0-beta.85",
"@jpmorganchase/mosaic-theme": "^0.1.0-beta.85",
"@philpl/buble": "^0.19.7",
"@salt-ds/core": "^1.33.0",
"@salt-ds/lab": "1.0.0-alpha.50",
"@types/react": "^18.0.26",
Expand Down
2 changes: 1 addition & 1 deletion packages/site/public/search-data.json

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions packages/standard-generator/src/templates/next.config.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ module.exports = {
]
},
webpack(config) {
// Swaps out Buble for a smaller version that removes the latest Regex spec features.
// See https://github.com/FormidableLabs/react-live#what-bundle-size-can-i-expect
config.plugins.push(
new webpack.NormalModuleReplacementPlugin(/^buble$/, require.resolve('@philpl/buble'))
);
// Required by MDX-JS
if (config.resolve.fallback) {
config.resolve.fallback.fs = false;
Expand Down
1 change: 0 additions & 1 deletion packages/standard-generator/src/templates/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
},
"dependencies": {
{{{ printDependencies dependencies }}}
"@philpl/buble": "^0.19.7",
"@types/react": "^18.0.26",
"next": "^13.4.1",
"next-auth": "^4.24.5"
Expand Down
Loading

0 comments on commit 7379927

Please sign in to comment.