Skip to content

Commit

Permalink
chore: move check to helper & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Aug 3, 2023
1 parent 56c621c commit 1c1b72f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions samples/seed/articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
href: csharp_coding_standards.md
- name: Markdown
href: markdown.md
- name: Microsoft Docs
href: https://docs.microsoft.com/en-us/
9 changes: 9 additions & 0 deletions templates/modern/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ export function breakWordLit(text: string): TemplateResult {
})
return html`${result}`
}

/**
* Check if the url is external.
* @param url The url to check.
* @returns True if the url is external.
*/
export function isExternalHref(url: URL): boolean {
return url.hostname !== window.location.hostname || url.protocol !== window.location.protocol
}
4 changes: 2 additions & 2 deletions templates/modern/src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

import { render, html, TemplateResult } from 'lit-html'
import { breakWordLit, meta } from './helper'
import { breakWordLit, meta, isExternalHref } from './helper'
import { themePicker } from './theme'
import { TocNode } from './toc'

Expand Down Expand Up @@ -130,7 +130,7 @@ function findActiveItem(items: (NavItem | NavItemContainer)[]): NavItem {
let activeItem: NavItem
let maxPrefix = 0
for (const item of items.map(i => 'items' in i ? i.items : i).flat()) {
if (item.href.hostname !== window.location.hostname || item.href.port !== window.location.port) {
if (isExternalHref(item.href)) {
continue
}
const prefix = commonUrlPrefix(url, item.href)
Expand Down
8 changes: 2 additions & 6 deletions templates/modern/src/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { TemplateResult, html, render } from 'lit-html'
import { classMap } from 'lit-html/directives/class-map.js'
import { breakWordLit, meta } from './helper'
import { breakWordLit, meta, isExternalHref } from './helper'

export type TocNode = {
name: string
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function renderToc(): Promise<TocNode[]> {
if (node.href) {
const url = new URL(node.href, tocUrl)
node.href = url.href
active = isExternal(url) ? false : normalizeUrlPath(url) === normalizeUrlPath(window.location)
active = isExternalHref(url) ? false : normalizeUrlPath(url) === normalizeUrlPath(window.location)
if (active) {
if (node.items) {
node.expanded = true
Expand All @@ -79,10 +79,6 @@ export async function renderToc(): Promise<TocNode[]> {
return false
}

function isExternal(url: URL): boolean {
return url.hostname !== window.location.hostname || url.port !== window.location.port
}

function renderToc() {
render(html`${renderTocFilter()} ${renderTocNodes(items) || renderNoFilterResult()}`, tocContainer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
"topicHref": null,
"tocHref": null,
"level": 2.0
},
{
"name": "Microsoft Docs",
"href": "https://docs.microsoft.com/en-us/",
"topicHref": "https://docs.microsoft.com/en-us/",
"tocHref": null,
"level": 2.0,
"items": [],
"leaf": true
}
],
"_appName": "Seed",
Expand All @@ -65,4 +74,4 @@
"leaf": false,
"title": "Table of Content",
"_disableToc": true
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"content": "{\"items\":[{\"name\":\"Home\",\"href\":\"index.html\",\"topicHref\":\"index.html\"},{\"name\":\"Articles\",\"href\":\"articles/docfx_getting_started.html\",\"tocHref\":\"articles/toc.html\",\"topicHref\":\"articles/docfx_getting_started.html\"},{\"name\":\"API Documentation\",\"items\":[{\"name\":\".NET API\",\"href\":\"api/BuildFromAssembly.html\",\"tocHref\":\"api/toc.html\",\"topicHref\":\"api/BuildFromAssembly.html\",\"topicUid\":\"BuildFromAssembly\"},{\"name\":\"REST API\",\"href\":\"restapi/petstore.html\",\"tocHref\":\"restapi/toc.html\",\"topicHref\":\"restapi/petstore.html\"}]}]}"
}
"content": "{\"items\":[{\"name\":\"Home\",\"href\":\"index.html\",\"topicHref\":\"index.html\"},{\"name\":\"Articles\",\"href\":\"articles/docfx_getting_started.html\",\"tocHref\":\"articles/toc.html\",\"topicHref\":\"articles/docfx_getting_started.html\"},{\"name\":\"API Documentation\",\"items\":[{\"name\":\".NET API\",\"href\":\"api/BuildFromAssembly.html\",\"tocHref\":\"api/toc.html\",\"topicHref\":\"api/BuildFromAssembly.html\",\"topicUid\":\"BuildFromAssembly\"},{\"name\":\"REST API\",\"href\":\"restapi/petstore.html\",\"tocHref\":\"restapi/toc.html\",\"topicHref\":\"restapi/petstore.html\"}]},{\"name\":\"Microsoft Docs\",\"href\":\"https://docs.microsoft.com/en-us/\",\"topicHref\":\"https://docs.microsoft.com/en-us/\"}]}"
}
7 changes: 6 additions & 1 deletion test/docfx.Snapshot.Tests/SamplesTest.Seed/toc.verified.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"topicHref": "restapi/petstore.html"
}
]
},
{
"name": "Microsoft Docs",
"href": "https://docs.microsoft.com/en-us/",
"topicHref": "https://docs.microsoft.com/en-us/"
}
]
}
}

0 comments on commit 1c1b72f

Please sign in to comment.