vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.45k stars 2.03k forks source link

Feature request: Add hook for transforming minisearch index data #2454

Open GulomovCreative opened 1 year ago

GulomovCreative commented 1 year ago

Is your feature request related to a problem? Please describe.

At this moment, we have no way to somehow influence the fields being added to the local search index, except to write new solution. It would be more convenient if it was possible to change and/or add fields in a hook similar to transformData

Describe the solution you'd like

export default {
  transformPageIndex(index, { documentId }) {
    index.newField = 'someValue'
    index.category = getCategory(documentId)
  }

  // or

  transformPageIndex(index, { documentId }) {
    return {
      newField: 'someValue',
      category: getCategory(documentId)
    }
  }
}

Describe alternatives you've considered

No response

Additional context

No response

Validations

brc-dd commented 1 year ago

Should be possible using something like this (ignore ts errors, if any):

import { defineConfig } from 'vitepress'

export default defineConfig({
  themeConfig: {
    search: {
      provider: 'local',
      options: {
        miniSearch: {
          options: {
            fields: ['title', 'titles', 'text', 'category'],
            storeFields: ['title', 'titles', 'category'],
            extractField(document, fieldName) {
              if (fieldName === 'category') return getCategory(document)
              return document[fieldName]
            }
          },
          searchOptions: {
            /* ... */
          }
        }
      }
    }
  }
})