Skip to content

Commit

Permalink
Merge pull request #20349 from storybookjs/norbert/mdx2-comments-migr…
Browse files Browse the repository at this point in the history
…ation

add auto-migration for MDX html-type comments to JS-type comments
  • Loading branch information
ndelangen authored Jan 12, 2023
2 parents e465ca4 + 97e8194 commit 6a085f8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 21 deletions.
94 changes: 82 additions & 12 deletions code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/// <reference types="@types/jest" />;

import { dedent } from 'ts-dedent';
import { fixMdxScript } from './mdx-1-to-2';
import { fixMdxStyleTags, fixMdxComments } from './mdx-1-to-2';

describe('fix', () => {
it('fixes badly-formatted style blocks', () => {
expect(
fixMdxScript(dedent`
it('fixMdxStyleTags fixes badly-formatted style blocks', () => {
expect(
fixMdxStyleTags(dedent`
<style>{\`
.foo {}
.bar {}
\`}</style>
`)
).toEqual(dedent`
).toEqual(dedent`
<style>
{\`
.foo {}
Expand All @@ -22,19 +21,19 @@ describe('fix', () => {
\`}
</style>
`);
});
});

it('fixes multiple style blocks', () => {
expect(
fixMdxScript(dedent`
it('fixMdxStyleTags fixes multiple style blocks', () => {
expect(
fixMdxStyleTags(dedent`
<style>{\`
.foo {}
\`}</style>
<style>{\`
.bar {}
\`}</style>
`)
).toMatchInlineSnapshot(`
).toMatchInlineSnapshot(`
<style>
{\`
.foo {}
Expand All @@ -46,5 +45,76 @@ describe('fix', () => {
\`}
</style>
`);
});
});

it('fixMdxComments fixes all comments', () => {
expect(
fixMdxComments(dedent`
# Hello
<!-- This is a comment -->
and this is not
<!-- This is another comment -->
`)
).toMatchInlineSnapshot(`
"# Hello
{/* This is a comment */}
and this is not
{/* This is another comment */}"
`);
});

it('fixMdxComments keeps html comments in codeblocks', () => {
expect(
fixMdxComments(dedent`
# Hello
~~~html
<!-- This is a comment -->
~~~
~~~html
<!-- This is a comment -->
~~~
\`\`\`html
<!-- This is a comment -->
\`\`\`
\`\`\`html
<!-- This is a comment -->
\`\`\`
and this is not
<!-- This is another comment -->
`)
).toMatchInlineSnapshot(`
"# Hello
~~~html
<!-- This is a comment -->
~~~
~~~html
<!-- This is a comment -->
~~~
\`\`\`html
<!-- This is a comment -->
\`\`\`
\`\`\`html
<!-- This is a comment -->
\`\`\`
and this is not
{/* This is another comment */}"
`);
});
35 changes: 26 additions & 9 deletions code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,28 @@ import fse from 'fs-extra';
import globby from 'globby';
import type { Fix } from '../types';

const MDX1_SCRIPT_START = /<style>{`/g;
const MDX1_SCRIPT_END = /`}<\/style>/g;
const MDX1_STYLE_START = /<style>{`/g;
const MDX1_STYLE_END = /`}<\/style>/g;
const MDX1_COMMENT = /<!--(.+)-->/g;
const MDX1_CODEBLOCK = /(?:\n~~~(?:\n|.)*?\n~~~)|(?:\n```(?:\n|.)*?\n```)/g;

export const fixMdxScript = (mdx: string) => {
return mdx.replace(MDX1_SCRIPT_START, '<style>\n {`').replace(MDX1_SCRIPT_END, ' `}\n</style>');
export const fixMdxStyleTags = (mdx: string) => {
return mdx.replace(MDX1_STYLE_START, '<style>\n {`').replace(MDX1_STYLE_END, ' `}\n</style>');
};

export const fixMdxComments = (mdx: string) => {
const codeblocks = mdx.matchAll(MDX1_CODEBLOCK);

// separate the mdx into sections without codeblocks & replace html comments NOT in codeblocks
const sections = mdx
.split(MDX1_CODEBLOCK)
.map((v) => v.replace(MDX1_COMMENT, (original, group) => `{/*${group}*/}`));

// interleave the original codeblocks with the replaced sections
return sections.reduce((acc, item, i) => {
const next = codeblocks.next();
return next.done ? acc + item : acc + item + next.value[0];
}, '');
};

const logger = console;
Expand Down Expand Up @@ -48,10 +65,10 @@ export const mdx1to2: Fix<Mdx1to2Options> = {
},

async run({ result: { storiesMdxFiles }, dryRun }) {
await Promise.all(
storiesMdxFiles.map(async (fname) => {
await Promise.all([
...storiesMdxFiles.map(async (fname) => {
const contents = await fse.readFile(fname, 'utf-8');
const updated = fixMdxScript(contents);
const updated = fixMdxComments(fixMdxStyleTags(contents));
if (updated === contents) {
logger.info(`🆗 Unmodified ${basename(fname)}`);
} else {
Expand All @@ -60,7 +77,7 @@ export const mdx1to2: Fix<Mdx1to2Options> = {
await fse.writeFile(fname, updated);
}
}
})
);
}),
]);
},
};

0 comments on commit 6a085f8

Please sign in to comment.