publish-docsearch-config.js 1.4 KB

12345678910111213141516171819202122232425262728
  1. 'use strict'
  2. const fsp = require('node:fs/promises')
  3. const ospath = require('node:path')
  4. /**
  5. * An Antora extension that generates the docsearch config file from a Handlebars template and publishes it with the
  6. * site, where the scraper job can retrieve it.
  7. */
  8. module.exports.register = function ({ config: { templatePath = './docsearch/config.json.hbs' } }) {
  9. const expandPath = this.require('@antora/expand-path-helper')
  10. const handlebars = this.require('handlebars').create()
  11. handlebars.registerHelper('eq', (a, b) => a === b)
  12. handlebars.registerHelper('and', (a, b) => a && b)
  13. this.on('beforePublish', async ({ playbook, contentCatalog, siteCatalog }) => {
  14. templatePath = expandPath(templatePath, { dot: playbook.dir })
  15. const templateSrc = await fsp.readFile(templatePath, 'utf8')
  16. const templateBasename = ospath.basename(templatePath)
  17. const template = handlebars.compile(templateSrc, { noEscape: true, preventIndent: true, srcName: templateBasename })
  18. const components = contentCatalog.getComponentsSortedBy('name').filter((component) => component.latest.version)
  19. const stopPages = contentCatalog.getPages((page) => {
  20. return page.out && ('page-archived' in page.asciidoc.attributes || 'page-noindex' in page.asciidoc.attributes)
  21. })
  22. const compiled = template({ components, site: playbook.site, stopPages })
  23. siteCatalog.addFile({ contents: Buffer.from(compiled), out: { path: 'docsearch-config.json' } })
  24. })
  25. }