Skip to content

Commit

Permalink
root-component-extension processes page-aliases
Browse files Browse the repository at this point in the history
Closes gh-19
  • Loading branch information
rwinch committed Jan 18, 2024
1 parent 4133891 commit aa8b104
Show file tree
Hide file tree
Showing 2 changed files with 288 additions and 39 deletions.
65 changes: 40 additions & 25 deletions lib/root-component-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,53 @@

module.exports.register = function ({ config = {} }) {
this.once('contentClassified', ({ contentCatalog }) => {
const rootComponentName = config.rootComponentName
if (!rootComponentName) {
throw new Error('Missing required configuration attribute root_component_name for root-component-extension')
}
const rootComponentName = getRootComponentName(config)
contentCatalog.findBy({ component: rootComponentName }).forEach((file) => {
if (file.out) {
file.out.rootPath = fixRootPathForRootComponentNameAndUrl(file.out.rootPath, rootComponentName, file.out.path)
file.out.dirname = removeRootComponentNameFromUrl(rootComponentName, file.out.dirname)
file.out.path = removeRootComponentNameFromUrl(rootComponentName, file.out.path)
}
if (file.pub) {
if (file.pub.rootPath) {
file.pub.rootPath = fixRootPathForRootComponentNameAndUrl(file.pub.rootPath, rootComponentName, file.pub.url)
}
file.pub.url = removeRootComponentNameFromUrl(rootComponentName, file.pub.url)
}
if (file.rel) {
if (file.rel.pub) {
file.rel.pub.rootPath = fixRootPathForRootComponentNameAndUrl(
file.rel.pub.rootPath,
rootComponentName,
file.rel.pub.url
)
file.rel.pub.url = removeRootComponentNameFromUrl(rootComponentName, file.rel.pub.url) || '/'
}
}
removeRootComponentNameFromFile(rootComponentName, file)
})
const rootComponent = contentCatalog.getComponent(rootComponentName)
rootComponent?.versions?.forEach((version) => {
version.url = removeRootComponentNameFromUrl(rootComponentName, version.url)
})
})
this.once('documentsConverted', ({ contentCatalog }) => {
const rootComponentName = getRootComponentName(config)
contentCatalog.findBy({ component: rootComponentName, family: 'alias' }).forEach((file) => {
removeRootComponentNameFromFile(rootComponentName, file)
})
})
}

function getRootComponentName (config) {
const rootComponentName = config.rootComponentName
if (!rootComponentName) {
throw new Error('Missing required configuration attribute root_component_name for root-component-extension')
}
return rootComponentName
}

function removeRootComponentNameFromFile (rootComponentName, file) {
if (file.out) {
file.out.rootPath = fixRootPathForRootComponentNameAndUrl(file.out.rootPath, rootComponentName, file.out.path)
file.out.dirname = removeRootComponentNameFromUrl(rootComponentName, file.out.dirname)
file.out.path = removeRootComponentNameFromUrl(rootComponentName, file.out.path)
}
if (file.pub) {
if (file.pub.rootPath) {
file.pub.rootPath = fixRootPathForRootComponentNameAndUrl(file.pub.rootPath, rootComponentName, file.pub.url)
}
file.pub.url = removeRootComponentNameFromUrl(rootComponentName, file.pub.url)
}
if (file.rel) {
if (file.rel.pub) {
file.rel.pub.rootPath = fixRootPathForRootComponentNameAndUrl(
file.rel.pub.rootPath,
rootComponentName,
file.rel.pub.url
)
file.rel.pub.url = removeRootComponentNameFromUrl(rootComponentName, file.rel.pub.url) || '/'
}
}
}

function urlStartsWithRootComponentName (url, rootComponentName) {
Expand Down
Loading

0 comments on commit aa8b104

Please sign in to comment.