Skip to content

Commit

Permalink
feat: variables and glossary (#865)
Browse files Browse the repository at this point in the history
| [![PR App][icn]][demo] | Fix RM-9018 RM-9022 |
| :--------------------: | :-----------------: |

## 🧰 Changes

Supports Variables and GlossaryItems!

Ok, so this PR only has a small 🤏 code change for those components. We just have to format the correct jsx in the editor for this to work, _I think_.

This PR does

- rename tests from `flavored-*` to `*`
- removes `yaml` from the top of the doc
- runs the variable browser test

## 🧬 QA & Testing

- [Broken on production][prod].
- [Working in this PR app][demo].

[demo]: https://markdown-pr-PR_NUMBER.herokuapp.com
[prod]: https://SUBDOMAIN.readme.io
[icn]: https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
  • Loading branch information
kellyjosephprice authored Apr 17, 2024
1 parent e0f53e4 commit aeebd7c
Show file tree
Hide file tree
Showing 33 changed files with 177 additions and 16 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions __tests__/browser/markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('visual regression tests', () => {
// 'tablesTests',
// 'codeBlockTests',
// 'tableOfContentsTests',
// 'varsTest',
'varsTest',
];

it.each(docs)(
Expand All @@ -37,7 +37,7 @@ describe('visual regression tests', () => {

expect(image).toMatchImageSnapshot();
},
10000
10000,
);

it.skip('renders html blocks, style tags, and style attributes with safeMode off', async () => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions __tests__/components/GlossaryTerm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import { run, compile } from '../../index';

describe('GlossaryItem', () => {
it('renders a glossary item', async () => {
const md = `<GlossaryItem term="parliament" />`;
const Content = await run(compile(md));
render(<Content />);

expect(screen.getByText('parliament')).toBeVisible();
});
});
14 changes: 14 additions & 0 deletions __tests__/components/Variable.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import { run, compile } from '../../index';

describe('Variable', () => {
it('render a variable', async () => {
const md = `<Variable variable="name" />`;
const Content = await run(compile(md));
render(<Content />);

expect(screen.getByText('NAME')).toBeVisible();
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 9 additions & 4 deletions docs/variable-tests.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
---
title: "Variable Tests"
title: 'Variable Tests'
category: 5fdf9fc9c2a7ef443e937315
hidden: true
---
<<defvar>> and `<<defvar>>` and:

<Variable variable="defvar" /> and `<Variable variable="defvar" />` and:

```
<<defvar>>
<Variable variable="defvar" />
```

and

```js
const xyz = "<<defvar>>";
const xyz = "<Variable variable="defvar" />";
```

## Glossary Items

<GlossaryItem term="demo" />
3 changes: 2 additions & 1 deletion example/Doc.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, FunctionComponent, useEffect, useState } from 'react';
import React, { FunctionComponent, useEffect, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import * as mdx from '../index';
import docs from './docs';
Expand Down Expand Up @@ -28,6 +28,7 @@ const Doc = () => {

setContent(() => content);
};

render();
}, [doc, lazyImages, safeMode]);

Expand Down
7 changes: 3 additions & 4 deletions example/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ const Form = () => {

const onSelect = (event: React.ChangeEvent<HTMLSelectElement>) => {
setSearchParams(params => {
params.delete('edit');
return params;
});

navigate(event.target.value);
navigate(`/${event.target.value}`);
};

const onChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
Expand All @@ -36,7 +35,7 @@ const Form = () => {
params.set('edit', value);
return params;
},
{ replace: true }
{ replace: true },
);

if (!edited) {
Expand All @@ -51,7 +50,7 @@ const Form = () => {
params.delete('edit');
return params;
},
{ replace: true }
{ replace: true },
);
}
}, [edited, searchParams]);
Expand Down
9 changes: 5 additions & 4 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debug from 'debug';
import { remark } from 'remark';
import remarkMdx from 'remark-mdx';
import remarkFrontmatter from 'remark-frontmatter';

import { createProcessor, compileSync, run as mdxRun, RunOptions } from '@mdx-js/mdx';
import * as runtime from 'react/jsx-runtime';
Expand Down Expand Up @@ -38,21 +39,21 @@ export const utils = {
};

const makeUseMDXComponents = (more: RunOpts['components']) => {
const components = { ...Components, ...more };
const components = { ...more, ...Components, Variable };

return () => components;
};

export const reactProcessor = (opts = {}) => {
return createProcessor({ remarkPlugins: [calloutTransformer], ...opts });
return createProcessor({ remarkPlugins: [remarkFrontmatter, calloutTransformer], ...opts });
};

export const compile = (text: string, opts = {}) => {
return String(
compileSync(text, {
outputFormat: 'function-body',
providerImportSource: '#',
remarkPlugins: [calloutTransformer],
remarkPlugins: [remarkFrontmatter, calloutTransformer],
...opts,
}),
).replace(/await import\(_resolveDynamicMdxSpecifier\('react'\)\)/, 'arguments[0].imports.React');
Expand Down Expand Up @@ -87,7 +88,7 @@ export const html = (text: string, opts = {}) => {
};

export const mdast: any = (text: string, opts = {}) => {
const processor = remark().use(remarkMdx);
const processor = remark().use(remarkMdx).use(remarkFrontmatter);

try {
const tree = processor.parse(text);
Expand Down
126 changes: 126 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"prop-types": "^15.8.1",
"rehype-sanitize": "^4.0.0",
"remark": "^15.0.1",
"remark-frontmatter": "^5.0.0",
"remark-mdx": "^3.0.0",
"remark-parse": "^11.0.0",
"trim": "^1.0.1",
Expand Down Expand Up @@ -71,8 +72,8 @@
"@readme/eslint-config": "^14.0.0",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@testing-library/react": "^14",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^14",
"@testing-library/user-event": "^14.5.2",
"@types/mdast": "^4.0.3",
"@types/mdx": "^2.0.12",
Expand Down

0 comments on commit aeebd7c

Please sign in to comment.