Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Extends [`RunOptions`](https://mdxjs.com/packages/mdx/#runoptions)
- `components` (`Record<string, MDXModule>`, optional) -- An object of tag names to executed components.
- `imports` (`Record<string, unknown>`, optional) -- An object of modules to import.
- `terms` (`GlossaryTerm[]`, optional)
- `variables` (`Variables`, optional)
- `variables` (`Variables`, optional) -- An object containing [user variables}(https://github.com/readmeio/variable).

### `RMDXModule`

Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

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

i hate this snap

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.
15 changes: 0 additions & 15 deletions __tests__/compilers/compatability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@ import { mdx } from '../../index';
import * as rdmd from '@readme/markdown-legacy';

describe('compatability with RDMD', () => {
it('compiles variable nodes', () => {
const ast = {
type: 'readme-variable',
text: 'parliament',
data: {
hName: 'readme-variable',
hProperties: {
variable: 'parliament',
},
},
};

expect(mdx(ast).trim()).toBe('<Variable name="parliament" />');
});

it('compiles glossary nodes', () => {
const ast = {
type: 'readme-glossary-item',
Expand Down
16 changes: 16 additions & 0 deletions __tests__/compilers/variable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as rmdx from '../../index';

describe('variable compiler', () => {
it('compiles back to the ', () => {
const mdx = `
## Hello!

{user.name}

### Bye bye!
`;
const tree = rmdx.mdast(mdx);

expect(rmdx.mdx(tree).trim()).toStrictEqual(mdx.trim());
});
});
55 changes: 55 additions & 0 deletions __tests__/transformers/variables.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import * as rmdx from '../../index';
import { execute } from '../helpers';
import { render, screen } from '@testing-library/react';

describe('variables transformer', () => {
it('renders user variables', async () => {
const mdx = '{user.name}';
const variables = {
user: {
name: 'Test User',
},
};
const Content = await execute(mdx, { variables });

render(<Content />);

expect(screen.findByText('Test User')).toBeDefined();
});

it('renders user variables in a phrasing context', async () => {
const mdx = 'Hello, {user.name}!';
const variables = {
user: {
name: 'Test User',
},
};
const Content = await execute(mdx, { variables });

render(<Content />);

expect(screen.findByText('Test User')).toBeDefined();
});

it('parses variables into the mdast', () => {
const mdx = `{user.name}`;

// @ts-ignore
expect(rmdx.mdast(mdx)).toStrictEqualExceptPosition({
children: [
{
children: [],
data: {
hName: 'Variable',
hProperties: {
name: 'name',
},
},
type: 'readme-variable',
},
],
type: 'root',
});
});
});
200 changes: 0 additions & 200 deletions __tests__/variable-parser.test.js

This file was deleted.

12 changes: 5 additions & 7 deletions docs/variable-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ category: 5fdf9fc9c2a7ef443e937315
hidden: true
---

<Variable variable="defvar" /> and `<Variable variable="defvar" />` and:
This is the variable `defvar`: {user.defvar}

```
<Variable variable="defvar" />
```
Ok, but this one is defined: {user.email}

and
It does not render in code blocks:

```js
const xyz = "<Variable variable="defvar" />";
```
{user.defvar}
```
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these vars need to be usable within code blocks; I've seen customers use this functionality to construct customized copyable snippets! (It used to be that you'd escape them– something like \<<defvar\>> –if you wanted to show the string literally.)


## Glossary Items
Expand Down
20 changes: 13 additions & 7 deletions example/Doc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ Object.entries(components).forEach(async ([tag, body]) => {
executedComponents[tag] = await mdx.run(mdx.compile(body));
});

const variables = {
user: {
email: '[email protected]',
},
defaults: [],
};

const terms = [
{
term: 'demo',
Expand All @@ -40,6 +33,19 @@ const terms = [
},
];

const variables = {
user: {
email: '[email protected]',
name: 'kelly joseph price',
},
defaults: [
{
name: 'defvar',
default: '(default value for defvar)',
},
],
};

const Doc = () => {
const { fixture } = useParams();
const [searchParams] = useSearchParams();
Expand Down
3 changes: 0 additions & 3 deletions processor/compile/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { toXml } from 'xast-util-to-xml';
import { NodeTypes } from '../../enums';

type CompatNodes =
| { type: NodeTypes.variable; text: string }
| { type: NodeTypes.glossary; data: { hProperties: { term: string } } }
| { type: NodeTypes.glossary; data: { hName: 'Glossary' }; children: [{ type: 'text'; value: string }] }
| { type: NodeTypes.reusableContent; tag: string }
Expand All @@ -29,8 +28,6 @@ const html = (node: Html) => {

const compatibility = (node: CompatNodes) => {
switch (node.type) {
case NodeTypes.variable:
return `<Variable name="${node.text}" />`;
case NodeTypes.glossary:
// @ts-expect-error
const term = node.data?.hProperties?.term || node.children[0].value;
Expand Down
3 changes: 2 additions & 1 deletion processor/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import gemoji from './gemoji';
import htmlBlock from './html-block';
import image from './image';
import compatibility from './compatibility';
import variable from './variable';
import { NodeTypes } from '../../enums';

function compilers() {
Expand All @@ -19,7 +20,7 @@ function compilers() {
[NodeTypes.embed]: embed,
[NodeTypes.htmlBlock]: htmlBlock,
[NodeTypes.image]: image,
[NodeTypes.variable]: compatibility,
[NodeTypes.variable]: variable,
[NodeTypes.glossary]: compatibility,
[NodeTypes.reusableContent]: compatibility,
html: compatibility,
Expand Down
Loading