diff --git a/apps/site/components/DownloadSection.tsx b/apps/site/components/DownloadSection.tsx index b6fe875fecb30..eb27a4a5d83d4 100644 --- a/apps/site/components/DownloadSection.tsx +++ b/apps/site/components/DownloadSection.tsx @@ -16,6 +16,20 @@ type DownloadSectionProps = PropsWithChildren<{ releases: Array; }>; +// Ignore translatable comments and blank lines when comparing snippet commands. +const normalizeSnippetCommands = (content: string) => + content + .split(/\r?\n/) + .filter(line => line.trim() && !line.trimStart().startsWith('#')) + .map(line => + line + // Keep the opening backtick when removing a template's first comment. + .replace(/^([ \t]*`)#[^`\\]*$/, '$1') + // Ignore inline comments after Node.js and npm version checks. + .replace(/^([ \t]*(?:node|npm) -v)[ \t]+#.*$/, '$1') + ) + .join('\n'); + const DownloadSection: FC = async ({ releases, children, @@ -32,11 +46,23 @@ const DownloadSection: FC = async ({ const { pathname } = getClientContext(); + // Exclude outdated translations so the merge can use their English snippets. + const currentLocaleSnippets = localeSnippets.filter(snippet => { + const fallback = fallbackSnippets.find(s => s.name === snippet.name); + return ( + !fallback || + normalizeSnippetCommands(snippet.content) === + normalizeSnippetCommands(fallback.content) + ); + }); + // Some available translations do not have download snippets translated or have them partially translated // This aims to merge the available translated snippets with the fallback snippets const memoizedSnippets = fallbackSnippets - .filter(snippet => !localeSnippets.some(s => s.name === snippet.name)) - .concat(localeSnippets); + .filter( + snippet => !currentLocaleSnippets.some(s => s.name === snippet.name) + ) + .concat(currentLocaleSnippets); // Decides which initial release to use based on the current pathname const initialRelease: Array = pathname.endsWith('/alpha')