Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions apps/site/components/DownloadSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ type DownloadSectionProps = PropsWithChildren<{
releases: Array<NodeRelease>;
}>;

// 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<DownloadSectionProps> = async ({
releases,
children,
Expand All @@ -32,11 +46,23 @@ const DownloadSection: FC<DownloadSectionProps> = 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<NodeReleaseStatus> = pathname.endsWith('/alpha')
Expand Down