From 6ca379bbe259e9e7f4e49fe5c2f890559720186f Mon Sep 17 00:00:00 2001 From: OnkarRuikar <87750369+OnkarRuikar@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:06:02 +0530 Subject: [PATCH 1/2] feature(tool/move): update files pointing to the moved document(s) --- content/document.ts | 2 ++ tool/cli.ts | 53 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/content/document.ts b/content/document.ts index 75133c4f4e16..a35e91e15113 100644 --- a/content/document.ts +++ b/content/document.ts @@ -114,6 +114,8 @@ export function saveFile( "tags", "original_slug", "browser-compat", + "status", + "l10n", ]; const saveMetadata = {}; diff --git a/tool/cli.ts b/tool/cli.ts index 348ca7cb6c94..4da4fe4c08b9 100644 --- a/tool/cli.ts +++ b/tool/cli.ts @@ -372,25 +372,27 @@ program // find references to the deleted document in content console.log("Checking references..."); - const referringFiles = []; + const referringFiles = new Set(); const allDocs = await Document.findAll(); for (const document of allDocs.iterDocs()) { const rawBody = document.rawBody; for (const deleted of deletedDocs) { const url = `/${locale}/docs/${deleted}`; if (rawBody.includes(url)) { - referringFiles.push(`${document.url}`); + referringFiles.add(`${document.url}`); } } } - if (referringFiles.length) { + if (referringFiles.size) { console.warn( chalk.yellow( - `\n${referringFiles.length} files are referring to the deleted document. ` + - `Please update the following files to remove the links:\n\t${referringFiles.join( - "\n\t" - )}` + `\n${referringFiles.size} files are referring to the deleted document(s). ` + + `Please update the following files to remove the links:\n\t${[ + ...referringFiles, + ] + .sort() + .join("\n\t")}` ) ); } else { @@ -443,8 +445,41 @@ program default: true, }); if (run) { - const moved = await Document.move(oldSlug, newSlug, locale); - console.log(chalk.green(`Moved ${moved.length} documents.`)); + const movedDocs = await Document.move(oldSlug, newSlug, locale); + console.log(chalk.green(`Moved ${movedDocs.length} documents.`)); + + // find and update references to the moved document + console.log("\nUpdating references..."); + const updatedFiles = new Set(); + const allDocs = await Document.findAll(); + for (const document of allDocs.iterDocs()) { + const rawBody = document.rawBody; + for (const [oldSlug, newSlug] of movedDocs) { + const updatedBody = rawBody.replaceAll(oldSlug, newSlug); + if (rawBody !== updatedBody) { + Document.saveFile( + document.fileInfo.path, + updatedBody, + document.metadata, + document.metadata.frontMatterKeys + ); + updatedFiles.add(document.url); + } + } + } + + if (updatedFiles.size) { + console.warn( + chalk.green( + `${updatedFiles.size} files referring to the moved document(s) have been updated:` + + `\n\t${[...updatedFiles.values()].sort().join("\n\t")}` + ) + ); + } else { + console.log( + chalk.green("\nNo file is referring to the moved document.") + ); + } } }) ) From 6c71a3134e1c3ba9452cc7e46f05d2f5d1a7b350 Mon Sep 17 00:00:00 2001 From: OnkarRuikar <87750369+OnkarRuikar@users.noreply.github.com> Date: Wed, 8 May 2024 12:00:19 +0530 Subject: [PATCH 2/2] fix bug --- content/document.ts | 2 -- tool/cli.ts | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/content/document.ts b/content/document.ts index a35e91e15113..75133c4f4e16 100644 --- a/content/document.ts +++ b/content/document.ts @@ -114,8 +114,6 @@ export function saveFile( "tags", "original_slug", "browser-compat", - "status", - "l10n", ]; const saveMetadata = {}; diff --git a/tool/cli.ts b/tool/cli.ts index 4da4fe4c08b9..e963fb8a4d3e 100644 --- a/tool/cli.ts +++ b/tool/cli.ts @@ -455,7 +455,10 @@ program for (const document of allDocs.iterDocs()) { const rawBody = document.rawBody; for (const [oldSlug, newSlug] of movedDocs) { - const updatedBody = rawBody.replaceAll(oldSlug, newSlug); + const updatedBody = rawBody.replace( + new RegExp(oldSlug, "g"), + newSlug + ); if (rawBody !== updatedBody) { Document.saveFile( document.fileInfo.path,