Skip to content

Commit 96128e2

Browse files
authored
[GH-2791] Add platform tags to docs search results (#2792)
1 parent 82feadf commit 96128e2

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const PLATFORM_RULES = [
2+
{pattern: /\/api\/flink\//, label: 'SedonaFlink', cls: 'flink'},
3+
{pattern: /\/api\/snowflake\//, label: 'SedonaSnow', cls: 'snow'},
4+
{pattern: /\/setup\/flink\//, label: 'SedonaFlink', cls: 'flink'},
5+
{pattern: /\/setup\/snowflake\//, label: 'SedonaSnow', cls: 'snow'},
6+
{pattern: /\/tutorial\/flink\//, label: 'SedonaFlink', cls: 'flink'},
7+
{pattern: /\/tutorial\/snowflake\//, label: 'SedonaSnow', cls: 'snow'},
8+
{pattern: /sedonaflink\/?$/, label: 'SedonaFlink', cls: 'flink'},
9+
{pattern: /sedonasnow\/?$/, label: 'SedonaSnow', cls: 'snow'},
10+
{pattern: /\/api\/sql\//, label: 'SedonaSpark', cls: 'spark'},
11+
{pattern: /\/api\/stats\//, label: 'SedonaSpark', cls: 'spark'},
12+
{pattern: /\/api\/viz\//, label: 'SedonaSpark', cls: 'spark'},
13+
{pattern: /\/setup\/(?!flink|snowflake)/, label: 'SedonaSpark', cls: 'spark'},
14+
{
15+
pattern: /\/tutorial\/(?!flink|snowflake)/,
16+
label: 'SedonaSpark',
17+
cls: 'spark',
18+
},
19+
{pattern: /sedonaspark\/?$/, label: 'SedonaSpark', cls: 'spark'},
20+
];
21+
22+
function getPlatform(href) {
23+
for (const rule of PLATFORM_RULES) {
24+
if (rule.pattern.test(href)) {
25+
return rule;
26+
}
27+
}
28+
return null;
29+
}
30+
31+
function tagResult(item) {
32+
if (item.querySelector('.search-tag')) return;
33+
34+
const link = item.querySelector('a');
35+
if (!link) return;
36+
37+
const platform = getPlatform(link.getAttribute('href') || '');
38+
if (!platform) return;
39+
40+
const tag = document.createElement('span');
41+
tag.className = `search-tag search-tag--${platform.cls}`;
42+
tag.textContent = platform.label;
43+
44+
const title = link.querySelector('h1, h2');
45+
if (title) {
46+
title.appendChild(tag);
47+
}
48+
}
49+
50+
function handleMutations(mutationsList) {
51+
for (const mutation of mutationsList) {
52+
for (const node of mutation.addedNodes) {
53+
if (!(node instanceof HTMLElement)) continue;
54+
55+
if (node.matches('.md-search-result__item')) {
56+
tagResult(node);
57+
continue;
58+
}
59+
60+
const items = node.querySelectorAll
61+
? node.querySelectorAll('.md-search-result__item')
62+
: [];
63+
items.forEach(tagResult);
64+
}
65+
}
66+
}
67+
68+
export const searchTags = () => {
69+
const resultList = document.querySelector('.md-search-result__list');
70+
if (!resultList) return;
71+
72+
resultList.querySelectorAll('.md-search-result__item').forEach(tagResult);
73+
74+
const observer = new MutationObserver(handleMutations);
75+
observer.observe(resultList, {childList: true, subtree: true});
76+
};

docs-overrides/assets/javascripts/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {industriesTabs} from './components/industries-tabs';
22
import {codeTabs} from './components/code-tabs';
33
import {typedAnimation} from './components/typed-animation';
4+
import {searchTags} from './components/search-tags';
45

56
document.addEventListener('DOMContentLoaded', () => {
67
industriesTabs();
78
codeTabs();
89
typedAnimation();
10+
searchTags();
911
});
1012

1113
document$.subscribe(function () {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.search-tag {
2+
display: inline-block;
3+
margin-left: 0.5em;
4+
padding: 0.1em 0.5em;
5+
border-radius: 3px;
6+
font-size: 0.65em;
7+
font-weight: 700;
8+
line-height: 1.6;
9+
vertical-align: middle;
10+
white-space: nowrap;
11+
letter-spacing: 0.02em;
12+
13+
&--spark {
14+
background-color: #fff3e0;
15+
color: #e65100;
16+
}
17+
18+
&--flink {
19+
background-color: #e3f2fd;
20+
color: #1565c0;
21+
}
22+
23+
&--snow {
24+
background-color: #e0f7fa;
25+
color: #00695c;
26+
}
27+
}

docs-overrides/assets/stylesheets/extra.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@import "components/buttons";
1313
@import "components/footer";
1414
@import "components/sidebar";
15+
@import "components/search-tags";
1516

1617
// Pages
1718
@import "pages/page-home";

0 commit comments

Comments
 (0)