@antora/asciidoc-loader
Version:
Loads AsciiDoc content into an Asciidoctor Document object (AST) for use in an Antora documentation pipeline.
31 lines (26 loc) • 1.04 kB
JavaScript
const { posix: path } = require('path')
/**
* Computes the shortest relative path between two URLs.
*
* This function takes into account directory index URLs and extensionless
* URLs. It assumes it's working with root-relative URLs, not absolute URLs.
*
* @memberof asciidoc-loader
*
* @param {String} from - The root-relative start URL.
* @param {String} to - The root-relative target URL.
* @param {String} [hash=''] - The URL hash to append to the URL (not #).
*
* @returns {String} The shortest relative path to travel from the start URL to the target URL.
*/
function computeRelativeUrlPath (from, to, hash = '') {
if (to.charAt() !== '/') return to + hash
if (to === from) return hash || (isDir(to) ? './' : path.basename(to))
const rel = path.relative(path.dirname(from + '.'), to)
return rel ? (isDir(to) ? rel + '/' : rel) + hash : (isDir(to) ? './' : '../' + path.basename(to)) + hash
}
function isDir (str) {
return str.charAt(str.length - 1) === '/'
}
module.exports = computeRelativeUrlPath