breaking: Remove trailing slash from base_url and base_path outputs

This commit is contained in:
James M. Greene
2022-08-18 17:45:54 -05:00
parent 9a141972ca
commit dc5b850bfd
3 changed files with 21 additions and 10 deletions

View File

@@ -21,10 +21,10 @@ inputs:
required: false required: false
outputs: outputs:
base_url: 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: origin:
description: 'GitHub Pages site origin. Examples: "https://octocat.github.io", "https://www.example.com"' description: 'GitHub Pages site origin. Examples: "https://octocat.github.io", "https://www.example.com"'
host: host:
description: 'GitHub Pages site host. Examples: "octocat.github.io", "www.example.com"' description: 'GitHub Pages site host. Examples: "octocat.github.io", "www.example.com"'
base_path: base_path:
description: 'GitHub Pages site full base path. Examples: "/my-repo/" or "/"' description: 'GitHub Pages site full base path. Examples: "/my-repo" or ""'

View File

@@ -1,10 +1,21 @@
const core = require('@actions/core') const core = require('@actions/core')
function removeTrailingSlash(str) {
if (str.endsWith('/')) {
str = str.slice(0, -1)
}
return str
}
function outputPagesBaseUrl(siteUrl) { 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('origin', siteUrl.origin)
core.setOutput('host', siteUrl.host) core.setOutput('host', siteUrl.host)
core.setOutput('base_path', siteUrl.pathname) core.setOutput('base_path', basePath)
} }
module.exports = outputPagesBaseUrl module.exports = outputPagesBaseUrl

View File

@@ -23,10 +23,10 @@ describe('outputPagesBaseUrl', () => {
outputPagesBaseUrl(new URL(baseUrl)) 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('origin', 'https://octocat.github.io')
expect(core.setOutput).toHaveBeenCalledWith('host', '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 () => { it('gets expected outputs for project site', async () => {
@@ -34,10 +34,10 @@ describe('outputPagesBaseUrl', () => {
outputPagesBaseUrl(new URL(baseUrl)) 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('origin', 'https://octocat.github.io')
expect(core.setOutput).toHaveBeenCalledWith('host', '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 () => { it('gets expected outputs for site with custom domain name', async () => {
@@ -45,9 +45,9 @@ describe('outputPagesBaseUrl', () => {
outputPagesBaseUrl(new URL(baseUrl)) 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('origin', 'https://www.example.com')
expect(core.setOutput).toHaveBeenCalledWith('host', 'www.example.com') expect(core.setOutput).toHaveBeenCalledWith('host', 'www.example.com')
expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') expect(core.setOutput).toHaveBeenCalledWith('base_path', '')
}) })
}) })