Skip to content

Commit

Permalink
support for page queries in source path urls
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 2, 2023
1 parent 01c269b commit 3f49875
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Any BREAKING CHANGE between minor versions will be documented here in upper case
## [1.18.4] - Unreleased
### Added
- Generic to `Page` to set the data interface. For example: `Page<Post>`.
- Support for multiple generated pages for `site.url()`.
For example: `site.url("~/multilanguage.tmpl.js(lang=en)")` returns the page with `lang=en` among all pages generated by this file.

## [1.18.3] - 2023-07-26
### Fixed
Expand Down
11 changes: 9 additions & 2 deletions core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,16 @@ export default class Site {
if (path.startsWith("~/")) {
path = decodeURI(path.slice(1));

// Has a search query
const match = path.match(/^(.*)\s*\(([^)]+)\)$/);
const srcPath = match ? match[1] : path;
const pages = match
? this.searcher.pages(match[2]).map<Page>((data) => data.page!)
: this.pages;

// It's a page
const page = this.pages.find((page) =>
page.src.path + page.src.ext === path
const page = pages.find((page) =>
page.src.path + page.src.ext === srcPath
);

if (page) {
Expand Down
12 changes: 6 additions & 6 deletions tests/__snapshots__/multilanguage.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ snapshot[`multilanguage plugin 3`] = `
' +
'<link rel="alternate" hreflang="fr" href="http://localhost/fr/other-page-french.html">
' +
"</head><body><p>Other page</p></body></html>",
'</head><body><p>Other page <a href="/gl/">Link to index in Galego</a></p></body></html>',
data: {
alternates: "Array(2)",
children: "<p>Other page</p>",
content: "<p>Other page</p>",
children: '<p>Other page <a href="/gl/">Link to index in Galego</a></p>',
content: \`<p>Other page <a href="{{ '~/index.md(lang=gl)' | url }}">Link to index in Galego</a></p>\`,
date: [],
id: "other",
lang: "en",
Expand Down Expand Up @@ -228,11 +228,11 @@ snapshot[`multilanguage plugin 3`] = `
' +
'<link rel="alternate" hreflang="fr" href="http://localhost/fr/other-page-french.html">
' +
"</head><body><p>Other page</p></body></html>",
'</head><body><p>Other page <a href="/gl/">Link to index in Galego</a></p></body></html>',
data: {
alternates: "Array(2)",
children: "<p>Other page</p>",
content: "<p>Other page</p>",
children: '<p>Other page <a href="/gl/">Link to index in Galego</a></p>',
content: \`<p>Other page <a href="{{ '~/index.md(lang=gl)' | url }}">Link to index in Galego</a></p>\`,
date: [],
id: "other",
lang: "fr",
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/multilanguage/other.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ fr:
url: /other-page-french.html
---

<p>Other page</p>
<p>Other page <a href="{{ '~/index.md(lang=gl)' | url }}">Link to index in Galego</a></p>

0 comments on commit 3f49875

Please sign in to comment.