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

Allow portions of documents to be excluded from search index #11190

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions news/changelog-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ All changes included in 1.6:

- ([#10268](https://github.com/quarto-dev/quarto-cli/issues/10268)): `quarto create` supports opening project in Positron, in addition to VS Code and RStudio IDE.
- ([#10285](https://github.com/quarto-dev/quarto-cli/issues/10285)): Include text from before the first chapter sections in search indices. In addition, include text of every element with `.quarto-include-in-search-index` class in search indices.
- ([#11189](https://github.com/quarto-dev/quarto-cli/issues/11189)): Exclude text of every element with the `.quarto-exclude-from-search-index` class from search indices.
- ([#10566](https://github.com/quarto-dev/quarto-cli/issues/10566)): Ensure that `quarto run` outputs `stdout` and `stderr` to the correct streams.

### Websites
Expand Down
6 changes: 6 additions & 0 deletions src/project/types/website/website-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ export async function updateSearchIndex(
}
});

// Remove all page elements that should be excluded from the search index
const exclusions = doc.querySelectorAll(".quarto-exclude-from-search-index");
for (const exclusion of exclusions) {
exclusion._remove();
}

// We always take the first child of the main region (whether that is a p or section)
// and create an index entry for the page itself (with no hash). If there is other
// 'unsectioned' content on the page, we include that as well.
Expand Down
6 changes: 6 additions & 0 deletions tests/docs/search/issue-11189/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.quarto/
*.html
*.pdf
*_files/
/_site_with_filter/
/_site_without_filter/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: Remove-class
author: Nick Vigilante
version: 1.0.0
quarto-required: ">=99.9.0"
contributes:
filters:
- remove-class.lua

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

-- Reformat all heading text
function RemoveClass(el)
class = "quarto-exclude-from-search-index"
if el.classes:includes(class) then
el.classes:remove(class)
end
end
4 changes: 4 additions & 0 deletions tests/docs/search/issue-11189/_quarto-with-filter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project:
output-dir: _site_with_filter
post-render:
- check-index-with-filter.ts
4 changes: 4 additions & 0 deletions tests/docs/search/issue-11189/_quarto-without-filter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project:
output-dir: _site_without_filter
post-render:
- check-index-without-filter.ts
19 changes: 19 additions & 0 deletions tests/docs/search/issue-11189/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
project:
type: website

profile:
default: without-filter

website:
title: "issue-11189"
navbar:
left:
- href: index.qmd
text: Home
- about.qmd

format:
html:
theme: cosmo
css: styles.css
toc: true
11 changes: 11 additions & 0 deletions tests/docs/search/issue-11189/about.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: "About"
---

About this site

::: {.quarto-exclude-from-search-index}

Please don't find me.

:::
9 changes: 9 additions & 0 deletions tests/docs/search/issue-11189/check-index-with-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const json = JSON.parse(Deno.readTextFileSync("_site_with_filter/search.json"));

const obj = Object.fromEntries(json.map((x: any) => [x.objectID, x]));

const file = "index.html";

if (obj[file].text.match("Please find me.") === null) {
throw new Error("could not find text that should be shown in " + file);
};
9 changes: 9 additions & 0 deletions tests/docs/search/issue-11189/check-index-without-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const json = JSON.parse(Deno.readTextFileSync("_site_without_filter/search.json"));

const obj = Object.fromEntries(json.map((x: any) => [x.objectID, x]));

const file = "about.html";

if (obj[file].text.match("Please don't find me.") !== null) {
throw new Error("found text that should be hidden in " + file);
};
15 changes: 15 additions & 0 deletions tests/docs/search/issue-11189/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "issue-11189"
filters:
- _extensions/remove-class/remove-class.lua
---

This is a Quarto website.

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.

::: {.quarto-exclude-from-search-index}

Please find me.

:::
1 change: 1 addition & 0 deletions tests/docs/search/issue-11189/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* css styles */
40 changes: 40 additions & 0 deletions tests/smoke/search/validate-search-index-exclusion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { testQuartoCmd } from "../../test.ts";
import { fileExists, noErrorsOrWarnings } from "../../verify.ts";

import { existsSync } from "../../../src/deno_ral/fs.ts";
import { join } from "../../../src/deno_ral/path.ts";
import { docs } from "../../utils.ts";

// Test a simple site
const input = docs("search/issue-11189");
const withFilterProfile = "--profile with-filter"

testQuartoCmd(
"render",
[input],
[noErrorsOrWarnings],
{
name: "Test search exclusions without filter",
teardown: async () => {
const siteDir = join(input, "_site_without_filter");
if (existsSync(siteDir)) {
await Deno.remove(siteDir, { recursive: true });
}
},
},
);

testQuartoCmd(
"render",
[withFilterProfile, input],
[noErrorsOrWarnings],
{
name: "Test search exclusions with filter",
teardown: async () => {
const siteDir = join(input, "_site_with_filter");
if (existsSync(siteDir)) {
await Deno.remove(siteDir, { recursive: true });
}
},
},
);
Loading