-
Notifications
You must be signed in to change notification settings - Fork 43
[WEB-3888] Introduce support for MDX templating #2527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,929
−2,111
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 @@ | ||
# Ignore all mdx files in src/pages - we need to be able to render components on one line for inlining | ||
src/pages/**/*.mdx | ||
src/pages/**/*.md |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,154 @@ | ||
import React, { ReactNode } from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import If from './mdx/If'; | ||
import CodeSnippet from '@ably/ui/core/CodeSnippet'; | ||
|
||
// Mock the dependencies we need for testing | ||
jest.mock('./MDXWrapper', () => { | ||
return { | ||
__esModule: true, | ||
default: ({ children, pageContext }: { children: ReactNode; pageContext: any }) => ( | ||
<div data-testid="mdx-wrapper"> | ||
{pageContext?.frontmatter?.title && <h1>{pageContext.frontmatter.title}</h1>} | ||
<div data-testid="mdx-content">{children}</div> | ||
</div> | ||
), | ||
}; | ||
}); | ||
|
||
// Mock the layout context | ||
jest.mock('src/contexts/layout-context', () => ({ | ||
useLayoutContext: () => ({ | ||
activePage: { language: 'javascript' }, | ||
setLanguage: jest.fn(), | ||
}), | ||
LayoutProvider: ({ children }: { children: ReactNode }) => <div data-testid="layout-provider">{children}</div>, | ||
})); | ||
|
||
// We need to mock minimal implementation of other dependencies that CodeSnippet might use | ||
jest.mock('@ably/ui/core/Icon', () => { | ||
return { | ||
__esModule: true, | ||
default: ({ name, size, additionalCSS, color }: any) => ( | ||
<span | ||
data-testid={`icon-${name}`} | ||
className={`${additionalCSS || ''} ${color || ''}`} | ||
style={{ width: size, height: size }} | ||
> | ||
{name} | ||
</span> | ||
), | ||
}; | ||
}); | ||
|
||
// Mock Code component used by CodeSnippet | ||
jest.mock('@ably/ui/core/Code', () => { | ||
return { | ||
__esModule: true, | ||
default: ({ language, snippet }: any) => ( | ||
<pre data-testid={`code-${language}`}> | ||
<code>{snippet}</code> | ||
</pre> | ||
), | ||
}; | ||
}); | ||
|
||
describe('MDX component integration', () => { | ||
it('renders basic content correctly', () => { | ||
render( | ||
<div> | ||
<h1>Test Heading</h1> | ||
<p>Test paragraph</p> | ||
</div>, | ||
); | ||
|
||
expect(screen.getByText('Test Heading')).toBeInTheDocument(); | ||
expect(screen.getByText('Test paragraph')).toBeInTheDocument(); | ||
}); | ||
|
||
it('conditionally renders content with If component', () => { | ||
render( | ||
<div> | ||
<If lang="javascript">This should be visible</If> | ||
<If lang="ruby">This should not be visible</If> | ||
</div>, | ||
); | ||
|
||
expect(screen.getByText('This should be visible')).toBeInTheDocument(); | ||
expect(screen.queryByText('This should not be visible')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders code snippets with different languages (JavaScript active)', () => { | ||
render( | ||
<div> | ||
<CodeSnippet> | ||
<pre> | ||
<code className="language-javascript"> | ||
{`var ably = new Ably.Realtime('API_KEY'); | ||
var channel = ably.channels.get('channel-name'); | ||
|
||
// Subscribe to messages on channel | ||
channel.subscribe('event', function(message) { | ||
console.log(message.data); | ||
});`} | ||
</code> | ||
</pre> | ||
<pre> | ||
<code className="language-swift"> | ||
{`let realtime = ARTRealtime(key: "API_KEY") | ||
let channel = realtime.channels.get("channel-name") | ||
|
||
// Subscribe to messages on channel | ||
channel.subscribe("event") { message in | ||
print(message.data) | ||
}`} | ||
</code> | ||
</pre> | ||
</CodeSnippet> | ||
</div>, | ||
); | ||
|
||
const javascriptElement = screen.queryByTestId('code-javascript'); | ||
const swiftElement = screen.queryByTestId('code-swift'); | ||
|
||
expect(javascriptElement).toBeInTheDocument(); | ||
expect(swiftElement).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders code snippets (TypeScript active)', () => { | ||
render( | ||
<div> | ||
<CodeSnippet lang="typescript"> | ||
<pre> | ||
<code className="language-javascript"> | ||
{`var ably = new Ably.Realtime('API_KEY'); | ||
var channel = ably.channels.get('channel-name'); | ||
|
||
// Subscribe to messages on channel | ||
channel.subscribe('event', function(message) { | ||
console.log(message.data); | ||
});`} | ||
</code> | ||
</pre> | ||
<pre> | ||
<code className="language-typescript"> | ||
{`const ably = new Ably.Realtime('API_KEY'); | ||
const channel = ably.channels.get('channel-name'); | ||
|
||
// Subscribe to messages on channel | ||
channel.subscribe('event', (message: Ably.Types.Message) => { | ||
console.log(message.data); | ||
});`} | ||
</code> | ||
</pre> | ||
</CodeSnippet> | ||
</div>, | ||
); | ||
|
||
const javascriptElement = screen.queryByTestId('code-javascript'); | ||
const typescriptElement = screen.queryByTestId('code-typescript'); | ||
|
||
expect(javascriptElement).not.toBeInTheDocument(); | ||
expect(typescriptElement).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A first go at something a little less "runtime". Since the syntax for defining codeblocks is set by Markdown which is quite rigid itself - we can dig through MDX files for present languages and build a representative superset that way. Textile files are still determined at runtime as they are still tied into the half-Textile-half-React world.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice! It would have been super convenient if the raw content of the file was available in
onCreatePage
, then you could forego all the duplicate IO (just a wishlist item)