@antora/assembler
Version:
A JavaScript library that merges AsciiDoc content from multiple pages in an Antora site into assembly files and delegates to an exporter to convert those files to another format, such as PDF.
58 lines (53 loc) • 2.31 kB
JavaScript
const { posix: path } = require('node:path')
function computeOut (src) {
const { component, version, module: module_ = 'ROOT', family, relative } = src
const outRelative = family === 'page' ? relative.replace(/\.adoc$/, '.html') : relative
const { dir: dirname, base: basename } = path.parse(outRelative)
const componentVersion = this.getComponentVersion(component, version)
const versionSegment =
'activeVersionSegment' in componentVersion
? componentVersion.activeVersionSegment
: resolveActiveVersionSegment.call(this, component, version)
const outDirSegments = []
const moduleRootPathSegments = []
if (component !== 'ROOT') outDirSegments.push(component)
if (versionSegment) outDirSegments.push(versionSegment)
if (module_ !== 'ROOT') outDirSegments.push(module_)
const outModuleDirSegments = outDirSegments.slice()
if (family !== 'page') {
outDirSegments.push(`_${family}s`)
moduleRootPathSegments.push('..')
}
if (dirname) {
outDirSegments.push(dirname)
for (const _ of dirname.split('/')) moduleRootPathSegments.push('..')
}
const rootPathSegments = moduleRootPathSegments.slice()
for (const _ of outModuleDirSegments) rootPathSegments.push('..')
const outDirname = outDirSegments.join('/')
const result = {
dirname: outDirname,
basename,
path: outDirname + '/' + basename,
moduleRootPath: moduleRootPathSegments.length ? moduleRootPathSegments.join('/') : '.',
rootPath: rootPathSegments.length ? rootPathSegments.join('/') : '.',
}
return result
}
function resolveActiveVersionSegment (component, version) {
let startPage = this.resolvePage('index.adoc', { component, version })
let startPageSrc
if (startPage) {
startPageSrc = startPage.src
} else {
startPageSrc = { component, version, module: 'ROOT', family: 'page', relative: 'index.adoc' }
this.removeFile((startPage = this.addFile({ src: startPageSrc })))
}
const outPathSegments = startPage.out.path.split('/')
for (const _ of startPage.out.moduleRootPath.split('/')) outPathSegments.pop()
if (startPageSrc.module !== 'ROOT') outPathSegments.pop()
if (startPageSrc.component !== 'ROOT') outPathSegments.shift()
return outPathSegments.length ? outPathSegments[0] : ''
}
module.exports = computeOut