Commit d42740c034
Unsigned
Layout: unified · split
docs/.vitepress/plugins/urlSearchPlugin.ts +44 −2
| @@ -15,6 +15,11 @@ function extractLinksFromMarkdown(src: string, pageId: string): PageLink[] { | |||
| 15 | const seen = new Set<string>() | 15 | const seen = new Set<string>() |
| 16 | const headingStack: string[] = [] | 16 | const headingStack: string[] = [] |
| 17 | const slugCounts = new Map<string, number>() | 17 | const slugCounts = new Map<string, number>() |
| 18 | const containerStack: string[] = [] | ||
| 19 | const tabStack: { | ||
| 20 | title: string | ||
| 21 | headings: string[] | ||
| 22 | }[] = [] | ||
| 18 | const collapsibleStack: { | 23 | const collapsibleStack: { |
| 19 | anchor: string | 24 | anchor: string |
| 20 | headings: string[] | 25 | headings: string[] |
| @@ -54,9 +59,46 @@ function extractLinksFromMarkdown(src: string, pageId: string): PageLink[] { | |||
| 54 | currentAnchor = getAnchor(text) | 59 | currentAnchor = getAnchor(text) |
| 55 | } | 60 | } |
| 56 | 61 | ||
| 62 | const getActiveTab = () => | ||
| 63 | tabStack.length ? tabStack[tabStack.length - 1] : undefined | ||
| 64 | |||
| 65 | const getTitles = () => { | ||
| 66 | const tab = getActiveTab() | ||
| 67 | if (!tab) return headingStack.filter(Boolean) | ||
| 68 | |||
| 69 | const baseHeadings = tab.headings.filter(Boolean) | ||
| 70 | const nestedHeadings = headingStack | ||
| 71 | .slice(tab.headings.length) | ||
| 72 | .filter(Boolean) | ||
| 73 | return [...baseHeadings, tab.title, ...nestedHeadings] | ||
| 74 | } | ||
| 75 | |||
| 57 | for (let i = 0; i < lines.length; i++) { | 76 | for (let i = 0; i < lines.length; i++) { |
| 58 | const line = lines[i] | 77 | const line = lines[i] |
| 59 | 78 | ||
| 79 | const containerOpenMatch = line.match(/^\s*:{3,}\s*([A-Za-z][\w-]*)\b/) | ||
| 80 | if (containerOpenMatch) { | ||
| 81 | containerStack.push(containerOpenMatch[1].toLowerCase()) | ||
| 82 | } else if (/^\s*:{3,}\s*$/.test(line)) { | ||
| 83 | const closed = containerStack.pop() | ||
| 84 | if (closed === 'tabs') tabStack.pop() | ||
| 85 | } | ||
| 86 | |||
| 87 | const tabMatch = line.match(/^\s*==\s+(.+)$/) | ||
| 88 | if (tabMatch && containerStack.includes('tabs')) { | ||
| 89 | const title = tabMatch[1].trim() | ||
| 90 | if (title) { | ||
| 91 | const tab = getActiveTab() | ||
| 92 | if (tab) { | ||
| 93 | tab.title = title | ||
| 94 | tab.headings = [...headingStack] | ||
| 95 | } else { | ||
| 96 | tabStack.push({ title, headings: [...headingStack] }) | ||
| 97 | } | ||
| 98 | } | ||
| 99 | continue | ||
| 100 | } | ||
| 101 | |||
| 60 | const collapsibleMatch = line.match( | 102 | const collapsibleMatch = line.match( |
| 61 | /^\s*<Collapsible\b(?=[^>]*\btitle=(['"])(.*?)\1)[^>]*>\s*$/i | 103 | /^\s*<Collapsible\b(?=[^>]*\btitle=(['"])(.*?)\1)[^>]*>\s*$/i |
| 62 | ) | 104 | ) |
| @@ -113,7 +155,7 @@ function extractLinksFromMarkdown(src: string, pageId: string): PageLink[] { | |||
| 113 | linkText, | 155 | linkText, |
| 114 | pageId, | 156 | pageId, |
| 115 | anchor: currentAnchor, | 157 | anchor: currentAnchor, |
| 116 | titles: headingStack.filter(Boolean) | 158 | titles: getTitles() |
| 117 | }) | 159 | }) |
| 118 | } | 160 | } |
| 119 | 161 | ||
| @@ -129,7 +171,7 @@ function extractLinksFromMarkdown(src: string, pageId: string): PageLink[] { | |||
| 129 | linkText: href, | 171 | linkText: href, |
| 130 | pageId, | 172 | pageId, |
| 131 | anchor: currentAnchor, | 173 | anchor: currentAnchor, |
| 132 | titles: headingStack.filter(Boolean) | 174 | titles: getTitles() |
| 133 | }) | 175 | }) |
| 134 | } | 176 | } |
| 135 | } | 177 | } |
docs/.vitepress/theme/components/Search.vue +4 −2
| @@ -1936,7 +1936,8 @@ function onMouseMove(e: MouseEvent) { | |||
| 1936 | <div class="url-result-body"> | 1936 | <div class="url-result-body"> |
| 1937 | <div class="url-path"> | 1937 | <div class="url-path"> |
| 1938 | <Hash | 1938 | <Hash |
| 1939 | v-if="item.titles.length > 0" | 1939 | v-if="item.anchor || |
| 1940 | item.titles.length > 0" | ||
| 1940 | stroke-width="1.25" | 1941 | stroke-width="1.25" |
| 1941 | :size="18" | 1942 | :size="18" |
| 1942 | /> | 1943 | /> |
| @@ -2006,7 +2007,8 @@ function onMouseMove(e: MouseEvent) { | |||
| 2006 | <div> | 2007 | <div> |
| 2007 | <div class="titles"> | 2008 | <div class="titles"> |
| 2008 | <Hash | 2009 | <Hash |
| 2009 | v-if="p.titles.length > 0" | 2010 | v-if="getDocAnchor(p.id) || |
| 2011 | p.titles.length > 0" | ||
| 2010 | stroke-width="1.25" | 2012 | stroke-width="1.25" |
| 2011 | :size="18" | 2013 | :size="18" |
| 2012 | /> | 2014 | /> |
docs/.vitepress/theme/workers/searchWorker.ts +57 −2
| @@ -116,6 +116,59 @@ function compareByPageAndDoc(a: { id: unknown }, b: { id: unknown }) { | |||
| 116 | return getDocOrder(aId) - getDocOrder(bId) | 116 | return getDocOrder(aId) - getDocOrder(bId) |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | function stripMarkdown(value: string) { | ||
| 120 | return value | ||
| 121 | .replace(/`([^`]+)`/g, '$1') | ||
| 122 | .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1') | ||
| 123 | .replace(/[*_~]/g, '') | ||
| 124 | .trim() | ||
| 125 | } | ||
| 126 | |||
| 127 | function getQueryTerms(query: string) { | ||
| 128 | return query | ||
| 129 | .toLowerCase() | ||
| 130 | .split(/\s+/) | ||
| 131 | .map((term) => term.trim()) | ||
| 132 | .filter(Boolean) | ||
| 133 | } | ||
| 134 | |||
| 135 | function findMatchingLinkForResult( | ||
| 136 | result: WorkerSearchResult, | ||
| 137 | query: string | ||
| 138 | ) { | ||
| 139 | const pageId = getPageKey(String(result.id)) | ||
| 140 | const anchor = String(result.id).split('#')[1] ?? '' | ||
| 141 | const terms = getQueryTerms(query) | ||
| 142 | if (!terms.length) return | ||
| 143 | |||
| 144 | return urlLinks.find((link) => { | ||
| 145 | if (link.pageId.replace(/\/$/, '') !== pageId || link.anchor !== anchor) { | ||
| 146 | return false | ||
| 147 | } | ||
| 148 | |||
| 149 | const linkText = stripMarkdown(link.linkText).toLowerCase() | ||
| 150 | return terms.every((term) => linkText.includes(term)) | ||
| 151 | }) | ||
| 152 | } | ||
| 153 | |||
| 154 | function withLinkPathTitles( | ||
| 155 | results: WorkerSearchResult[], | ||
| 156 | query: string | ||
| 157 | ) { | ||
| 158 | if (!urlLinks.length) return results | ||
| 159 | |||
| 160 | return results.map((result) => { | ||
| 161 | const link = findMatchingLinkForResult(result, query) | ||
| 162 | if (!link) return result | ||
| 163 | |||
| 164 | return { | ||
| 165 | ...result, | ||
| 166 | title: stripMarkdown(link.linkText), | ||
| 167 | titles: link.titles | ||
| 168 | } | ||
| 169 | }) | ||
| 170 | } | ||
| 171 | |||
| 119 | function rebuildDocOrder(index: MiniSearch<Result>) { | 172 | function rebuildDocOrder(index: MiniSearch<Result>) { |
| 120 | const map = new Map<string, number>() | 173 | const map = new Map<string, number>() |
| 121 | const ids = (index as any)._documentIds | 174 | const ids = (index as any)._documentIds |
| @@ -190,13 +243,15 @@ async function textSearch(message: TextSearchRequest) { | |||
| 190 | (r) => !seen.has(r.id) && r.match && Object.keys(r.match).length > 0 | 243 | (r) => !seen.has(r.id) && r.match && Object.keys(r.match).length > 0 |
| 191 | ) | 244 | ) |
| 192 | const results = [...top, ...titleHits].sort(compareByPageAndDoc) | 245 | const results = [...top, ...titleHits].sort(compareByPageAndDoc) |
| 246 | await ensureUrlLinks() | ||
| 247 | const displayResults = withLinkPathTitles(results, message.query) | ||
| 193 | const terms = new Set<string>() | 248 | const terms = new Set<string>() |
| 194 | for (const result of results) { | 249 | for (const result of displayResults) { |
| 195 | for (const term in result.match) terms.add(term) | 250 | for (const term in result.match) terms.add(term) |
| 196 | } | 251 | } |
| 197 | 252 | ||
| 198 | return { | 253 | return { |
| 199 | results, | 254 | results: displayResults, |
| 200 | terms: [...terms] | 255 | terms: [...terms] |
| 201 | } | 256 | } |
| 202 | } | 257 | } |