Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding flag to retain “index.html” in results URLs #233

Merged
merged 3 commits into from
Feb 27, 2023
Merged
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
10 changes: 10 additions & 0 deletions docs/content/docs/search-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ Overrides the bundle directory. In most cases this should be automatically detec

See [multisite search > weighting](/docs/multisite/#changing-the-weighting-of-individual-indexes)

### Keep Index URL

```json
{
"keepIndexUrl": true
}
```

Keeps `index.html` at the end of search result paths. For example, a file called "public/cat/index.html will have a search result url of `public/cat/index.html`. Defaults to `false`, stripping `index.html`.

Comment on lines +66 to +75
Copy link
Contributor

Choose a reason for hiding this comment

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

These docs should actually live in docs/config-options.md — this page is for the browser-side config — but I can move them to the correct place after this is merged 🙂

### Merge filter

See [multisite search > filtering](/docs/multisite/#filtering-results-by-index)
30 changes: 30 additions & 0 deletions pagefind/features/search_options.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,33 @@ Feature: Search Options
"""
Then There should be no logs
Then The selector "[data-url]" should contain "/blog/cat/"

Scenario: Keep Index URL can be configured
Given I have a "public/index.html" file with the body:
"""
<p data-url>Nothing</p>
"""
Given I have a "public/cat/index.html" file with the body:
"""
<h1>world</h1>
"""
When I run my program with the flags:
| --bundle-dir blog/_pagefind |
| --keep-index-url |
Then I should see "Running Pagefind" in stdout
Then I should see the file "public/blog/_pagefind/pagefind.js"
When I serve the "public" directory
When I load "/"
When I evaluate:
"""
async function() {
let pagefind = await import("/blog/_pagefind/pagefind.js");

let search = await pagefind.search("world");

let data = await search.results[0].data();
document.querySelector('[data-url]').innerText = data.url;
}
"""
Then There should be no logs
Then The selector "[data-url]" should contain "/blog/cat/index.html"
8 changes: 7 additions & 1 deletion pagefind/src/fossick/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,15 @@ fn build_url(page_url: &Path, options: &SearchOptions) -> String {
return "/unknown/".to_string();
};

let final_url: String = if !options.keep_index_url {
url.to_slash_lossy().to_owned().replace("index.html", "")
} else {
url.to_slash_lossy().to_owned().to_string()
};

format!(
"/{}",
url.to_slash_lossy().to_owned().replace("index.html", "")
final_url
)
}

Expand Down
11 changes: 11 additions & 0 deletions pagefind/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ pub struct PagefindInboundConfig {
#[clap(required = false)]
#[serde(default = "defaults::default_false")]
pub verbose: bool,

#[clap(
long,
short,
help = "Keep \"index.html\" at the end of search result paths. Defaults to false, stripping \"index.html\"."
)]
#[clap(required = false)]
#[serde(default = "defaults::default_false")]
pub keep_index_url: bool,
}

mod defaults {
Expand Down Expand Up @@ -99,6 +108,7 @@ pub struct SearchOptions {
pub force_language: Option<String>,
pub version: &'static str,
pub logger: Logger,
pub keep_index_url: bool
}

impl SearchOptions {
Expand All @@ -124,6 +134,7 @@ impl SearchOptions {
force_language: config.force_language,
version: env!("CARGO_PKG_VERSION"),
logger: Logger::new(log_level),
keep_index_url: config.keep_index_url
})
}
}
Expand Down