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:
parent
3131107838
commit
468d687a50
@ -15,7 +15,7 @@
|
|||||||
// type ReadmeData = Readme & { repoUrl: string };
|
// type ReadmeData = Readme & { repoUrl: string };
|
||||||
|
|
||||||
// all SDK repos and what they map to for the sidebar.
|
// all SDK repos and what they map to for the sidebar.
|
||||||
const SDKS = {
|
const SDKS_base = {
|
||||||
'unleash-client-go': {
|
'unleash-client-go': {
|
||||||
sidebarName: 'Go',
|
sidebarName: 'Go',
|
||||||
branch: 'v3',
|
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) {
|
function getReadmeRepoData(filename) {
|
||||||
const repoName = filename.split('/')[0];
|
const repoName = filename.split('/')[0];
|
||||||
|
|
||||||
const repoData = SDKS[repoName];
|
const repoData = SDKS[repoName];
|
||||||
|
|
||||||
const repoUrl = `https://github.com/Unleash/${repoName}`;
|
return repoData;
|
||||||
|
|
||||||
if (repoData) {
|
|
||||||
return {
|
|
||||||
repoUrl,
|
|
||||||
...repoData,
|
|
||||||
slugName: (repoData.slugName ?? repoData.sidebarName).toLowerCase(),
|
|
||||||
};
|
|
||||||
} else return { sidebarName: repoName, repoUrl };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const documentUrls = Object.entries(SDKS).map(
|
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 modifyContent = (filename, content) => {
|
||||||
const sdk = getReadmeRepoData(filename);
|
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)).
|
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 } })}
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user