Skip to content

Commit

Permalink
fix: upload images in changelogs (#3623)
Browse files Browse the repository at this point in the history
* fix: upload images from changelogs

* fix: sort changelog items by date, desc
  • Loading branch information
abvthecity authored May 15, 2024
1 parent c9226c6 commit bcd3a95
Showing 1 changed file with 33 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,8 +158,6 @@ export async function publishDocs({

const { docsRegistrationId, uploadUrls } = startDocsRegisterResponse.body;

// const copiedRecord: Record<RelativeFilePath, string> = Object.assign({}, parsedDocsConfig.pages);

const collectedFileIds = new Map<RelativeFilePath, string>();

await Promise.all([
Expand Down Expand Up @@ -285,8 +297,7 @@ async function constructRegisterDocsRequest({
}
}),
{}
),
...convertedDocsConfiguration.pages
)
};
pages = Object.fromEntries(
Object.entries(pages).map(([pageId, pageContent]) => {
Expand Down Expand Up @@ -332,7 +343,6 @@ function createEditThisPageUrl(

interface ConvertedDocsConfiguration {
config: Omit<WithoutQuestionMarks<DocsV1Write.DocsConfig>, "logo" | "colors" | "typography" | "colorsV2">;
pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent>;
}

async function convertDocsConfiguration({
Expand Down Expand Up @@ -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<DocsV1Write.PageId, DocsV1Write.PageContent>;
}

async function convertNavigationConfig({
Expand All @@ -431,7 +437,6 @@ async function convertNavigationConfig({
absolutePathToFernFolder: AbsoluteFilePath;
}): Promise<ConvertedNavigationConfig> {
let config: DocsV1Write.NavigationConfig;
let pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent> = {};
switch (navigationConfig.type) {
case "untabbed": {
const untabbedItems = await Promise.all(
Expand All @@ -449,12 +454,6 @@ async function convertNavigationConfig({
})
)
);
for (const untabbedItem of untabbedItems) {
pages = {
...pages,
...untabbedItem.pages
};
}
config = {
items: untabbedItems.map((item) => item.item)
};
Expand All @@ -474,7 +473,6 @@ async function convertNavigationConfig({
config = {
tabs: tabbedItem.tabs
};
pages = { ...pages, ...tabbedItem.pages };
break;
}
case "versioned":
Expand All @@ -494,10 +492,6 @@ async function convertNavigationConfig({
fullSlugs,
absolutePathToFernFolder
});
pages = {
...pages,
...convertedNavigation.pages
};
return {
version: version.version,
config: convertedNavigation.config,
Expand All @@ -515,10 +509,7 @@ async function convertNavigationConfig({
default:
assertNever(navigationConfig);
}
return {
config,
pages
};
return { config };
}

function convertAvailability(availability: docsYml.RawSchemas.VersionAvailability): DocsV1Write.VersionAvailability {
Expand All @@ -538,7 +529,6 @@ function convertAvailability(availability: docsYml.RawSchemas.VersionAvailabilit

interface ConvertedUnversionedNavigationConfig {
config: DocsV1Write.UnversionedNavigationConfig;
pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent>;
}

async function convertUnversionedNavigationConfig({
Expand All @@ -565,7 +555,6 @@ async function convertUnversionedNavigationConfig({
absolutePathToFernFolder: AbsoluteFilePath;
}): Promise<ConvertedUnversionedNavigationConfig> {
let config: DocsV1Write.UnversionedNavigationConfig;
let pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent> = {};
switch (navigationConfig.type) {
case "untabbed": {
const untabbedItems = await Promise.all(
Expand 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": {
Expand All @@ -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(
Expand All @@ -642,11 +621,7 @@ async function convertTabbedNavigation(
fullSlugs: Record<DocsV1Write.PageId, { fullSlug?: string }>;
absolutePathToFernFolder: AbsoluteFilePath;
}
): Promise<{
tabs: DocsV1Write.NavigationTab[];
pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent>;
}> {
let pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent> = {};
): Promise<{ tabs: DocsV1Write.NavigationTab[] }> {
const convertedTabs = await Promise.all(
items.map(async (tabbedItem) => {
const tabConfig = tabs?.[tabbedItem.tab];
Expand Down Expand Up @@ -690,13 +665,6 @@ async function convertTabbedNavigation(
)
);

tabbedItems.forEach((tabbedItem) => {
pages = {
...pages,
...tabbedItem.pages
};
});

return {
title: tabConfig.displayName,
icon: tabConfig.icon,
Expand All @@ -705,10 +673,7 @@ async function convertTabbedNavigation(
};
})
);
return {
tabs: convertedTabs,
pages
};
return { tabs: convertedTabs };
}

function convertDocsTypographyConfiguration({
Expand Down Expand Up @@ -938,7 +903,6 @@ function convertImageReference({

interface ConvertedNavigationItem {
item: DocsV1Write.NavigationItem;
pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent>;
}

async function convertNavigationItem({
Expand All @@ -963,7 +927,6 @@ async function convertNavigationItem({
absolutePathToFernFolder: AbsoluteFilePath;
}): Promise<ConvertedNavigationItem> {
let convertedItem: DocsV1Write.NavigationItem;
let pages: Record<DocsV1Write.PageId, DocsV1Write.PageContent> = {};
switch (item.type) {
case "page": {
convertedItem = {
Expand Down Expand Up @@ -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": {
Expand All @@ -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("/"));
Expand All @@ -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,
Expand Down Expand Up @@ -1089,8 +1049,7 @@ async function convertNavigationItem({
assertNever(item);
}
return {
item: convertedItem,
pages
item: convertedItem
};
}

Expand Down

0 comments on commit bcd3a95

Please sign in to comment.