Commit e70a1c91 authored by Guillaume Grossetie's avatar Guillaume Grossetie
Browse files

resolves #27 if the site URL is set, only use the pathname segment

parent 3a7ef6f9
......@@ -25,6 +25,7 @@ function generateIndex (playbook, pages) {
siteUrl = ''
}
if (siteUrl.charAt(siteUrl.length - 1) === '/') siteUrl = siteUrl.substr(0, siteUrl.length - 1)
const urlPath = extractUrlPath(siteUrl)
if (!pages.length) return {}
// Map of Lunr ref to document
const documentsStore = {}
......@@ -78,7 +79,7 @@ function generateIndex (playbook, pages) {
component: page.src.component,
version: page.src.version,
name: page.src.stem,
url: siteUrl + page.pub.url,
url: urlPath + page.pub.url,
titles: titles // TODO get title id to be able to use fragment identifier
}
})
......@@ -114,6 +115,15 @@ function generateIndex (playbook, pages) {
store: documentsStore
}
}
// Extract the path from an URL
function extractUrlPath (url) {
if (url) {
if (url.charAt() === '/') return url
const urlPath = new URL(url).pathname
return urlPath === '/' ? '' : urlPath
}
return ''
}
// Helper function allowing Antora to create a site asset containing the index
function createIndexFile (index) {
......
......@@ -5,8 +5,8 @@ const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
const generateIndex = require('../lib/index')
describe('Generate index', function () {
it('should generate an empty index when there\'s no page', function () {
describe('Generate index', () => {
it('should generate an empty index when there\'s no page', () => {
const playbook = {
site: {
url: 'https://antora.org'
......@@ -17,29 +17,10 @@ describe('Generate index', function () {
const index = generateIndex(playbook, pages)
expect(index).to.be.empty()
})
it('should use relative links when site URL is not define', function () {
const playbook = {
site: {} // site.url is undefined
}
const pages = [{
contents: Buffer.from('foo'),
src: {
component: 'component-a',
version: '2.0',
stem: 'install-foo'
},
asciidoc: {},
pub: {
url: '/component-a/install-foo'
}
}]
const index = generateIndex(playbook, pages)
expect(index.store['/component-a/install-foo'].url).to.equal('/component-a/install-foo')
})
it('should generate an index', function () {
it('should generate an index', () => {
const playbook = {
site: {
url: 'https://antora.org'
url: 'https://antora.org/docs/'
}
}
const pages = [{
......@@ -55,7 +36,7 @@ describe('Generate index', function () {
}
}]
const index = generateIndex(playbook, pages)
const installPage = index.store['https://antora.org/component-a/install-foo']
const installPage = index.store['/docs/component-a/install-foo']
expect(installPage.text).to.equal('foo')
expect(installPage.component).to.equal('component-a')
expect(installPage.version).to.equal('2.0')
......@@ -67,7 +48,7 @@ describe('Generate index', function () {
expect(index.index.search('*foo*'), '*foo* is present in contents').to.have.lengthOf(1)
expect(index.index.search('foo*'), 'foo* is present in contents').to.have.lengthOf(1)
})
it('should generate an document for each titles', function () {
it('should generate a document for each titles', () => {
const playbook = {
site: {
url: 'https://docs.antora.org'
......@@ -97,7 +78,7 @@ describe('Generate index', function () {
}
}]
const index = generateIndex(playbook, pages)
const installPage = index.store['https://docs.antora.org/antora/1.0/']
const installPage = index.store['/antora/1.0/']
expect(installPage.text).to.equal('The Static Site Generator for Tech Writers This site hosts the technical documentation for Antora With Antora, you manage docs as code')
expect(installPage.component).to.equal('hello')
expect(installPage.version).to.equal('1.0')
......@@ -148,7 +129,7 @@ describe('Generate index', function () {
}
}]
const index = generateIndex(playbook, pages)
const installPage = index.store['https://docs.antora.org/antora/1.0/']
const installPage = index.store['/antora/1.0/']
expect(installPage.text).to.equal('The Static Site Generator for Tech Writers This site hosts the technical documentation for Antora With Antora, you manage docs as code')
expect(installPage.component).to.equal('hello')
expect(installPage.version).to.equal('1.0')
......@@ -196,7 +177,7 @@ describe('Generate index', function () {
}
}]
const index = generateIndex(playbook, pages)
const whatsNewPage = index.store['https://docs.antora.org/antora/1.0/whats-new.html']
const whatsNewPage = index.store['/antora/1.0/whats-new.html']
expect(whatsNewPage.title).to.equal('What’s New in Antora')
})
it('should exclude pages with noindex defined as metadata', () => {
......@@ -320,9 +301,9 @@ describe('Generate index', function () {
}
}]
const index = generateIndex(playbook, pages)
const privatePage = index.store['https://docs.antora.org/antora/1.0/']
const privatePage = index.store['/antora/1.0/']
expect(privatePage).to.be.undefined()
const featuresPage = index.store['https://docs.antora.org/antora/1.0/features/']
const featuresPage = index.store['/antora/1.0/features/']
expect(featuresPage.text).to.equal('Automate the assembly of your secure, nimble static site as changes happen instead of wrestling with a CMS giant. Rebuild and deploy your site automatically in a matter of seconds in response to any change. Never have to worry about patching security holes in your deployed CMS application since you don’t have one. All pages are static—​in the JAMstack style. Need to migrate your site to a different domain? Just rebuild the site and relaunch it on the new host. Adapt your site to fit seamlessly with your other web properties. No site is an island. Sites must play nice with others to maintain a consistent brand and user experiences. Static sites generated by Antora are well-suited for this role. With page templates and a little help from an automated process, you can blend your documentation pages into existing sites, giving the impression it’s all part of a single uniform site.')
})
it('should exclude pages with noindex defined as attribute', () => {
......@@ -395,9 +376,69 @@ describe('Generate index', function () {
}
}]
const index = generateIndex(playbook, pages)
const privatePage = index.store['https://docs.antora.org/antora/1.0/']
const privatePage = index.store['/antora/1.0/']
expect(privatePage).to.be.undefined()
const featuresPage = index.store['https://docs.antora.org/antora/1.0/features/']
const featuresPage = index.store['/antora/1.0/features/']
expect(featuresPage.text).to.equal('Automate the assembly of your secure, nimble static site as changes happen instead of wrestling with a CMS giant.')
})
describe('Paths', () => {
it('should use relative links when site URL is not defined', () => {
const playbook = {
site: {} // site.url is undefined
}
const pages = [{
contents: Buffer.from('foo'),
src: {
component: 'component-a',
version: '2.0',
stem: 'install-foo'
},
pub: {
url: '/component-a/install-foo'
}
}]
const index = generateIndex(playbook, pages)
expect(index.store['/component-a/install-foo'].url).to.equal('/component-a/install-foo')
})
it('should use relative links when site URL is a relative path', () => {
const playbook = {
site: {
url: '/docs'
}
}
const pages = [{
contents: Buffer.from('foo'),
src: {
component: 'component-a',
version: '2.0',
stem: 'install-foo'
},
pub: {
url: '/component-a/install-foo'
}
}]
const index = generateIndex(playbook, pages)
expect(index.store['/docs/component-a/install-foo'].url).to.equal('/docs/component-a/install-foo')
})
it('should use absolute links when site URL is an absolute local path (using file:// protocol)', () => {
const playbook = {
site: {
url: 'file:///path/to/docs'
}
}
const pages = [{
contents: Buffer.from('foo'),
src: {
component: 'component-a',
version: '2.0',
stem: 'install-foo'
},
pub: {
url: '/component-a/install-foo'
}
}]
const index = generateIndex(playbook, pages)
expect(index.store['/path/to/docs/component-a/install-foo'].url).to.equal('/path/to/docs/component-a/install-foo')
})
})
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment