1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/website/readme-fns.js
Thomas Heartman 14e052b9ac
docs: auto-generate remaining server-side SDK docs (#2858)
This PR builds on the preceding doc auto-generation PRs and generates
documentation for the remaining server-side SDKs.

## Why

Refer to https://github.com/Unleash/unleash/pull/2809 for more context
about generating SDK docs.

## What

-   Adds generation for the remaining server-side SDKs
- Moves generated docs from the `/reference/sdks` directory to
`/generated` directory.
- Makes sure that the URLs do not change because of the move by using
the `slug` frontmatter property.
- replaces relative github links in the markdown documents so that they
become absolute github links. (refer to the next section)
- Updates some image styling so that it doesn't apply to readme badges
(we don't need them using `display: block`)

### On link replacing:

This PR adds handling of links in the generated documentation.
Specifically, it changes links in one case:

Relative links to github. Links to code and other files in the
repository. These are prefixed with the repository's URL.

While this should work in most cases, it will fail in cases where the
links to the files are not on the repository's primary branch.
(typically main, but could also be "v3", for instance). In these cases,
the links will get a double branch in the URL and will fail. However, I
see no easy way around this (though suggestions are definitely
accepted!), and think it's a fair tradeoff. It takes the links from
"definitely failing" to "will work in the vast majority of cases".

Note: I originally also wanted to handle the case where the link is an
absolute link to docs.getunleash.io. We could turn these into relative
urls to avoid full page reloads and enjoy a smoother experience.
However, the client-side redirects don't work correctly if the relative
URL goes to a redirect page, so you end up with a 404 page. As such, I
think it's better to leave the links as absolute for now.
2023-01-13 12:40:28 +01:00

152 lines
4.3 KiB
JavaScript

// Type definitions
//
// type Readme = {
// // This is the name that is placed before "SDK" in the sidebar.
// sidebarName: string;
//
// // The repo's primary branch. Falls back to "main" if nothing is defined
// branch?: string;
//
// // If present, this will be used to construct the slug. If no "slugName" is
// // defined, the `sidebarName` will be used to create the slug.
// slugName?: string;
// };
//
// type ReadmeData = Readme & { repoUrl: string };
// all SDK repos and what they map to for the sidebar.
const sdksUnmapped = {
'unleash-client-go': {
sidebarName: 'Go',
branch: 'v3',
},
'unleash-client-java': {
sidebarName: 'Java',
},
'unleash-client-node': {
sidebarName: 'Node',
},
'unleash-client-php': {
sidebarName: 'PHP',
},
'unleash-client-python': {
sidebarName: 'Python',
},
'unleash-client-ruby': {
sidebarName: 'Ruby',
},
'unleash-client-rust': {
sidebarName: 'Rust',
},
'unleash-client-dotnet': {
sidebarName: '.NET',
slugName: 'dotnet',
},
// 'unleash-android-proxy-sdk': {
// sidebarName: 'Android',
// slugName: 'android-proxy',
// },
};
const SDKS = Object.fromEntries(
Object.entries(sdksUnmapped).map(([repoName, repoData]) => {
const repoUrl = `https://github.com/Unleash/${repoName}`;
const slugName = (
repoData.slugName ?? repoData.sidebarName
).toLowerCase();
const branch = repoData.branch ?? 'main';
return [repoName, { ...repoData, repoUrl, slugName, branch }];
}),
);
function getReadmeRepoData(filename) {
const repoName = filename.split('/')[0];
const repoData = SDKS[repoName];
return repoData;
}
const documentUrls = Object.entries(SDKS).map(
([repo, { branch }]) => `${repo}/${branch}/README.md`,
);
// Replace links in the incoming readme content.
//
// There's one cases we want to handle:
//
// 1. Relative links that point to the repo. These must be prefixed with the
// link to the github repo.
//
// Note: You might be tempted to handle absolute links to docs.getunleash.io and
// make them relative. While absolute links will work, they trigger full page
// refreshes. Relative links give a slightly smoother user experience.
//
// However, if the old link goes to a redirect, then the client-side redirect
// will not kick in, so you'll end up with a "Page not found".
const replaceLinks = ({ content, repo }) => {
const markdownLink = /(?<=\[.*\]\(\s?)([^\s\)]+)(?=.*\))/g;
const replacer = (url) => {
try {
// This constructor will throw if the URL is relative.
// https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
const parsedUrl = new URL(url);
return url;
} catch {
// case 1
if (url.startsWith('#')) {
// ignore links to other doc sections
return url;
} else {
const separator = url.startsWith('/') ? '' : '/';
return `${repo.url}/blob/${repo.branch}${separator}${url}`;
}
}
};
return content.replaceAll(markdownLink, replacer);
};
const modifyContent = (filename, content) => {
const sdk = getReadmeRepoData(filename);
const generationTime = new Date();
return {
filename: `server-side/${sdk.slugName}.md`,
content: `---
title: ${sdk.sidebarName} SDK
slug: /reference/sdks/${sdk.slugName}
---
:::info Generated content
This document was generated from the README in the [${
sdk.sidebarName
} SDK's GitHub repository](${sdk.repoUrl}).
:::
:::tip Connecting to Unleash
To connect to Unleash, you'll need your Unleash API url (e.g. \`https://<your-unleash>/api\`) and a [server-side API token](/reference/api-tokens-and-client-keys.mdx#client-tokens) ([how do I create an API token?](/how-to/how-to-create-api-tokens.mdx)).
:::
${replaceLinks({ content, repo: { url: sdk.repoUrl, branch: sdk.branch } })}
---
This content was generated on <time datetime="${generationTime.toISOString()}">${generationTime.toLocaleString(
'en-gb',
{ dateStyle: 'long', timeStyle: 'full' },
)}</time>
`,
};
};
module.exports.readmes = {
documentUrls,
modifyContent,
};