Skip to content

Commit

Permalink
feat: toc external link (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Jankowski authored Aug 14, 2020
1 parent 16cef72 commit ebd0670
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/TableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function TableOfContentsInner<T extends TableOfContentsItem = TableOfContentsIte
return (
<RowComponent
key={index}
item={item}
item={isExternalLink(item) ? { ...item, isExternalLink: true } : item}
index={index}
isExpanded={isExpanded}
toggleExpanded={toggleExpandedFunctions[index]}
Expand Down Expand Up @@ -346,3 +346,11 @@ function findAncestorIndices(currentDepth: number, precedingContents: TableOfCon
parentIndex,
];
}

function isExternalLink(item: TableOfContentsItem): boolean {
return isTableOfContentsLink(item) && item.to !== void 0 && /^(http|#|mailto)/.test(item.to);
}

function isTableOfContentsLink(item: TableOfContentsItem): item is ITableOfContentsLink {
return 'to' in item;
}

0 comments on commit ebd0670

Please sign in to comment.