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

Add Pagefind weight frontmatter #2371

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changeset/eighty-zoos-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Adds the `pagefind.weight` frontmatter property for customizing how a page ranks in Pagefind search results.
38 changes: 38 additions & 0 deletions docs/src/content/docs/guides/site-search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ This text will be hidden from search.
</div>
```

## Adjust ranking in search results

Pagefind's index ranking can be adjusted using the [`data-pagefind-weight`](https://pagefind.app/docs/weighting/#ranking-content-higher-or-lower) attribute. This can be used to [adjust a whole page](#adjust-whole-page-rank) or [individual sections of a page](#adjust-individual-section-rank).

Learn more about about how the weight value impacts search results in the [Pagefind Weighting content guide](https://pagefind.app/docs/weighting/).
Comment on lines +45 to +49
Copy link
Member

@ematipico ematipico Sep 19, 2024

Choose a reason for hiding this comment

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

It would be nice to explain to users when they should change the ranking, and why. The docs of pagefind only explain some technical stuff, but from the point of view of a maintainer like myself, it would be nice to have some guidance on why I should use it.

Copy link
Contributor Author

@lorenzolewis lorenzolewis Sep 22, 2024

Choose a reason for hiding this comment

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

Suggested change
## Adjust ranking in search results
Pagefind's index ranking can be adjusted using the [`data-pagefind-weight`](https://pagefind.app/docs/weighting/#ranking-content-higher-or-lower) attribute. This can be used to [adjust a whole page](#adjust-whole-page-rank) or [individual sections of a page](#adjust-individual-section-rank).
Learn more about about how the weight value impacts search results in the [Pagefind Weighting content guide](https://pagefind.app/docs/weighting/).
## Adjust ranking in search results
Pagefind's index ranking can be adjusted using the [`data-pagefind-weight`](https://pagefind.app/docs/weighting/#ranking-content-higher-or-lower) attribute. This can be used to [adjust a whole page](#adjust-whole-page-rank) or [individual sections of a page](#adjust-individual-section-rank).
For example, if there is a high-level guide that is a better entry point for readers than an error reference page containing similar keywords, increasing the weight of the guide page will cause it to be ranked higher in the Pagefind search results.
Learn more about about how the weight value impacts search results in the [Pagefind Weighting content guide](https://pagefind.app/docs/weighting/).

@ematipico Thanks! Do you think this gives a good example of the usage here? Happy for any suggestions if anything else comes to mind 🥳

Copy link
Member

Choose a reason for hiding this comment

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

We're getting somewhere I think. Now we should explain when to increase the page weight and when decrease the page weight.


### Adjust whole page rank

To adjust where a page is listed in the Pagefind search results, add the [`pagefind.weight`](/reference/frontmatter/#pagefind) property to the page's frontmatter:

```md title="src/content/docs/important-doc.md" ins={3,4}
---
title: Content to hide from search
pagefind:
weight: 2
---
```

### Adjust individual section rank

Pagefind will use the [`data-pagefind-weight`](https://pagefind.app/docs/weighting/#ranking-content-higher-or-lower) attribute to adjust the ranking of search results.

In this example, the

```md title="src/content/docs/custom-weighted-page.md" ins="data-pagefind-weight=\"5.0\""
---
title: Custom Weighted Page
---

This text will be ranked normally.

<div data-pagefind-weight="5.0">

This text will be ranked higher in search results.

