Skip to content

Commit

Permalink
Merge pull request #19 from actions/no-trailing-slashes
Browse files Browse the repository at this point in the history
[BREAKING] Remove the trailing slash from `base_url` and `base_path` outputs
  • Loading branch information
JamesMGreene authored Aug 19, 2022
2 parents 9a14197 + 0ec542a commit 61fd3a3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 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 ""'
28 changes: 20 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15571,24 +15571,40 @@ module.exports = { getContext }
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(2186)
const removeTrailingSlash = __nccwpck_require__(9255)

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


/***/ }),

/***/ 9255:
/***/ ((module) => {

module.exports = function removeTrailingSlash(str) {
return str.endsWith('/') ? str.slice(0, -1) : str
}


/***/ }),

/***/ 4770:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(2186)
const { ConfigParser } = __nccwpck_require__(8395)
const removeTrailingSlash = __nccwpck_require__(9255)

// Return the settings to be passed to a {ConfigParser} for a given static site generator,
// optional configuration file path, and a Pages path value to inject
Expand All @@ -15609,9 +15625,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, pat
}
case 'next':
// Next does not want a trailing slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './next.config.js',
Expand All @@ -15636,9 +15650,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, pat
}
case 'sveltekit':
// SvelteKit does not want a trailing slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './svelte.config.js',
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/output-pages-base-url.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const core = require('@actions/core')
const removeTrailingSlash = require('./remove-trailing-slash')

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', '')
})
})
3 changes: 3 additions & 0 deletions src/remove-trailing-slash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function removeTrailingSlash(str) {
return str.endsWith('/') ? str.slice(0, -1) : str
}
9 changes: 3 additions & 6 deletions src/set-pages-path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const core = require('@actions/core')
const { ConfigParser } = require('./config-parser')
const removeTrailingSlash = require('./remove-trailing-slash')

// Return the settings to be passed to a {ConfigParser} for a given static site generator,
// optional configuration file path, and a Pages path value to inject
Expand All @@ -20,9 +21,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, pat
}
case 'next':
// Next does not want a trailing slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './next.config.js',
Expand All @@ -47,9 +46,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, pat
}
case 'sveltekit':
// SvelteKit does not want a trailing slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './svelte.config.js',
Expand Down

0 comments on commit 61fd3a3

Please sign in to comment.