From bcd3a9552f1e8ab68450cf298eef547f334f33f3 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Tue, 14 May 2024 20:40:13 -0400 Subject: [PATCH] fix: upload images in changelogs (#3623) * fix: upload images from changelogs * fix: sort changelog items by date, desc --- .../src/publishDocs.ts | 107 ++++++------------ 1 file changed, 33 insertions(+), 74 deletions(-) diff --git a/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts b/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts index c28482ccaeb..27baea5297e 100644 --- a/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts +++ b/packages/cli/generation/remote-generation/remote-workspace-runner/src/publishDocs.ts @@ -13,7 +13,7 @@ import chalk from "chalk"; import { readFile } from "fs/promises"; import matter from "gray-matter"; import { imageSize } from "image-size"; -import { last } from "lodash-es"; +import { last, orderBy } from "lodash-es"; import * as mime from "mime-types"; import terminalLink from "terminal-link"; import { promisify } from "util"; @@ -57,6 +57,20 @@ export async function publishDocs({ absoluteFilepathToDocsConfig: docsWorkspace.absoluteFilepathToDocsConfig }); + // track all changelog markdown files in parsedDocsConfig.pages + fernWorkspaces.forEach((workspace) => { + if (workspace.changelog != null) { + workspace.changelog.files.forEach((file) => { + const filename = last(file.absoluteFilepath.split("/")); + if (filename == null) { + return; + } + const relativePath = relative(docsWorkspace.absoluteFilepath, file.absoluteFilepath); + parsedDocsConfig.pages[relativePath] = file.contents; + }); + } + }); + const mdxImageReferences: docsYml.ImageReference[] = []; // preprocess markdown files to extract image paths @@ -144,8 +158,6 @@ export async function publishDocs({ const { docsRegistrationId, uploadUrls } = startDocsRegisterResponse.body; - // const copiedRecord: Record = Object.assign({}, parsedDocsConfig.pages); - const collectedFileIds = new Map(); await Promise.all([ @@ -285,8 +297,7 @@ async function constructRegisterDocsRequest({ } }), {} - ), - ...convertedDocsConfiguration.pages + ) }; pages = Object.fromEntries( Object.entries(pages).map(([pageId, pageContent]) => { @@ -332,7 +343,6 @@ function createEditThisPageUrl( interface ConvertedDocsConfiguration { config: Omit, "logo" | "colors" | "typography" | "colorsV2">; - pages: Record; } async function convertDocsConfiguration({ @@ -396,15 +406,11 @@ async function convertDocsConfiguration({ css: parsedDocsConfig.css, js: convertJavascriptConfiguration(parsedDocsConfig.js, uploadUrls, parsedDocsConfig) }; - return { - config, - pages: convertedNavigation.pages - }; + return { config }; } interface ConvertedNavigationConfig { config: DocsV1Write.NavigationConfig; - pages: Record; } async function convertNavigationConfig({ @@ -431,7 +437,6 @@ async function convertNavigationConfig({ absolutePathToFernFolder: AbsoluteFilePath; }): Promise { let config: DocsV1Write.NavigationConfig; - let pages: Record = {}; switch (navigationConfig.type) { case "untabbed": { const untabbedItems = await Promise.all( @@ -449,12 +454,6 @@ async function convertNavigationConfig({ }) ) ); - for (const untabbedItem of untabbedItems) { - pages = { - ...pages, - ...untabbedItem.pages - }; - } config = { items: untabbedItems.map((item) => item.item) }; @@ -474,7 +473,6 @@ async function convertNavigationConfig({ config = { tabs: tabbedItem.tabs }; - pages = { ...pages, ...tabbedItem.pages }; break; } case "versioned": @@ -494,10 +492,6 @@ async function convertNavigationConfig({ fullSlugs, absolutePathToFernFolder }); - pages = { - ...pages, - ...convertedNavigation.pages - }; return { version: version.version, config: convertedNavigation.config, @@ -515,10 +509,7 @@ async function convertNavigationConfig({ default: assertNever(navigationConfig); } - return { - config, - pages - }; + return { config }; } function convertAvailability(availability: docsYml.RawSchemas.VersionAvailability): DocsV1Write.VersionAvailability { @@ -538,7 +529,6 @@ function convertAvailability(availability: docsYml.RawSchemas.VersionAvailabilit interface ConvertedUnversionedNavigationConfig { config: DocsV1Write.UnversionedNavigationConfig; - pages: Record; } async function convertUnversionedNavigationConfig({ @@ -565,7 +555,6 @@ async function convertUnversionedNavigationConfig({ absolutePathToFernFolder: AbsoluteFilePath; }): Promise { let config: DocsV1Write.UnversionedNavigationConfig; - let pages: Record = {}; switch (navigationConfig.type) { case "untabbed": { const untabbedItems = await Promise.all( @@ -586,12 +575,6 @@ async function convertUnversionedNavigationConfig({ config = { items: untabbedItems.map((item) => item.item) }; - for (const untabbedItem of untabbedItems) { - pages = { - ...pages, - ...untabbedItem.pages - }; - } break; } case "tabbed": { @@ -608,16 +591,12 @@ async function convertUnversionedNavigationConfig({ config = { tabs: tabbedItem.tabs }; - pages = { ...pages, ...tabbedItem.pages }; break; } default: assertNever(navigationConfig); } - return { - config, - pages - }; + return { config }; } async function convertTabbedNavigation( @@ -642,11 +621,7 @@ async function convertTabbedNavigation( fullSlugs: Record; absolutePathToFernFolder: AbsoluteFilePath; } -): Promise<{ - tabs: DocsV1Write.NavigationTab[]; - pages: Record; -}> { - let pages: Record = {}; +): Promise<{ tabs: DocsV1Write.NavigationTab[] }> { const convertedTabs = await Promise.all( items.map(async (tabbedItem) => { const tabConfig = tabs?.[tabbedItem.tab]; @@ -690,13 +665,6 @@ async function convertTabbedNavigation( ) ); - tabbedItems.forEach((tabbedItem) => { - pages = { - ...pages, - ...tabbedItem.pages - }; - }); - return { title: tabConfig.displayName, icon: tabConfig.icon, @@ -705,10 +673,7 @@ async function convertTabbedNavigation( }; }) ); - return { - tabs: convertedTabs, - pages - }; + return { tabs: convertedTabs }; } function convertDocsTypographyConfiguration({ @@ -938,7 +903,6 @@ function convertImageReference({ interface ConvertedNavigationItem { item: DocsV1Write.NavigationItem; - pages: Record; } async function convertNavigationItem({ @@ -963,7 +927,6 @@ async function convertNavigationItem({ absolutePathToFernFolder: AbsoluteFilePath; }): Promise { let convertedItem: DocsV1Write.NavigationItem; - let pages: Record = {}; switch (item.type) { case "page": { convertedItem = { @@ -1003,12 +966,6 @@ async function convertNavigationItem({ hidden: item.hidden, skipUrlSlug: item.skipUrlSlug }; - for (const sectionItem of sectionItems) { - pages = { - ...pages, - ...sectionItem.pages - }; - } break; } case "apiSection": { @@ -1029,7 +986,7 @@ async function convertNavigationItem({ } }) }); - const changelogItems: DocsV1Write.ChangelogItem[] = []; + const unsortedChangelogItems: { date: Date; pageId: RelativeFilePath }[] = []; if (workspace.changelog != null) { for (const file of workspace.changelog.files) { const filename = last(file.absoluteFilepath.split("/")); @@ -1041,15 +998,18 @@ async function convertNavigationItem({ continue; } const relativePath = relative(absolutePathToFernFolder, file.absoluteFilepath); - pages[relativePath] = { - markdown: file.contents - }; - changelogItems.push({ - date: changelogDate.toISOString(), - pageId: relativePath - }); + unsortedChangelogItems.push({ date: changelogDate, pageId: relativePath }); } } + + // sort changelog items by date, in descending order + const changelogItems = orderBy(unsortedChangelogItems, (item) => item.date, "desc").map( + (item): DocsV1Write.ChangelogItem => ({ + date: item.date.toISOString(), + pageId: item.pageId + }) + ); + convertedItem = { type: "api", title: item.title, @@ -1089,8 +1049,7 @@ async function convertNavigationItem({ assertNever(item); } return { - item: convertedItem, - pages + item: convertedItem }; }