Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Jan 8, 2025
2 parents 4616fb3 + a5a8061 commit 6d10c97
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ jobs:
# Sync docs to rdmd.readme.io
#
- name: Sync docs to rdmd.readme.io
uses: readmeio/rdme@v8
uses: readmeio/rdme@v9
with:
rdme: docs ./docs --key=${{ secrets.RDME_KEY }} --version=2
33 changes: 33 additions & 0 deletions __tests__/lib/plain/custom-components.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { hast, plain } from '../../../index';

describe('plain compiler', () => {
it('should include the title of Accordion', () => {
const mdx = `
<Accordion title="Title">
Body
</Accordion>
`;

expect(plain(hast(mdx))).toContain('Title Body');
});

it('should include the title of Card', () => {
const mdx = `
<Card title="Title">
Body
</Card>
`;

expect(plain(hast(mdx))).toContain('Title Body');
});

it('should include the title of Tab', () => {
const mdx = `
<Tab title="Title">
Body
</Tab>
`;

expect(plain(hast(mdx))).toContain('Title Body');
});
});
17 changes: 17 additions & 0 deletions __tests__/migration/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,21 @@ ${JSON.stringify(
"
`);
});

it('compiles tables with leading escapes', () => {
const md = `
| Col 1 | Col 2 |
| --------- | ----- |
| \\_foo_\\ | bar |
`;

const mdx = migrate(md);

expect(mdx).toMatchInlineSnapshot(`
"| Col 1 | Col 2 |
| --------- | ----- |
| \\_foo\\_\\ | bar |
"
`);
});
});
8 changes: 6 additions & 2 deletions lib/hast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import astProcessor, { rehypePlugins, MdastOpts } from './ast-processor';
import remarkRehype from 'remark-rehype';
import { injectComponents } from '../processor/transform';
import { injectComponents, mdxToHast } from '../processor/transform';
import { MdastComponents } from '../types';
import mdast from './mdast';

Expand All @@ -10,7 +10,11 @@ const hast = (text: string, opts: MdastOpts = {}) => {
return memo;
}, {});

const processor = astProcessor(opts).use(injectComponents({ components })).use(remarkRehype).use(rehypePlugins);
const processor = astProcessor(opts)
.use(injectComponents({ components }))
.use(mdxToHast)
.use(remarkRehype)
.use(rehypePlugins);

return processor.runSync(processor.parse(text));
};
Expand Down
59 changes: 33 additions & 26 deletions lib/plain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,39 @@ function one(node: Nodes, opts: Options) {
if ('tagName' in node) {
if (STRIP_TAGS.includes(node.tagName)) return '';

if (node.tagName === 'html-block') {
if (!node.properties.html) return '';
return all(hast(node.properties.html.toString()), opts);
}

if (node.tagName === 'rdme-callout') {
const { icon, title } = node.properties;

const children = node?.children?.slice(title ? 1 : 0);
const body = children ? all({ type: 'root', children }, opts) : '';

return [icon, ' ', title, title && body && ': ', body].filter(Boolean).join('');
}

if (node.tagName === 'readme-glossary-item') {
return node.properties.term;
}

if (node.tagName === 'readme-variable') {
const key = node.properties.variable.toString();
const val = opts.variables[key];
return val || `<<${key}>>`;
}

if (node.tagName === 'img') {
return node.properties?.title || '';
switch (node.tagName) {
case 'html-block': {
if (!node.properties.html) return '';
return all(hast(node.properties.html.toString()), opts);
}
case 'rdme-callout': {
const { icon, title } = node.properties;

const children = node?.children?.slice(title ? 1 : 0);
const body = children ? all({ type: 'root', children }, opts) : '';

return [icon, ' ', title, title && body && ': ', body].filter(Boolean).join('');
}
case 'readme-glossary-item': {
return node.properties.term;
}
case 'readme-variable': {
const key = node.properties.variable.toString();
const val = opts.variables[key];
return val || `<<${key}>>`;
}
case 'img': {
return node.properties?.title || '';
}
case 'Accordion':
case 'Card':
case 'Tab': {
const title = node.properties?.title || '';
const children = node?.children;
const body = children ? all({ type: 'root', children }, opts) : '';

return [title, body].filter(Boolean).join(' ');
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

10 changes: 2 additions & 8 deletions processor/migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import imagesTransformer from './images';
import linkReferenceTransformer from './linkReference';
import tableCellTransformer from './table-cell';

const transformers = [
emphasisTransformer,
imagesTransformer,
linkReferenceTransformer,
tableCellTransformer,
];

export default transformers
const transformers = [emphasisTransformer, imagesTransformer, linkReferenceTransformer, tableCellTransformer];

export default transformers;
14 changes: 8 additions & 6 deletions processor/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import embedTransformer from './embeds';
import imageTransformer from './images';
import gemojiTransformer from './gemoji+';

import compatabilityTransfomer from './compatability';
import divTransformer from './div';
import injectComponents from './inject-components';
import mdxToHast from './mdx-to-hast';
import mermaidTransformer from './mermaid';
import readmeComponentsTransformer from './readme-components';
import readmeToMdx from './readme-to-mdx';
import variablesTransformer from './variables';
import tablesToJsx from './tables-to-jsx';
import compatabilityTransfomer from './compatability';
import mermaidTransformer from './mermaid';
import variablesTransformer from './variables';

export {
compatabilityTransfomer,
divTransformer,
injectComponents,
mdxToHast,
mermaidTransformer,
readmeComponentsTransformer,
readmeToMdx,
injectComponents,
variablesTransformer,
tablesToJsx,
mermaidTransformer,
variablesTransformer,
};

export const defaultTransforms = {
Expand Down
27 changes: 27 additions & 0 deletions processor/transform/mdx-to-hast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { visit } from 'unist-util-visit';
import { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx';
import { Transform } from 'mdast-util-from-markdown';
import { Parents } from 'mdast';
import { getAttrs, isMDXElement } from '../utils';
import * as Components from '../../components';

const setData = (node: MdxJsxFlowElement | MdxJsxTextElement, index: number, parent: Parents) => {
if (!node.name) return;
if (!(node.name in Components)) return;

parent.children[index] = {
...node,
data: {
hName: node.name,
hProperties: getAttrs(node),
},
};
};

const mdxToHast = (): Transform => tree => {
visit(tree, isMDXElement, setData);

return tree;
};

export default mdxToHast;
2 changes: 1 addition & 1 deletion processor/transform/tables-to-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const visitor = (table: Table, index: number, parent: Parents) => {
parent.children.splice(index, 1, { type: 'text', value: '\n' });
});

if (!phrasing(content)) {
if (!phrasing(content) && content.type !== 'escape') {
hasFlowContent = true;
return EXIT;
}
Expand Down

0 comments on commit 6d10c97

Please sign in to comment.