Add url field to PageInfo for canonical URL access in layouts#250
Conversation
Coverage Report for CI Build 26374300398Coverage increased (+0.5%) to 92.658%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
There was a problem hiding this comment.
Pull request overview
This PR aims to make canonical URL construction easier in layouts by automatically injecting a pageUrl value into PageData.vars, derived from pageInfo.
Changes:
- Inject
pageUrlinto the base of thevarsmerge insidePageData.vars, allowing user-definedpageUrlto override it via later spreads.
Comments suppressed due to low confidence (1)
lib/build-pages/page-data.js:149
- New public-ish behavior (
vars.pageUrl) isn’t covered by tests. The repo already has node:test coverage for build behavior (eg test-cases/general-features/index.test.js). Please add a regression test that assertsvars.pageUrl(or an HTML marker rendered from it) for both an index page (/some-dir/) and a loose markdown page (/some-dir/file.html), and ideally cover Windows path normalization as well.
pageUrl: this.pageInfo.path ? `/${this.pageInfo.path}/` : '/',
...globalVars,
...globalDataVars,
...pageVars,
...builderVars,
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
This PR makes vars.pageUrl available to every rendered page by computing it from pageInfo inside PageData.vars, allowing layouts to build canonical/OG URLs without duplicating the project’s URL-construction convention.
Changes:
- Injects a computed
pageUrlintoPageData.varsat the base of the vars merge (so user overrides still win). - Computes
pageUrlwith special handling forindex.html(trailing-slash URLs) vs non-index outputs (include filename). - Normalizes URL paths via
fsPathToUrlPath()for cross-platform (Windows) safety.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Adds automatic vars.pageUrl injection to every page so layouts can build canonical URLs without manually reconstructing URL path conventions.
Changes:
- Inject
pageUrlintoPageData.varsat the lowest precedence so user-defined vars can override it. - Compute
pageUrlbased onpageInfo.path+outputName, mappingindex.htmlto trailing-slash URLs and including filenames for non-index outputs. - Normalize path separators via
fsPathToUrlPath()for cross-platform URL consistency.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This looks ready to go. It just needs readme docs and the above questions resolved. |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Every page now gets a pageUrl derived from pageInfo.path so layouts can build canonical URLs without manually computing the path-to-URL conversion. User-supplied pageUrl in pageVars or globalVars still takes precedence. Closes #238 Fix pageUrl for loose markdown pages and Windows path separators Loose markdown files (not page.md/README.md) have outputName set to their slug.html rather than index.html, so the previous formula produced an incorrect directory-style URL for them. The URL is now derived from outputName: index pages get a trailing slash, non-index pages include the filename. fsPathToUrlPath is used to normalize any OS-specific path separators. Extract computePageUrl helper and add unit tests for URL derivation rules Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Remove unnecessary #computePageUrl wrapper method Fix pretty types Improve typecast Document pageUrl variable injection in vars
Closes #238
What changed
Adds a
urlfield to thePageInfoobject, computed once at page identification time frompathandoutputName. Layouts can now usepage.urldirectly instead of manually reconstructing the URL frompage.path.URL derivation rules
outputName === 'index.html'): trailing-slash URL —/blog/my-post/, or/for the root/blog/loose-page.htmlPath separators are normalized via
fsPathToUrlPathfor Windows safety.Why
page.urlinstead ofvars.pageUrlurlis a fact about the page derived from its filesystem location, not a user-configurable variable. It belongs onPageInfoalongsidepathandoutputName, not injected into the user variable cascade. Layouts already receivepagedirectly.Also in this PR
@types/prettyinstalled, removing the// @ts-expect-erroron theprettyimport// @ts-ignoreinPageData.varswith a targeted/** @type {T} */castpretty()call-site type error (Awaited<V>→ cast tostring)page.urland replace manual path construction in examples