55 id =" algolia-search-input"
66 :placeholder =" placeholder"
77 :class =" activeSearchClass"
8- @keypress.enter.prevent =" $emit('openDrawer') "
8+ @keypress.enter.prevent =" performSearch "
99 />
1010 <div :class =" activeSearchIconClass" >
11- <img @click =" $emit('openDrawer') " alt =" search icon" :src =" withBase(activeSearchIcon)" />
11+ <img @click =" performSearch " alt =" search icon" :src =" withBase(activeSearchIcon)" />
1212 </div >
1313 </form >
1414</template >
1515
1616<script setup>
1717import {usePageFrontmatter , withBase } from " @vuepress/client" ;
18- import {computed , inject , watch } from " vue" ;
19- const { MAX_ALGOLIA_HITS_PER_PAGE } = inject (' themeConfig' )
20-
18+ import {computed , inject , ref , watch } from " vue" ;
2119
20+ const { MAX_HITS_PER_PAGE } = inject (' themeConfig' )
2221const {headerDefaultSearchIcon , headerSearchIcon , headerSearchPlaceholder } = inject (' themeConfig' )
22+
2323const props = defineProps ({
2424 options: {
2525 type: [Object , Array ],
@@ -37,9 +37,10 @@ const props = defineProps({
3737 type: Boolean ,
3838 }
3939});
40- const emit = defineEmits ([" openDrawer" , ' update:modelValue' , ' result' ])
4140
41+ const emit = defineEmits ([" openDrawer" , ' update:modelValue' , ' result' ])
4242const frontmatter = usePageFrontmatter ()
43+
4344const isGlobalLayout = computed (() => {
4445 return frontmatter .value .layout === ' HomeLayout'
4546})
@@ -61,43 +62,107 @@ const placeholderDesktop = computed(() => {
6162})
6263
6364const placeholder = computed (() => {
64- return props .isMobileWidth ? ' Search accross all Imunify Security support ' : placeholderDesktop .value
65+ return props .isMobileWidth ? ' Search accross all CloudLinux Docs ' : placeholderDesktop .value
6566})
6667
67-
68- const initialize = async (userOptions ) => {
69- if ( typeof window === ' undefined' ) return
70- const [docsearchModule ] = await Promise .all ([
71- import (/* webpackChunkName: "docsearch" */ " docsearch.js/dist/cdn/docsearch.min.js" ),
72- import (/* webpackChunkName: "docsearch" */ " docsearch.js/dist/cdn/docsearch.min.css" ),
73- ]);
74- const docsearch = docsearchModule .default ;
75- docsearch (
76- Object .assign ({}, userOptions, {
77- inputSelector: " #algolia-search-input" ,
78- algoliaOptions: {
79- hitsPerPage: MAX_ALGOLIA_HITS_PER_PAGE ,
80- },
81- handleSelected : () => {
82- emit (' openDrawer' )
83- },
84- transformData : (hits ) => {
85- emit (' result' , hits)
68+ function parseDocs (api_response ) {
69+ return api_response .cloudlinux_docs .map ((doc ) => {
70+ const titleParts = doc .title .split (" ->" ).map ((part ) => part .trim ());
71+
72+ const hierarchy = {
73+ lvl0: titleParts[0 ] || null ,
74+ lvl1: titleParts[1 ] || null ,
75+ lvl2: titleParts[2 ] || null ,
76+ lvl3: titleParts[3 ] || null ,
77+ lvl4: titleParts[4 ] || null ,
78+ lvl5: null ,
79+ lvl6: null ,
80+ };
81+
82+ const anchor = doc .url .split (" #" )[1 ] || " " ;
83+
84+ const objectID = doc .id ;
85+
86+ return {
87+ anchor,
88+ content: null ,
89+ hierarchy,
90+ url: doc .url ,
91+ title: doc .title ,
92+ preview: doc .preview ,
93+ category: doc .category ,
94+ section: doc .section ,
95+ objectID,
96+ _highlightResult: {
97+ hierarchy: {
98+ lvl0: {
99+ value: hierarchy .lvl0 || " " ,
100+ matchLevel: " none" ,
101+ matchedWords: [],
102+ },
103+ lvl1: {
104+ value: hierarchy .lvl1 || " " ,
105+ matchLevel: " full" ,
106+ fullyHighlighted: false ,
107+ matchedWords: [hierarchy .lvl1 ? .toLowerCase ()],
108+ },
86109 },
87- })
88- );
89- };
90- watch (
91- () => props .options ,
92- async (newValue ) => {
93- await initialize (newValue);
94- }, {
95- immediate: true
110+ hierarchy_camel: [
111+ {
112+ lvl0: {
113+ value: hierarchy .lvl0 || " " ,
114+ matchLevel: " none" ,
115+ matchedWords: [],
116+ },
117+ lvl1: {
118+ value: ` <span class="algolia-docsearch-suggestion--highlight">${ hierarchy .lvl1 || " " } </span>` ,
119+ matchLevel: " full" ,
120+ fullyHighlighted: false ,
121+ matchedWords: [hierarchy .lvl1 ? .toLowerCase ()],
122+ },
123+ },
124+ ],
125+ },
126+ };
127+ });
128+ }
129+
130+ async function queryGlobalSearch (query , n_results = 10 ) {
131+ const baseUrl = ' https://global-search.cl-edu.com/search' ;
132+ let url = ` ${ baseUrl} ?query=${ query} &collections=cloudlinux_docs&n_results=${ n_results} &source=cloudlinux_docs` ;
133+ try {
134+ const response = await fetch (url);
135+ if (! response .ok ) {
136+ throw new Error (` HTTP error! status: ${ response .status } ` );
96137 }
138+ const data = await response .json ();
139+ return data;
140+ } catch (error) {
141+ console .error (' Error querying global search:' , error);
142+ return null ;
143+ }
144+ }
145+
146+ const performSearch = async () => {
147+ const data = await queryGlobalSearch (props .modelValue , MAX_HITS_PER_PAGE );
148+ if (data) {
149+ const hits = parseDocs (data);
150+ console .log (hits); // Log the results data structure
151+ emit (' result' , hits);
152+ emit (' openDrawer' );
153+ }
154+ }
155+
156+ watch (
157+ () => props .options ,
158+ async (newValue ) => {
159+ // Initialize if needed or any other dependent setup
160+ }, {
161+ immediate: true
162+ }
97163);
98164< / script>
99165
100-
101166< style lang= " stylus" >
102167@import ' ../../styles/config.styl'
103168.algolia - autocomplete
@@ -139,7 +204,6 @@ watch(
139204 justify- content center
140205 align- content center
141206
142-
143207@media (max- width: $mobileBreakpoint)
144208 .drawer - header__search
145209 width 100 %
@@ -158,4 +222,4 @@ watch(
158222 width 75 %
159223 .header - layout__search- title
160224 text- align center
161- </style >
225+ < / style>
0 commit comments