Skip to content

Commit

Permalink
DN-6: Build the link by joining parts.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Dec 16, 2024
1 parent 59b2609 commit dacd2b0
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions client/src/components/pages/SideBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import path from 'path';
import {
SideBarContainer,
SideBarSiteName,
Expand Down Expand Up @@ -51,7 +52,7 @@ function renderSideBarContent(posts: PostMetadata[] | DefinitionMetadata[]) {
<SideBarEntriesContainer key={meta.slug}>
<SideEntryLink
key={meta.slug}
href={`/${meta.section}/${meta.slug}`}
href={buildHref(meta)}
passHref
>
<span style={{ fontWeight: 'bold' }}>
Expand All @@ -70,7 +71,7 @@ function renderSideBarContent(posts: PostMetadata[] | DefinitionMetadata[]) {
<SideBarEntriesContainer key={meta.slug}>
<SideEntryLink
key={meta.slug}
href={`/dictionaries/${meta.dictionary}/${meta.slug}`}
href={buildHref(meta)}
passHref
>
{meta.word}
Expand All @@ -81,3 +82,19 @@ function renderSideBarContent(posts: PostMetadata[] | DefinitionMetadata[]) {
);
}
}

function buildHref(meta: PostMetadata | DefinitionMetadata): string {
if (isPostMetadata(meta)) {
if (!meta.path) {
throw new Error('Post metadata does not have a valid "path".');
}
return path.join('/', meta.path);
} else {
if (!meta.dictionary || !meta.slug) {
throw new Error(
'Definition metadata must include both "dictionary" and "slug".'
);
}
return `/dictionaries/${meta.dictionary}/${meta.slug}`;
}
}

0 comments on commit dacd2b0

Please sign in to comment.