UNPKG

@gouvfr/dsfr-nexus

Version:

Le module `dsfr-nexus` est l'interface de ligne de commande (CLI) centrale du Système de Design de l’État - DSFR. Il offre des outils pour gérer et compiler les ressources du DSFR

61 lines (54 loc) 1.79 kB
import { DocumentCollection } from './document-collection.js'; import MiniSearch from 'minisearch'; import { createJSONFile } from '@gouvfr/dsfr-forge'; import { normalizeTerm } from '@gouvfr/dsfr-kit'; class SearchPublisher { constructor (state) { this._state = state; this._locales = state.i18n.locales; this._collections = new Map(); for (const locale of this._locales) this._collections.set(locale.code, new DocumentCollection(locale)); } add (collections) { for (const collection of collections) { const locale = collection.locale; this._collections.get(locale.code).add(...collection.documents); } } async write () { for (const collection of this._collections.values()) { collection.index(); await this.createIndex( collection.documents, this._state.getUrl('', collection.locale), { fields: ['title', 'keywords', 'summary', 'text'], storeFields: ['title', 'url', 'boost'], processTerm: (term) => normalizeTerm(term.toLowerCase()), } ); await this.createIndex( collection.documents, this._state.getUrl('search', collection.locale), { fields: ['title', 'keywords', 'summary', 'text'], storeFields: [ 'title', 'excerpt', 'cover', 'section', 'url', 'boost', 'badge', ], processTerm: (term) => normalizeTerm(term.toLowerCase()), } ); } } async createIndex (documents, dest, options) { const miniSearch = new MiniSearch(options); miniSearch.addAll(documents); createJSONFile(`${this._state.dest}${dest}/index.json`, miniSearch.toJSON()); } } export { SearchPublisher };