Browse Source

feature/1001636

lizi 2 years ago
parent
commit
08b6890f5a

+ 1 - 0
src/types/search.types.ts

@@ -8,6 +8,7 @@ export interface SearchResultDoc {
   unit_scholar: {scholar_name: string, unit_name: string}[];
   cited?: number;
   year?: number;
+  highlight?: Map<String, String>;
 }
 
 export interface SearchResult {

+ 4 - 1
src/views/search/SearchResultView.vue

@@ -340,13 +340,16 @@ onUnmounted(() => {
 <style scoped lang="scss">
 .search-result-wrap {
   margin-top: 20px;
-
   .search-box-wrap {
     padding: 0 120px;
   }
 
   .search-result-main {
     margin-top: 20px;
+    :deep em {
+      color: red;
+      font-style: normal;
+    }
   }
 
   @media (max-width: 1024px) {

+ 25 - 6
src/views/search/components/SearchResultItem.vue

@@ -1,9 +1,10 @@
 <script setup lang="ts">
-import type { PropType } from "vue";
+// import type { PropType } from "vue";
 import type { SearchResultDoc } from "@/types/search.types";
 import type { ResultMode } from "@/types/search.types";
+import { type PropType, onMounted } from "vue"
 
-defineProps({
+const props = defineProps({
   data: {
     type: Object as PropType<SearchResultDoc>,
     required: true,
@@ -21,6 +22,24 @@ function getAuthorAndUnit(scholar_name: string, unit_name: string) {
   }
   return str
 }
+
+function highlight(name: string, content: string){
+  if (props.data.highlight) {
+    let highlight = JSON.parse(JSON.stringify(props.data.highlight));
+    let reg = new RegExp('<em>(.*?)<\\/em>', 'g')
+    let hlTitles: string[] = []
+    let hlTitleOrigins: string[] = []
+    console.log(highlight,"highlight")
+    if (highlight) {
+      hlTitles = highlight[name] || highlight[name+'.exact']
+      for (let index in hlTitles) {
+        hlTitleOrigins[index] = hlTitles[index].replace(reg, '$1')
+        content = content.replace(hlTitleOrigins[index], hlTitles[index])
+      }
+    }
+  }
+  return content;
+}
 </script>
 
 <template>
@@ -37,23 +56,23 @@ function getAuthorAndUnit(scholar_name: string, unit_name: string) {
           {{ getAuthorAndUnit(item.scholar_name, item.unit_name) }}
         </span>
       </div>
-      <div v-if="data.keywords.length > 0">关键词:{{ data.keywords.join(",") }}</div>
+      <div v-if="data.keywords.length > 0">关键词:<span v-html="highlight('keywords', data.keywords.join(','))"></span></div>
       <div>
         <span>时间:{{ data.year }}</span>
         <span>,被引量:{{ data.cited }}</span>
       </div>
     </div>
-    <p v-if="mode != 'brief'">摘要:<span v-html="data.abstract.replace('摘要:', '')"></span></p>
+    <p v-if="mode != 'brief'">摘要:<span v-html="highlight('abstract', data.abstract.replace('摘要:', ''))"></span></p>
   </div>
 </template>
 
 <style scoped lang="scss">
+
 .item-wrap {
   padding: 5px;
-
   .metadata {
     padding-bottom: 5px;
-
+    
     .author-item {
       margin-left: 1em;