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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion components/context/ArticleContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export type LearningTrack = {

export type MiniTocItem = {
platform: string
contents: string
contents: {
href: string
title: string
}
items?: MiniTocItem[]
}

Expand Down
7 changes: 1 addition & 6 deletions components/context/RestContext.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { createContext, useContext } from 'react'

export type MiniTocItem = {
platform: string
contents: string & { title: string; href: string }
items?: MiniTocItem[]
}
import type { MiniTocItem } from 'components/context/ArticleContext'

export type RestContextT = {
title: string
Expand Down
3 changes: 2 additions & 1 deletion components/sidebar/RestCollapsibleSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { ActionList } from '@primer/react'
import { Link } from 'components/Link'
import { ProductTreeNode } from 'components/context/MainContext'
import { EventType, sendEvent } from 'components/lib/events'
import { MiniTocItem, useRestContext } from 'components/context/RestContext'
import { useRestContext } from 'components/context/RestContext'
import type { MiniTocItem } from 'components/context/ArticleContext'
import styles from './SidebarProduct.module.scss'

type SectionProps = {
Expand Down
8 changes: 5 additions & 3 deletions components/ui/MiniTocs/MiniTocs.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cx from 'classnames'
import { ActionList, Heading } from '@primer/react'

import { MiniTocItem } from 'components/context/ArticleContext'
import type { MiniTocItem } from 'components/context/ArticleContext'
import { Link } from 'components/Link'
import { useTranslation } from 'components/hooks/useTranslation'

Expand All @@ -14,7 +14,7 @@ const renderTocItem = (item: MiniTocItem) => {
return (
<ActionList.Item
as="li"
key={item.contents}
key={item.contents.href}
className={item.platform}
sx={{
listStyle: 'none',
Expand All @@ -30,7 +30,9 @@ const renderTocItem = (item: MiniTocItem) => {
}}
>
<div className={cx('lh-condensed d-block width-full')}>
<div dangerouslySetInnerHTML={{ __html: item.contents }} />
<a className="d-block width-auto" href={item.contents.href}>
{item.contents.title}
</a>
{item.items && item.items.length > 0 ? (
<ul className="ml-3">{item.items.map(renderTocItem)}</ul>
) : null}
Expand Down
4 changes: 1 addition & 3 deletions lib/get-mini-toc-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export default function getMiniTocItems(
// remove any <strong> tags but leave content
$('strong', item).map((i, el) => $(el).replaceWith($(el).contents()))

const contents = isRestPage
? { href: `${href}`, title: `${$(item).text()}` }
: `<a class="d-block width-auto" href="${href}">${$(item).html()}</a>`
const contents = { href, title: $(item).text().trim() }
const headingLevel = parseInt($(item)[0].name.match(/\d+/)[0], 10) || 0 // the `2` from `h2`

const platform = $(item).parent('.extended-markdown').attr('class') || ''
Expand Down
2 changes: 1 addition & 1 deletion lib/rest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function getRestMiniTocItems(
// is needed to generate the toc
const titles = restOperations.map((operation) => `### ${operation.title}\n`).join('')
toc += await renderContent(titles, context)
const restOperationsMiniTocItems = getMiniTocItems(toc, 3, '', true)
const restOperationsMiniTocItems = getMiniTocItems(toc, 3, '')
languageTree.get(version).get(category).set(subCategory, {
restOperationsMiniTocItems,
})
Expand Down
6 changes: 1 addition & 5 deletions middleware/render-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,13 @@ async function buildRenderedPage(req) {
async function buildMiniTocItems(req) {
const { context } = req
const { page } = context
const isRestReferencePage =
page.relativePath.startsWith('rest') &&
!page.relativePath.includes('rest/guides') &&
!page.relativePath.includes('rest/overview')

// get mini TOC items on articles
if (!page.showMiniToc) {
return
}

return getMiniTocItems(context.renderedPage, page.miniTocMaxHeadingLevel, '', isRestReferencePage)
return getMiniTocItems(context.renderedPage, page.miniTocMaxHeadingLevel, '')
}

export default async function renderPage(req, res, next) {
Expand Down
2 changes: 1 addition & 1 deletion pages/[versionId]/rest/[category]/[subcategory].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
RestContext,
RestContextT,
getRestContextFromRequest,
MiniTocItem,
} from 'components/context/RestContext'
import type { MiniTocItem } from 'components/context/ArticleContext'

type MinitocItemsT = {
restOperationsMiniTocItems: MiniTocItem[]
Expand Down
2 changes: 1 addition & 1 deletion pages/[versionId]/rest/[category]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
RestContext,
RestContextT,
getRestContextFromRequest,
MiniTocItem,
} from 'components/context/RestContext'
import type { MiniTocItem } from 'components/context/ArticleContext'
import {
getTocLandingContextFromRequest,
TocItem,
Expand Down