</div>
```

## Alternative search providers

### Algolia DocSearch
Expand Down
19 changes: 17 additions & 2 deletions docs/src/content/docs/reference/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,12 @@ next: false

### `pagefind`

**type:** `boolean`
**type:** `boolean | { weight: number; }`
**default:** `true`

Set whether this page should be included in the [Pagefind](https://pagefind.app/) search index. Set to `false` to exclude a page from search results:
Set the weighting of a page in the [Pagefind](https://pagefind.app/) search index or whether the page should be included.

Set to `false` to exclude a page from search results:

```md
---
Expand All @@ -268,6 +270,19 @@ pagefind: false
---
```

Set to `pagefind.weight` with a number between 0 and 10 (inclusive) to adjust the ranking a page in search results:

```md
---
# src/content/docs/example.md
# Adjust the ranking of a page
pagefind:
weight: 4.0
---
```

Learn more about about how the weight value impacts search results in the [Pagefind Weighting content guide](https://pagefind.app/docs/weighting/).

### `draft`

**type:** `boolean`
Expand Down
10 changes: 10 additions & 0 deletions packages/starlight/__e2e__/fixtures/pagefind/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import starlight from '@astrojs/starlight';
import { defineConfig } from 'astro/config';

export default defineConfig({
integrations: [
starlight({
title: 'Pagefind',
}),
],
});
9 changes: 9 additions & 0 deletions packages/starlight/__e2e__/fixtures/pagefind/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@e2e/pagefind",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/starlight": "workspace:*",
"astro": "^4.15.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineCollection } from 'astro:content';
import { docsSchema } from '@astrojs/starlight/schema';

export const collections = {
docs: defineCollection({ schema: docsSchema() }),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Index
pagefind:
weight: 5.5
---

This page has a custom weight of 5.5.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Index
pagefind: false
---

This page will NOT be indexed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Index
---

This is a page that should be indexed with no special weight.
28 changes: 28 additions & 0 deletions packages/starlight/__e2e__/pagefind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, testFactory } from './test-utils';

const test = testFactory('./fixtures/pagefind/');

test('page has pagefind data attribute', async ({ page, getProdServer }) => {
const starlight = await getProdServer();
await starlight.goto('/pagefind');

const main = page.locator('main');
await expect(main).toHaveAttribute('data-pagefind-body');
});

test('page has no pagefind data attribute', async ({ page, getProdServer }) => {
const starlight = await getProdServer();
await starlight.goto('/ignored-page');

const main = page.locator('main');
await expect(main).not.toHaveAttribute('data-pagefind-body');
});

test('page has custom weighting', async ({ page, getProdServer }) => {
const starlight = await getProdServer();
await starlight.goto('/custom-weight');

const main = page.locator('main');
await expect(main).toHaveAttribute('data-pagefind-body');
await expect(main).toHaveAttribute('data-pagefind-weight', '5.5');
});
8 changes: 7 additions & 1 deletion packages/starlight/components/Page.astro
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ import '../style/asides.css';
// Important that this is the last import so it can override built-in styles.
import 'virtual:starlight/user-css';

const pagefindConfig = Astro.props.entry.data.pagefind;

const pagefindEnabled =
Astro.props.entry.slug !== '404' &&
!Astro.props.entry.slug.endsWith('/404') &&
Astro.props.entry.data.pagefind !== false;
pagefindConfig !== false;

const pagefindWeight =
pagefindEnabled && typeof pagefindConfig === 'object' && pagefindConfig.weight;
---

<html
Expand Down Expand Up @@ -85,6 +90,7 @@ const pagefindEnabled =
<PageSidebar slot="right-sidebar" {...Astro.props} />
<main
data-pagefind-body={pagefindEnabled}
data-pagefind-weight={pagefindWeight}
lang={Astro.props.entryMeta.lang}
dir={Astro.props.entryMeta.dir}
>
Expand Down
10 changes: 8 additions & 2 deletions packages/starlight/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ const StarlightFrontmatterSchema = (context: SchemaContext) =>
})
.optional(),

/** Pagefind indexing for this page - set to false to disable. */
pagefind: z.boolean().default(true),
/**
* Pagefind configuration for this page - set to false to disable indexing or set
* as an object with a "weight" key between 0.0 and 10.0 to adjust the ranking where
* a larger number is a higher ranking.
*
* More information about Pagefind weighting: https://pagefind.app/docs/weighting/
*/
pagefind: z.union([z.boolean(), z.object({ weight: z.number().min(0).max(10) })]).default(true),

/**
* Indicates that this page is a draft and will not be included in production builds.
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading