-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
771ed1c
commit 113340b
Showing
7 changed files
with
150 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { MatchResultItem } from '../../logic/search'; | ||
|
||
export function SuggestionContent(props: { | ||
suggestion: MatchResultItem; | ||
query: string; | ||
isCurrent: boolean; | ||
}) { | ||
const { suggestion, query } = props; | ||
const renderHeaderMatch = () => { | ||
if (suggestion.type === 'header') { | ||
const { header, headerHighlightIndex } = suggestion; | ||
const headerPrefix = header.slice(0, headerHighlightIndex); | ||
const headerSuffix = header.slice(headerHighlightIndex + query.length); | ||
return ( | ||
<div font="medium"> | ||
<span>{headerPrefix}</span> | ||
<span bg="brand-light" p="y-0.4 x-0.8" rounded="md" text="text-1"> | ||
{query} | ||
</span> | ||
<span>{headerSuffix}</span> | ||
</div> | ||
); | ||
} else { | ||
return <div font="medium">{suggestion.header}</div>; | ||
} | ||
}; | ||
const renderStatementMatch = () => { | ||
if (suggestion.type !== 'content') { | ||
return; | ||
} | ||
const { statementHighlightIndex, statement } = suggestion; | ||
const statementPrefix = statement.slice(0, statementHighlightIndex); | ||
const statementSuffix = statement.slice( | ||
statementHighlightIndex + query.length | ||
); | ||
return ( | ||
<div font="normal" text="sm gray-light" w="100%"> | ||
<span>{statementPrefix}</span> | ||
<span bg="brand-light" p="y-0.4 x-0.8" rounded="md" text="[#000]"> | ||
{query} | ||
</span> | ||
<span>{statementSuffix}</span> | ||
</div> | ||
); | ||
}; | ||
return ( | ||
<div | ||
border-1="" | ||
table-cell="" | ||
p="x-3 y-2" | ||
hover="bg-[#f3f4f5]" | ||
className={`border-right-none ${props.isCurrent ? 'bg-[#f3f4f5]' : ''}`} | ||
transition="bg duration-200" | ||
> | ||
<div font="medium" text="sm"> | ||
{renderHeaderMatch()} | ||
</div> | ||
{suggestion.type === 'content' && renderStatementMatch()} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,8 @@ | ||
export const isProduction = () => import.meta.env.PROD; | ||
|
||
export function addLeadingSlash(url: string) { | ||
return url.charAt(0) === '/' ? url : '/' + url; | ||
} | ||
|
||
export function removeTrailingSlash(url: string) { | ||
return url.charAt(url.length - 1) === '/' ? url.slice(0, -1) : url; | ||
} | ||
|
||
export function normalizeHref(url?: string) { | ||
if (!url) { | ||
return '/'; | ||
} | ||
if (!isProduction() || url.startsWith('http')) { | ||
return url; | ||
} | ||
|
||
let suffix = ''; | ||
if (!import.meta.env.ENABLE_SPA) { | ||
suffix += '.html'; | ||
if (url.endsWith('/')) { | ||
suffix = 'index' + suffix; | ||
} | ||
} | ||
return addLeadingSlash(`${url}${suffix}`); | ||
} | ||
|
||
export { usePrevNextPage } from './usePrevNextPage'; | ||
export { useEditLink } from './useEditLink'; | ||
export { useSidebarData } from './useSidebarData'; | ||
export { useLocaleSiteData } from './useLocaleSiteData'; | ||
export { setupEffects, bindingAsideScroll } from './sideEffects'; | ||
export { setupCopyCodeButton } from './copyCode'; | ||
export { PageSearcher } from './search'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test, describe } from 'vitest'; | ||
import { Header } from 'shared/types'; | ||
import { backTrackHeaders } from './utils'; | ||
|
||
describe('utils logic', () => { | ||
test('back track the headers', () => { | ||
const headers: Header[] = [ | ||
{ depth: 1, text: '1', id: '1' }, | ||
{ depth: 2, text: '2', id: '2' }, | ||
{ depth: 3, text: '3', id: '3' }, | ||
{ depth: 4, text: '4', id: '4' }, | ||
{ depth: 5, text: '5', id: '5' } | ||
]; | ||
const res = backTrackHeaders(headers, 3); | ||
expect(res).toEqual([ | ||
{ depth: 2, text: '2', id: '2' }, | ||
{ depth: 3, text: '3', id: '3' }, | ||
{ depth: 4, text: '4', id: '4' } | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Header } from 'shared/types'; | ||
|
||
export const isProduction = () => import.meta.env.PROD; | ||
|
||
export function addLeadingSlash(url: string) { | ||
return url.charAt(0) === '/' ? url : '/' + url; | ||
} | ||
|
||
export function removeTrailingSlash(url: string) { | ||
return url.charAt(url.length - 1) === '/' ? url.slice(0, -1) : url; | ||
} | ||
|
||
export function normalizeHref(url?: string) { | ||
if (!url) { | ||
return '/'; | ||
} | ||
if (!isProduction() || url.startsWith('http')) { | ||
return url; | ||
} | ||
|
||
let suffix = ''; | ||
if (!import.meta.env.ENABLE_SPA) { | ||
suffix += '.html'; | ||
if (url.endsWith('/')) { | ||
suffix = 'index' + suffix; | ||
} | ||
} | ||
return addLeadingSlash(`${url}${suffix}`); | ||
} | ||
|
||
export function backTrackHeaders( | ||
rawHeaders: Header[], | ||
index: number | ||
): Header[] { | ||
let current = rawHeaders[index]; | ||
let currentIndex = index; | ||
|
||
const res: Header[] = [current]; | ||
while (current && current.depth > 2) { | ||
for (let i = currentIndex - 1; i >= 0; i--) { | ||
const header = rawHeaders[i]; | ||
if (header.depth > 1 && header.depth === current.depth - 1) { | ||
current = header; | ||
currentIndex = i; | ||
res.unshift(current); | ||
break; | ||
} | ||
} | ||
} | ||
return res; | ||
} |