diff --git a/action.yml b/action.yml index 8e4f6da..15fd9f8 100644 --- a/action.yml +++ b/action.yml @@ -21,10 +21,10 @@ inputs: required: false outputs: base_url: - description: 'GitHub Pages site full base URL. Examples: "https://octocat.github.io/my-repo/", "https://octocat.github.io/", "https://www.example.com/"' + description: 'GitHub Pages site full base URL. Examples: "https://octocat.github.io/my-repo", "https://octocat.github.io", "https://www.example.com"' origin: description: 'GitHub Pages site origin. Examples: "https://octocat.github.io", "https://www.example.com"' host: description: 'GitHub Pages site host. Examples: "octocat.github.io", "www.example.com"' base_path: - description: 'GitHub Pages site full base path. Examples: "/my-repo/" or "/"' + description: 'GitHub Pages site full base path. Examples: "/my-repo" or ""' diff --git a/src/output-pages-base-url.js b/src/output-pages-base-url.js index 669ea0d..3ea0cc5 100644 --- a/src/output-pages-base-url.js +++ b/src/output-pages-base-url.js @@ -1,10 +1,21 @@ const core = require('@actions/core') +function removeTrailingSlash(str) { + if (str.endsWith('/')) { + str = str.slice(0, -1) + } + return str +} + function outputPagesBaseUrl(siteUrl) { - core.setOutput('base_url', siteUrl.href) + // Many static site generators do not want the trailing slash, and it is much easier to add than remove in a workflow + const baseUrl = removeTrailingSlash(siteUrl.href) + const basePath = removeTrailingSlash(siteUrl.pathname) + + core.setOutput('base_url', baseUrl) core.setOutput('origin', siteUrl.origin) core.setOutput('host', siteUrl.host) - core.setOutput('base_path', siteUrl.pathname) + core.setOutput('base_path', basePath) } module.exports = outputPagesBaseUrl diff --git a/src/output-pages-base-url.test.js b/src/output-pages-base-url.test.js index e3f4586..2b5c5eb 100644 --- a/src/output-pages-base-url.test.js +++ b/src/output-pages-base-url.test.js @@ -23,10 +23,10 @@ describe('outputPagesBaseUrl', () => { outputPagesBaseUrl(new URL(baseUrl)) - expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) + expect(core.setOutput).toHaveBeenCalledWith('base_url', 'https://octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith('origin', 'https://octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io') - expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') + expect(core.setOutput).toHaveBeenCalledWith('base_path', '') }) it('gets expected outputs for project site', async () => { @@ -34,10 +34,10 @@ describe('outputPagesBaseUrl', () => { outputPagesBaseUrl(new URL(baseUrl)) - expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) + expect(core.setOutput).toHaveBeenCalledWith('base_url', 'https://octocat.github.io/my-repo') expect(core.setOutput).toHaveBeenCalledWith('origin', 'https://octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io') - expect(core.setOutput).toHaveBeenCalledWith('base_path', '/my-repo/') + expect(core.setOutput).toHaveBeenCalledWith('base_path', '/my-repo') }) it('gets expected outputs for site with custom domain name', async () => { @@ -45,9 +45,9 @@ describe('outputPagesBaseUrl', () => { outputPagesBaseUrl(new URL(baseUrl)) - expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) + expect(core.setOutput).toHaveBeenCalledWith('base_url', 'https://www.example.com') expect(core.setOutput).toHaveBeenCalledWith('origin', 'https://www.example.com') expect(core.setOutput).toHaveBeenCalledWith('host', 'www.example.com') - expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') + expect(core.setOutput).toHaveBeenCalledWith('base_path', '') }) })