Skip to content

Commit

Permalink
Merge pull request #1068 from daniel-ac-martin/add-mdx-test
Browse files Browse the repository at this point in the history
First class support for Markdown and MDX
  • Loading branch information
daniel-ac-martin authored Sep 4, 2024
2 parents 2f5465b + 5e5a2e7 commit 420e1c9
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/govuk-docs/src/common/page-loader.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const pageLoader = require.context('./pages', true, /\.([jt]sx?|html)$/i, 'sync');
export const pageLoader = require.context('./pages', true, /\.([jt]sx?|mdx?|html)$/i, 'sync');
export default pageLoader;
25 changes: 25 additions & 0 deletions apps/govuk-template/feat/md.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const pageWorks = () => (
it('is the correct page', () => {
cy.contains('Markdown test').should('be.visible');
cy.contains('Hello world!').should('be.visible');
})
);

describe('Markdown', () => {
describe('when visiting the page directly', () => {
it('successfully loads', () => {
cy.visit('/md');
});

pageWorks();
});

describe('when visiting the page indirectly', () => {
before(() => {
cy.visit('/');
cy.contains('Md').click();
});

pageWorks();
});
});
26 changes: 26 additions & 0 deletions apps/govuk-template/feat/mdx.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const pageWorks = () => (
it('is the correct page', () => {
cy.contains('MDX test').should('be.visible');
cy.contains('Hello world!').should('be.visible');
cy.contains('React supported').should('be.visible');
})
);

describe('Markdown', () => {
describe('when visiting the page directly', () => {
it('successfully loads', () => {
cy.visit('/mdx');
});

pageWorks();
});

describe('when visiting the page indirectly', () => {
before(() => {
cy.visit('/');
cy.contains('MDX').click();
});

pageWorks();
});
});
2 changes: 1 addition & 1 deletion apps/govuk-template/src/common/page-loader.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const pageLoader = require.context('./pages', true, /\.([jt]sx?|html)$/i, 'sync');
export const pageLoader = require.context('./pages', true, /\.([jt]sx?|mdx?|html)$/i, 'sync');
export default pageLoader;
37 changes: 37 additions & 0 deletions apps/govuk-template/src/common/pages/md.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Markdown test
=============

Hello world!

[Markdown](/md) has support for basic typography such as **bold** and _italic_. You can even use them **_together_**.

Ordered lists
-------------

### Reasons to be cheerful:

1. One
2. Two
3. Three

## Disordered lists

- A
- B
- C

## Block quotes

> **My quote**
>
> > Embedded
## Code

We can provide `code inline`, or in a block:

```
MY CODE
MORE CODE
```
80 changes: 80 additions & 0 deletions apps/govuk-template/src/common/pages/mdx.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { BackLink, NavigationMenu, Panel } from '@not-govuk/components';

export const title = 'MDX';

<div className="govuk-grid-row">
<div className="govuk-grid-column-one-quarter">
<NavigationMenu
items={[
{
href: "/",
text: "Home",
},
{
href: "/html",
text: "HTML",
},
{
href: "/md",
text: "Markdown",
},
{
href: "/mdx",
text: "MDX",
},
]}
/>
</div>
<div className="govuk-grid-column-three-quarters">
<BackLink />

MDX test
========

Hello world!

[Markdown](/md) has support for basic typography such as **bold** and _italic_. You can even use them **_together_**.

Ordered lists
-------------

### Reasons to be cheerful:

1. One
2. Two
3. Three

## Disordered lists

- A
- B
- C

## Block quotes

> **My quote**
>
> > Embedded
## Code

We can provide `code inline`, or in a block:

```
MY CODE
MORE CODE
```

---

## MDX

<Panel
classModifiers="confirmation"
title="React supported"
>
You can use React components in your MDX pages.
</Panel>
</div>
</div>
11 changes: 4 additions & 7 deletions lib/engine/src/lib/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PageModule, PageInfoSSR, PageLoader } from '@not-govuk/app-composer';
import { Response } from '@not-govuk/server-renderer';
import path from 'path';

const pageExtensionPattern = /\.([jt]sx?|html)$/i
const pageExtensionPattern = /\.([jt]sx?|mdx?|html)$/i

const removePrecedingDotSlash = (s: string): string => (
s.startsWith('./')
Expand Down Expand Up @@ -58,21 +58,18 @@ export const gatherPages = (pageLoader: PageLoader): Promise<PageInfoSSR[]> => P
.keys()
.map(async e => {
const mod: PageModule = await pageLoader(e);
const title: string = mod.title || src2Title(e);

return {
Component: mod.default,
href: src2Href(e),
src: e,
title: (
typeof mod.default === 'string'
? src2Title(e)
: mod.title
)
title
};
} )
);

const pageMiddleware = (title: string) => (req: Request, res: Response, next: Next) => {
const pageMiddleware = (title: string) => (_req: Request, res: Response, next: Next) => {
res.renderApp(200, title).finally(next);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/plop-pack/skel/app/src/common/page-loader.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const pageLoader = require.context('./pages', true, /\.([jt]sx?|html)$/i, 'sync');
export const pageLoader = require.context('./pages', true, /\.([jt]sx?|mdx?|html)$/i, 'sync');
export default pageLoader;

1 comment on commit 420e1c9

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://not-gov.uk as production
🚀 Deployed on https://66d88f17e19000fa1660ee74--notgovuk.netlify.app

Please sign in to comment.