Skip to content

Commit

Permalink
breaking: Remove trailing slash from base_url and base_path outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
James M. Greene authored and James M. Greene committed Aug 18, 2022
1 parent 9a14197 commit dc5b850
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""'
15 changes: 13 additions & 2 deletions src/output-pages-base-url.js
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions src/output-pages-base-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ 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 () => {
const baseUrl = 'https://octocat.github.io/my-repo/'

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 () => {
const baseUrl = 'https://www.example.com/'

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', '')
})
})

0 comments on commit dc5b850

Please sign in to comment.