Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/build-pages/compute-page-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fsPathToUrlPath } from './page-builders/fs-path-to-url.js'

/**
* Derive the canonical URL path for a page from its filesystem path and output name.
* Index pages get a trailing-slash URL; other outputs include the filename.
*
* @param {object} params
* @param {string} params.path - The page's directory path relative to src root
* @param {string} params.outputName - The output filename (e.g. 'index.html' or 'loose-md.html')
* @returns {string}
*/
export function computePageUrl ({ path, outputName }) {
if (outputName === 'index.html') {
return path ? fsPathToUrlPath(path) + '/' : '/'
}
return path ? fsPathToUrlPath(path) + '/' + outputName : '/' + outputName
}
11 changes: 11 additions & 0 deletions lib/build-pages/page-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { resolveVars, resolvePostVars } from './resolve-vars.js'
import { pageBuilders } from './page-builders/index.js'
import { computePageUrl } from './compute-page-url.js'
// @ts-expect-error
import pretty from 'pretty'

Expand Down Expand Up @@ -141,6 +142,7 @@ export class PageData {
const { globalVars, globalDataVars, pageVars, builderVars } = this
// @ts-ignore
return {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of this?

pageUrl: this.#computePageUrl(),
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a method?

...globalVars,
...globalDataVars,
...pageVars,
Expand All @@ -156,6 +158,15 @@ export class PageData {
return this.workerFiles
}

/**
* Derive the canonical URL path for this page from its filesystem path and output name.
* Index pages get a trailing-slash URL; other outputs include the filename.
* @return {string}
*/
#computePageUrl () {
return computePageUrl(this.pageInfo)
}

/**
* [init description]
* @param {object} params - Parameters required to initialize
Expand Down
21 changes: 21 additions & 0 deletions lib/build-pages/page-data.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from 'node:test'
import assert from 'node:assert'
import { computePageUrl } from './compute-page-url.js'

test.describe('computePageUrl', () => {
test('root index.html maps to /', () => {
assert.strictEqual(computePageUrl({ path: '', outputName: 'index.html' }), '/')
})

test('nested index.html gets a trailing-slash URL', () => {
assert.strictEqual(computePageUrl({ path: 'blog/post', outputName: 'index.html' }), '/blog/post/')
})

test('non-index output includes filename in URL', () => {
assert.strictEqual(computePageUrl({ path: 'md-page', outputName: 'loose-md.html' }), '/md-page/loose-md.html')
})

test('non-index file at root includes filename only', () => {
assert.strictEqual(computePageUrl({ path: '', outputName: 'robots.txt' }), '/robots.txt')
})
})