Skip to content

Commit

Permalink
chore: appropriate base url and results for api
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd committed Feb 28, 2024
1 parent 1d70c58 commit 04fd007
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
14 changes: 6 additions & 8 deletions app/[locale]/next-data/api-data/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import type { GitHubApiFile } from '@/types';
import { getGitHubApiDocsUrl } from '@/util/gitHubUtils';
import { parseRichTextIntoPlainText } from '@/util/stringUtils';

const getPathnameForApiFile = (name: string) =>
`api/${name.replace('.md', '.html')}`;
const getPathnameForApiFile = (name: string, version: string) =>
`docs/${version}/api/${name.replace('.md', '.html')}`;

// This is the Route Handler for the `GET` method which handles the request
// for a digest and metadata of all API pages from the Node.js Website
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
export const GET = async () => {
const releases = await getReleaseData();

const latestLTSRelease = releases.find(release =>
const { versionWithPrefix } = releases.find(release =>
['Active LTS', 'Maintenance LTS'].includes(release.status)
);
)!;

const gitHubApiResponse = await fetch(
getGitHubApiDocsUrl(latestLTSRelease!.versionWithPrefix)
);
const gitHubApiResponse = await fetch(getGitHubApiDocsUrl(versionWithPrefix));

return gitHubApiResponse.json().then((apiDocsFiles: Array<GitHubApiFile>) => {
// maps over each api file and get the download_url, fetch the content and deflates it
Expand All @@ -41,7 +39,7 @@ export const GET = async () => {

return {
filename,
pathname: getPathnameForApiFile(name),
pathname: getPathnameForApiFile(name, versionWithPrefix),
content: deflatedSource,
};
}
Expand Down
3 changes: 2 additions & 1 deletion components/Common/Search/States/WithSearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FC } from 'react';

import { pathToBreadcrumbs } from '@/components/Common/Search/utils';
import Link from '@/components/Link';
import { BASE_URL } from '@/next.constants.mjs';
import { highlighter } from '@/next.orama.mjs';
import type { SearchDoc } from '@/types';

Expand All @@ -15,7 +16,7 @@ type SearchResultProps = {

export const WithSearchResult: FC<SearchResultProps> = props => {
const isAPIResult = props.hit.document.siteSection.toLowerCase() === 'api';
const basePath = isAPIResult ? 'https://nodejs.org' : '';
const basePath = isAPIResult ? BASE_URL : '';
const path = `${basePath}/${props.hit.document.path}`;

return (
Expand Down
14 changes: 10 additions & 4 deletions components/MDX/SearchPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WithPoweredBy } from '@/components/Common/Search/States/WithPoweredBy';
import { pathToBreadcrumbs } from '@/components/Common/Search/utils';
import Link from '@/components/Link';
import { useBottomScrollListener } from '@/hooks/react-client';
import { DEFAULT_ORAMA_QUERY_PARAMS } from '@/next.constants.mjs';
import { BASE_URL, DEFAULT_ORAMA_QUERY_PARAMS } from '@/next.constants.mjs';
import { search as oramaSearch, highlighter } from '@/next.orama.mjs';
import type { SearchDoc } from '@/types';

Expand Down Expand Up @@ -70,8 +70,11 @@ const SearchPage: FC = () => {
? { where: { siteSection: { eq: searchSection } } }
: {};

const getDocumentURL = (path: string) =>
path.startsWith('api/') ? `https://nodejs.org/${path}` : path;
const getDocumentURL = (siteSection: string, path: string) => {
const isAPIResult = siteSection.toLowerCase() === 'api';
const basePath = isAPIResult ? BASE_URL : '';
return `${basePath}/${path}`;
};

return (
<div className={styles.searchPageContainer}>
Expand Down Expand Up @@ -103,8 +106,11 @@ const SearchPage: FC = () => {
{hits?.map(hit => (
<Link
key={hit.id}
href={getDocumentURL(hit.document.path)}
className={styles.searchResult}
href={getDocumentURL(
hit.document.siteSection.toLowerCase(),
hit.document.path
)}
>
<div>
<h2 className={styles.searchResultTitle}>
Expand Down

0 comments on commit 04fd007

Please sign in to comment.