1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-10 17:53:36 +02:00

docs: handle links in generated content

This change adds handling of both GitHub links and docs.getunleash
links to the generated docs.
This commit is contained in:
Thomas Heartman 2023-01-05 22:12:41 +01:00
parent 3131107838
commit 468d687a50
No known key found for this signature in database
GPG Key ID: 47CFBB2D87C87664

View File

@ -15,7 +15,7 @@
// type ReadmeData = Readme & { repoUrl: string };
// all SDK repos and what they map to for the sidebar.
const SDKS = {
const SDKS_base = {
'unleash-client-go': {
sidebarName: 'Go',
branch: 'v3',
@ -33,26 +33,70 @@ const SDKS = {
// },
};
const SDKS = Object.fromEntries(
Object.entries(SDKS_base).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];
const repoUrl = `https://github.com/Unleash/${repoName}`;
if (repoData) {
return {
repoUrl,
...repoData,
slugName: (repoData.slugName ?? repoData.sidebarName).toLowerCase(),
};
} else return { sidebarName: repoName, repoUrl };
return repoData;
}
const documentUrls = Object.entries(SDKS).map(
([repo, { branch = 'main' }]) => `${repo}/${branch ?? 'main'}/README.md`,
([repo, { branch }]) => `${repo}/${branch}/README.md`,
);
// Replace links in the incoming readme content.
//
// There's two cases we want to handle:
//
// 1. Relative links that point to the repo. These must be prefixed with the
// link to the github repo.
//
// 2. Absolute links to docs.getunleash.io. While absolute links will work, they
// trigger full page refreshes. If we can make them relative links instead, then
// we'll get a slightly smoother user experience.
const replaceLinks = ({ content, repo }) => {
const markdownLink = /(?<=\[.*\]\(\s?)(\S+)(?=.*\))/g;
const replacer = (url) => {
// case 2:
const docsUrl = 'https://docs.getunleash.io';
if (url.startsWith(docsUrl)) {
return url.substring(docsUrl.length);
}
// case 1
try {
// This constructor will throw if the URL is relative.
// https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
new URL(url);
return url;
} catch {
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);
@ -74,7 +118,7 @@ This document was generated from the README in the [${
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)).
:::
${content}
${replaceLinks({ content, repo: { url: sdk.repoUrl, branch: sdk.branch } })}
---