Skip to content

Commit 41c40fc

Browse files
yufeihp-kostov
authored andcommitted
feat: expand TOC on common URL rewrite rules (dotnet#9408)
Co-authored-by: Yufei Huang <[email protected]>
1 parent 123a896 commit 41c40fc

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
import { breakWord } from './helper'
4+
import { breakWord, isSameURL } from './helper'
55

6-
test('break-text', () => {
6+
test('break text', () => {
77
expect(breakWord('Other APIs')).toEqual(['Other APIs'])
88
expect(breakWord('System.CodeDom')).toEqual(['System.', 'Code', 'Dom'])
99
expect(breakWord('System.Collections.Dictionary<string, object>')).toEqual(['System.', 'Collections.', 'Dictionary<', 'string,', ' object>'])
1010
expect(breakWord('https://github.com/dotnet/docfx')).toEqual(['https://github.', 'com/', 'dotnet/', 'docfx'])
1111
})
12+
13+
test('is same URL', () => {
14+
expect(isSameURL({ pathname: '/' }, { pathname: '/' })).toBeTruthy()
15+
expect(isSameURL({ pathname: '/index.html' }, { pathname: '/' })).toBeTruthy()
16+
expect(isSameURL({ pathname: '/a/index.html' }, { pathname: '/a' })).toBeTruthy()
17+
expect(isSameURL({ pathname: '/a/index.html' }, { pathname: '/a/' })).toBeTruthy()
18+
expect(isSameURL({ pathname: '/a' }, { pathname: '/a/' })).toBeTruthy()
19+
expect(isSameURL({ pathname: '/a/foo.html' }, { pathname: '/a/foo' })).toBeTruthy()
20+
expect(isSameURL({ pathname: '/a/foo/' }, { pathname: '/a/foo' })).toBeTruthy()
21+
expect(isSameURL({ pathname: '/a/foo/index.html' }, { pathname: '/a/foo' })).toBeTruthy()
22+
23+
expect(isSameURL({ pathname: '/a/foo/index.html' }, { pathname: '/a/bar' })).toBeFalsy()
24+
})

templates/modern/src/helper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,17 @@ export function breakWordLit(text: string): TemplateResult {
7474
export function isExternalHref(url: URL): boolean {
7575
return url.hostname !== window.location.hostname || url.protocol !== window.location.protocol
7676
}
77+
78+
/**
79+
* Determines if two URLs should be considered the same.
80+
*/
81+
export function isSameURL(a: { pathname: string }, b: { pathname: string }): boolean {
82+
return normalizeUrlPath(a) === normalizeUrlPath(b)
83+
84+
function normalizeUrlPath(url: { pathname: string }): string {
85+
return url.pathname
86+
.replace(/\/index\.html$/gi, '/')
87+
.replace(/\.html$/gi, '')
88+
.replace(/\/$/gi, '')
89+
}
90+
}

templates/modern/src/toc.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { TemplateResult, html, render } from 'lit-html'
55
import { classMap } from 'lit-html/directives/class-map.js'
6-
import { breakWordLit, meta, isExternalHref, loc } from './helper'
6+
import { breakWordLit, meta, isExternalHref, loc, isSameURL } from './helper'
77

88
export type TocNode = {
99
name: string
@@ -66,7 +66,7 @@ export async function renderToc(): Promise<TocNode[]> {
6666
if (node.href) {
6767
const url = new URL(node.href, tocUrl)
6868
node.href = url.href
69-
active = isExternalHref(url) ? false : normalizeUrlPath(url) === normalizeUrlPath(window.location)
69+
active = isExternalHref(url) ? false : isSameURL(url, window.location)
7070
if (active) {
7171
if (node.items) {
7272
node.expanded = true
@@ -156,10 +156,6 @@ export async function renderToc(): Promise<TocNode[]> {
156156
function renderDownloadPdf(): TemplateResult {
157157
return pdf ? html`<div class="py-2 mb-md-4"><a class="pdf-link" href="${new URL(pdfFileName || 'toc.pdf', tocUrl)}">${loc('downloadPdf')}</a></div>` : null
158158
}
159-
160-
function normalizeUrlPath(url: { pathname: string }): string {
161-
return url.pathname.replace(/\/index\.html$/gi, '/')
162-
}
163159
}
164160

165161
function renderNextArticle(items: TocNode[], node: TocNode) {

0 commit comments

Comments
 (0)