root-component-name.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // https://gitlab.com/antora/antora/-/issues/132#note_712132072
  2. 'use strict'
  3. const { posix: path } = require('path')
  4. module.exports.register = (pipeline, { config }) => {
  5. pipeline.on('contentClassified', ({ contentCatalog }) => {
  6. const rootComponentName = config.rootComponentName || 'ROOT'
  7. const rootComponentNameLength = rootComponentName.length
  8. contentCatalog.findBy({ component: rootComponentName }).forEach((file) => {
  9. if (file.out) {
  10. file.out.dirname = file.out.dirname.substr(rootComponentNameLength)
  11. file.out.path = file.out.path.substr(rootComponentNameLength + 1)
  12. file.out.rootPath = fixPath(file.out.rootPath)
  13. }
  14. if (file.pub) {
  15. file.pub.url = file.pub.url.substr(rootComponentNameLength + 1)
  16. if (file.pub.rootPath) {
  17. file.pub.rootPath = fixPath(file.pub.rootPath)
  18. }
  19. }
  20. if (file.rel) {
  21. if (file.rel.pub) {
  22. file.rel.pub.url = file.rel.pub.url.substr(rootComponentNameLength + 1)
  23. file.rel.pub.rootPath = fixPath(file.rel.pub.rootPath);
  24. }
  25. }
  26. })
  27. const rootComponent = contentCatalog.getComponent(rootComponentName)
  28. rootComponent?.versions?.forEach((version) => {
  29. version.url = version.url.substr(rootComponentName.length + 1)
  30. })
  31. // const siteStartPage = contentCatalog.getById({ component: '', version: '', module: '', family: 'alias', relative: 'index.adoc' })
  32. // if (siteStartPage) delete siteStartPage.out
  33. })
  34. function fixPath(path) {
  35. return path.split('/').slice(1).join('/') || '.'
  36. }
  37. }