Skip to content
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

feat: variables and glossary #865

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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.
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" />";
Comment on lines +2 to +16
Copy link
Member

Choose a reason for hiding this comment

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

should we also add an example for <GlossaryItem /> to this?

```

## 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
Loading