Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions src/components/UserRelatedApiEnvironmentNotice/paths.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
const PROD_CATEGORY_PATH = '/docs/category/user-related-apis';
const PRELIVE_CATEGORY_PATH = '/docs/category/user-related-apis-pre-live';
const PROD_LATEST_PREFIX = '/docs/user_related_apis_versioned/';
const PRELIVE_PREFIX = '/docs/user_related_apis_prelive/';
const PROD_LATEST_ROOT = '/docs/user_related_apis_versioned';
const PRELIVE_ROOT = '/docs/user_related_apis_prelive';
const PROD_LATEST_PREFIX = `${PROD_LATEST_ROOT}/`;
const PRELIVE_PREFIX = `${PRELIVE_ROOT}/`;
const PROD_VERSION_PREFIX_REGEX = /^\d+\.\d+\.\d+(\/|$)/;

const normalizePath = (value) =>
value && value !== '/' ? value.replace(/\/+$/, '') : value;
Comment thread
basit3407 marked this conversation as resolved.
const isPathInTree = (pathname, rootPath) =>
pathname === rootPath || pathname.startsWith(`${rootPath}/`);
const isApiTreeRoot = (pathname) =>
pathname === PROD_LATEST_ROOT || pathname === PRELIVE_ROOT;
const hasAvailablePath = (availablePaths, targetPath) => {
const normalizedTargetPath = normalizePath(targetPath);

return (
availablePaths.has(normalizedTargetPath) ||
availablePaths.has(`${normalizedTargetPath}/`)
);
};
const stripPrefix = (value, prefix) => value.slice(prefix.length);
const getFallbackPath = (environment) =>
environment === 'production' ? PROD_CATEGORY_PATH : PRELIVE_CATEGORY_PATH;
Expand All @@ -15,7 +31,7 @@ function getUserRelatedDocsAvailablePaths(allDocsData) {
pluginData?.versions?.forEach((version) => {
version?.docs?.forEach((doc) => {
if (typeof doc?.path === 'string') {
paths.add(doc.path);
paths.add(normalizePath(doc.path));
}
});
});
Expand All @@ -25,16 +41,18 @@ function getUserRelatedDocsAvailablePaths(allDocsData) {
}

function getUserRelatedDocsEnvironment(pathname) {
const normalizedPathname = normalizePath(pathname);

if (
pathname === PROD_CATEGORY_PATH ||
pathname.startsWith(PROD_LATEST_PREFIX)
normalizedPathname === PROD_CATEGORY_PATH ||
isPathInTree(normalizedPathname, PROD_LATEST_ROOT)
) {
return 'production';
}

if (
pathname === PRELIVE_CATEGORY_PATH ||
pathname.startsWith(PRELIVE_PREFIX)
normalizedPathname === PRELIVE_CATEGORY_PATH ||
isPathInTree(normalizedPathname, PRELIVE_ROOT)
) {
return 'pre-live';
}
Expand All @@ -43,36 +61,45 @@ function getUserRelatedDocsEnvironment(pathname) {
}

function getTargetPath(pathname, targetEnvironment) {
const currentEnvironment = getUserRelatedDocsEnvironment(pathname);
const normalizedPathname = normalizePath(pathname);
const currentEnvironment = getUserRelatedDocsEnvironment(normalizedPathname);
const fallbackPath = getFallbackPath(targetEnvironment);
let targetPath = fallbackPath;

if (currentEnvironment && currentEnvironment === targetEnvironment) {
return {
fallbackPath,
targetPath: pathname,
targetPath: normalizedPathname,
};
}

if (targetEnvironment === 'production') {
if (pathname === PRELIVE_CATEGORY_PATH) {
if (normalizedPathname === PRELIVE_CATEGORY_PATH) {
targetPath = PROD_CATEGORY_PATH;
} else if (pathname.startsWith(PRELIVE_PREFIX)) {
targetPath = `${PROD_LATEST_PREFIX}${stripPrefix(pathname, PRELIVE_PREFIX)}`;
} else if (isPathInTree(normalizedPathname, PRELIVE_ROOT)) {
targetPath =
normalizedPathname === PRELIVE_ROOT
? PROD_LATEST_ROOT
: `${PROD_LATEST_PREFIX}${stripPrefix(normalizedPathname, PRELIVE_PREFIX)}`;
}
} else if (targetEnvironment === 'pre-live') {
if (pathname === PROD_CATEGORY_PATH) {
if (normalizedPathname === PROD_CATEGORY_PATH) {
targetPath = PRELIVE_CATEGORY_PATH;
} else if (pathname.startsWith(PROD_LATEST_PREFIX)) {
const relativePath = stripPrefix(pathname, PROD_LATEST_PREFIX);
} else if (isPathInTree(normalizedPathname, PROD_LATEST_ROOT)) {
const relativePath =
normalizedPathname === PROD_LATEST_ROOT
? ''
: stripPrefix(normalizedPathname, PROD_LATEST_PREFIX);
const normalizedRelativePath = relativePath.replace(PROD_VERSION_PREFIX_REGEX, '');

targetPath = `${PRELIVE_PREFIX}${normalizedRelativePath}`;
targetPath = normalizedRelativePath
? `${PRELIVE_PREFIX}${normalizedRelativePath}`
: PRELIVE_ROOT;
}
} else {
return {
fallbackPath: pathname,
targetPath: pathname,
fallbackPath: normalizedPathname,
targetPath: normalizedPathname,
};
}

Expand All @@ -84,10 +111,10 @@ function getTargetPath(pathname, targetEnvironment) {

function getUserRelatedDocsTarget(pathname, targetEnvironment, options = {}) {
const { fallbackPath, targetPath } = getTargetPath(pathname, targetEnvironment);
const hasExplicitDocTarget = targetPath !== fallbackPath;
const hasExplicitDocTarget = targetPath !== fallbackPath && !isApiTreeRoot(targetPath);
const hasEquivalentDoc = !hasExplicitDocTarget || !options.availablePaths
? true
: options.availablePaths.has(targetPath);
: hasAvailablePath(options.availablePaths, targetPath);
const path = hasEquivalentDoc ? targetPath : fallbackPath;

return {
Expand Down
64 changes: 63 additions & 1 deletion tests/user-related-env-paths.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,31 @@ test('detects production and pre-live user-related docs routes', () => {
getUserRelatedDocsEnvironment('/docs/user_related_apis_prelive/get-user-profile'),
'pre-live',
);
assert.equal(
getUserRelatedDocsEnvironment('/docs/user_related_apis_versioned/get-user-profile/'),
'production',
);
assert.equal(
getUserRelatedDocsEnvironment('/docs/user_related_apis_prelive/get-user-profile/'),
'pre-live',
);
assert.equal(getUserRelatedDocsEnvironment('/docs/user_related_apis_versioned'), 'production');
assert.equal(getUserRelatedDocsEnvironment('/docs/user_related_apis_versioned/'), 'production');
assert.equal(getUserRelatedDocsEnvironment('/docs/user_related_apis_prelive'), 'pre-live');
assert.equal(getUserRelatedDocsEnvironment('/docs/user_related_apis_prelive/'), 'pre-live');
assert.equal(getUserRelatedDocsEnvironment('/docs/category/user-related-apis'), 'production');
assert.equal(
getUserRelatedDocsEnvironment('/docs/category/user-related-apis/'),
'production',
);
assert.equal(
getUserRelatedDocsEnvironment('/docs/category/user-related-apis-pre-live'),
'pre-live',
);
assert.equal(
getUserRelatedDocsEnvironment('/docs/category/user-related-apis-pre-live/'),
'pre-live',
);
assert.equal(getUserRelatedDocsEnvironment('/docs/category/content-apis'), null);
});

Expand All @@ -55,6 +75,20 @@ test('maps the current doc route between production and pre-live trees', () => {
),
'/docs/user_related_apis_versioned/get-user-profile',
);
assert.equal(
getUserRelatedDocsPath(
'/docs/user_related_apis_prelive/get-user-profile/',
'production',
),
'/docs/user_related_apis_versioned/get-user-profile',
);
assert.equal(
getUserRelatedDocsPath(
'/docs/user_related_apis_versioned/1.0.0/get-user-profile/',
'pre-live',
),
'/docs/user_related_apis_prelive/get-user-profile',
);
assert.equal(
getUserRelatedDocsPath(
'/docs/user_related_apis_versioned/get-user-profile',
Expand All @@ -73,6 +107,18 @@ test('maps the current doc route between production and pre-live trees', () => {
getUserRelatedDocsPath('/docs/category/user-related-apis', 'pre-live'),
'/docs/category/user-related-apis-pre-live',
);
assert.equal(
getUserRelatedDocsPath('/docs/category/user-related-apis/', 'pre-live'),
'/docs/category/user-related-apis-pre-live',
);
assert.equal(
getUserRelatedDocsPath('/docs/user_related_apis_versioned/', 'pre-live'),
'/docs/user_related_apis_prelive',
);
assert.equal(
getUserRelatedDocsPath('/docs/user_related_apis_prelive/', 'production'),
'/docs/user_related_apis_versioned',
);
assert.equal(
getUserRelatedDocsPath('/docs/category/user-related-apis-pre-live', 'production'),
'/docs/category/user-related-apis',
Expand Down Expand Up @@ -128,6 +174,22 @@ test('falls back to the environment category when the target doc does not exist'
);
assert.equal(versionedProdToPreliveTarget.path, '/docs/user_related_apis_prelive/get-user-profile');
assert.equal(versionedProdToPreliveTarget.hasEquivalentDoc, true);

const trailingSlashTarget = getUserRelatedDocsTarget(
'/docs/user_related_apis_prelive/get-user-profile/',
'production',
{ availablePaths },
);
assert.equal(trailingSlashTarget.path, '/docs/user_related_apis_versioned/get-user-profile');
assert.equal(trailingSlashTarget.hasEquivalentDoc, true);

const rootTarget = getUserRelatedDocsTarget(
'/docs/user_related_apis_versioned/',
'pre-live',
{ availablePaths },
);
assert.equal(rootTarget.path, '/docs/user_related_apis_prelive');
assert.equal(rootTarget.hasEquivalentDoc, true);
});

test('collects available docs paths from Docusaurus docs data', () => {
Expand All @@ -137,7 +199,7 @@ test('collects available docs paths from Docusaurus docs data', () => {
{
docs: [
{ path: '/docs/user_related_apis_versioned/get-user-profile' },
{ path: '/docs/user_related_apis_prelive/get-sync-mutations' },
{ path: '/docs/user_related_apis_prelive/get-sync-mutations/' },
],
},
],
Expand Down