From 3d50c6b652c38560e2249564b71083ccd71321cb Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Tue, 20 Dec 2022 18:15:06 +0100 Subject: [PATCH 1/3] add migration for MDX html-type comments to JS-type comments --- .../src/automigrate/fixes/mdx-1-to-2.test.ts | 44 ++++++++++++----- .../cli/src/automigrate/fixes/mdx-1-to-2.ts | 47 ++++++++++++++----- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts index ea82440003f6..e07e6bdc28f6 100644 --- a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts +++ b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts @@ -1,19 +1,18 @@ /// ; 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` `) - ).toEqual(dedent` + ).toEqual(dedent` `); - }); +}); - it('fixes multiple style blocks', () => { - expect( - fixMdxScript(dedent` +it('fixMdxStyleTags fixes multiple style blocks', () => { + expect( + fixMdxStyleTags(dedent` @@ -34,7 +33,7 @@ describe('fix', () => { .bar {} \`} `) - ).toMatchInlineSnapshot(` + ).toMatchInlineSnapshot(` `); - }); +}); + +it('fixMdxComments fixes all comments', () => { + expect( + fixMdxComments(dedent` + # Hello + + + + and this is not + + + `) + ).toMatchInlineSnapshot(` + "# Hello + + {/* This is a comment */} + + and this is not + + {/* This is another comment */}" + `); }); diff --git a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts index 0702d0434df1..1eeab15c3cec 100644 --- a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts +++ b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts @@ -5,17 +5,24 @@ import fse from 'fs-extra'; import globby from 'globby'; import type { Fix } from '../types'; -const MDX1_SCRIPT_START = /'); +export const fixMdxStyleTags = (mdx: string) => { + return mdx.replace(MDX1_STYLE_START, ''); +}; + +export const fixMdxComments = (mdx: string) => { + return mdx.replace(MDX1_COMMENT, (original, group) => `{/*${group}*/}`); }; const logger = console; interface Mdx1to2Options { storiesMdxFiles: string[]; + allMdxFiles: string[]; + nonStoriesMdx1Files: string[]; } /** @@ -30,7 +37,13 @@ export const mdx1to2: Fix = { async check() { const storiesMdxFiles = await globby('./!(node_modules)**/*.(story|stories).mdx'); - return storiesMdxFiles.length ? { storiesMdxFiles } : undefined; + const allMdxFiles = await globby('./!(node_modules)**/*.mdx'); + + const nonStoriesMdx1Files = allMdxFiles.filter((fname) => !storiesMdxFiles.includes(fname)); + + return storiesMdxFiles.length + ? { storiesMdxFiles, allMdxFiles, nonStoriesMdx1Files } + : undefined; }, prompt({ storiesMdxFiles }) { @@ -47,11 +60,23 @@ export const mdx1to2: Fix = { `; }, - async run({ result: { storiesMdxFiles }, dryRun }) { - await Promise.all( - storiesMdxFiles.map(async (fname) => { + async run({ result: { storiesMdxFiles, nonStoriesMdx1Files }, dryRun }) { + await Promise.all([ + ...storiesMdxFiles.map(async (fname) => { + const contents = await fse.readFile(fname, 'utf-8'); + const updated = fixMdxComments(fixMdxStyleTags(contents)); + if (updated === contents) { + logger.info(`🆗 Unmodified ${basename(fname)}`); + } else { + logger.info(`✅ Modified ${basename(fname)}`); + if (!dryRun) { + await fse.writeFile(fname, updated); + } + } + }), + ...nonStoriesMdx1Files.map(async (fname) => { const contents = await fse.readFile(fname, 'utf-8'); - const updated = fixMdxScript(contents); + const updated = fixMdxComments(contents); if (updated === contents) { logger.info(`🆗 Unmodified ${basename(fname)}`); } else { @@ -60,7 +85,7 @@ export const mdx1to2: Fix = { await fse.writeFile(fname, updated); } } - }) - ); + }), + ]); }, }; From 28808e83474c4931f89d28aa22a9e499eb1fba80 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 9 Jan 2023 14:34:23 +0100 Subject: [PATCH 2/3] changer behavior to accommodate for codeblocks --- .../src/automigrate/fixes/mdx-1-to-2.test.ts | 34 +++++++++++++++++ .../cli/src/automigrate/fixes/mdx-1-to-2.ts | 38 ++++++++----------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts index e07e6bdc28f6..03e6c331f230 100644 --- a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts +++ b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts @@ -68,3 +68,37 @@ it('fixMdxComments fixes all comments', () => { {/* This is another comment */}" `); }); + +it('fixMdxComments keeps html comments in codeblocks', () => { + expect( + fixMdxComments(dedent` + # Hello + + ~~~html + + ~~~ + + ~~~html + + ~~~ + + and this is not + + + `) + ).toMatchInlineSnapshot(` + "# Hello + + ~~~html + + ~~~ + + ~~~html + + ~~~ + + and this is not + + {/* This is another comment */}" + `); +}); diff --git a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts index 1eeab15c3cec..baa316241884 100644 --- a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts +++ b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts @@ -8,21 +8,31 @@ import type { Fix } from '../types'; const MDX1_STYLE_START = /'); }; export const fixMdxComments = (mdx: string) => { - return mdx.replace(MDX1_COMMENT, (original, group) => `{/*${group}*/}`); + 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; interface Mdx1to2Options { storiesMdxFiles: string[]; - allMdxFiles: string[]; - nonStoriesMdx1Files: string[]; } /** @@ -37,13 +47,7 @@ export const mdx1to2: Fix = { async check() { const storiesMdxFiles = await globby('./!(node_modules)**/*.(story|stories).mdx'); - const allMdxFiles = await globby('./!(node_modules)**/*.mdx'); - - const nonStoriesMdx1Files = allMdxFiles.filter((fname) => !storiesMdxFiles.includes(fname)); - - return storiesMdxFiles.length - ? { storiesMdxFiles, allMdxFiles, nonStoriesMdx1Files } - : undefined; + return storiesMdxFiles.length ? { storiesMdxFiles } : undefined; }, prompt({ storiesMdxFiles }) { @@ -60,7 +64,7 @@ export const mdx1to2: Fix = { `; }, - async run({ result: { storiesMdxFiles, nonStoriesMdx1Files }, dryRun }) { + async run({ result: { storiesMdxFiles }, dryRun }) { await Promise.all([ ...storiesMdxFiles.map(async (fname) => { const contents = await fse.readFile(fname, 'utf-8'); @@ -74,18 +78,6 @@ export const mdx1to2: Fix = { } } }), - ...nonStoriesMdx1Files.map(async (fname) => { - const contents = await fse.readFile(fname, 'utf-8'); - const updated = fixMdxComments(contents); - if (updated === contents) { - logger.info(`🆗 Unmodified ${basename(fname)}`); - } else { - logger.info(`✅ Modified ${basename(fname)}`); - if (!dryRun) { - await fse.writeFile(fname, updated); - } - } - }), ]); }, }; From 43c0a32be39d6b9561497d38712b485c010929ec Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Thu, 12 Jan 2023 12:37:41 +0100 Subject: [PATCH 3/3] expand tests --- .../cli/src/automigrate/fixes/mdx-1-to-2.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts index 03e6c331f230..d3f1556f42b9 100644 --- a/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts +++ b/code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts @@ -82,6 +82,14 @@ it('fixMdxComments keeps html comments in codeblocks', () => { ~~~ + \`\`\`html + + \`\`\` + + \`\`\`html + + \`\`\` + and this is not @@ -97,6 +105,14 @@ it('fixMdxComments keeps html comments in codeblocks', () => { ~~~ + \`\`\`html + + \`\`\` + + \`\`\`html + + \`\`\` + and this is not {/* This is another comment */}"