Skip to content

Commit 87ce780

Browse files
authored
Merge pull request #11191 from quarto-dev/bugfix/10285
Index pre-section contents of book chapters and `.quarto-include-in-search-index` elements
2 parents 11c639d + f24a0d3 commit 87ce780

File tree

15 files changed

+139
-4
lines changed

15 files changed

+139
-4
lines changed

news/changelog-1.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ All changes included in 1.6:
7878
## Projects
7979

8080
- ([#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.
81+
- ([#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.
8182
- ([#10566](https://github.com/quarto-dev/quarto-cli/issues/10566)): Ensure that `quarto run` outputs `stdout` and `stderr` to the correct streams.
8283

8384
### Websites

src/project/types/website/website-search.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,31 @@ export async function updateSearchIndex(
326326

327327
// If there are any paragraphs residing outside a section, just
328328
// include that in the document entry
329-
const pararaphNodes = doc.querySelectorAll(
330-
`${mainSelector} > p, ${mainSelector} > div.cell`,
329+
const paragraphNodes = doc.querySelectorAll(
330+
`${mainSelector} > p, ${mainSelector} > div.cell, main > p`,
331331
);
332332

333-
for (const paragraphNode of pararaphNodes) {
333+
const addToIndex = (paragraphNode: Element) => {
334334
const text = paragraphNode.textContent.trim();
335335
if (text) {
336336
pageText.push(text);
337337
}
338338

339339
// Since these are already indexed with the main entry, remove them
340340
// so they are not indexed again
341-
(paragraphNode as Element).remove();
341+
paragraphNode.remove();
342+
};
343+
344+
for (const paragraphNode of paragraphNodes) {
345+
addToIndex(paragraphNode as Element);
346+
}
347+
348+
// Add forced inclusions to the index
349+
const forcedInclusions = doc.querySelectorAll(
350+
`.quarto-include-in-search-index`,
351+
);
352+
for (const forcedInclusion of forcedInclusions) {
353+
addToIndex(forcedInclusion as Element);
342354
}
343355

344356
if (pageText.length > 0) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
project:
2+
type: book
3+
post-render:
4+
- check-index.ts
5+
6+
book:
7+
title: "issue-10285"
8+
author: "Norah Jones"
9+
date: "10/24/2024"
10+
chapters:
11+
- index.qmd
12+
- intro.qmd
13+
- search-test.qmd
14+
- search-test-2.qmd
15+
- search-test-3.qmd
16+
- summary.qmd
17+
- references.qmd
18+
19+
bibliography: references.bib
20+
21+
format:
22+
html:
23+
theme: cosmo
24+
pdf:
25+
documentclass: scrreprt
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const json = JSON.parse(Deno.readTextFileSync("_book/search.json"));
2+
3+
const obj = Object.fromEntries(json.map((x: any) => [x.objectID, x]));
4+
5+
for (const file of ["search-test.html", "search-test-2.html", "search-test-3.html"]) {
6+
if (obj[file].text.match("Please find me.") === null) {
7+
throw new Error("missing pre-section content in " + file);
8+
};
9+
}
50 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Preface {.unnumbered}
2+
3+
This is a Quarto book.
4+
5+
To learn more about Quarto books visit <https://quarto.org/docs/books>.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
This is a book created from markdown and executable code.
4+
5+
See @knuth84 for additional discussion of literate programming.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@article{knuth84,
2+
author = {Knuth, Donald E.},
3+
title = {Literate Programming},
4+
year = {1984},
5+
issue_date = {May 1984},
6+
publisher = {Oxford University Press, Inc.},
7+
address = {USA},
8+
volume = {27},
9+
number = {2},
10+
issn = {0010-4620},
11+
url = {https://doi.org/10.1093/comjnl/27.2.97},
12+
doi = {10.1093/comjnl/27.2.97},
13+
journal = {Comput. J.},
14+
month = may,
15+
pages = {97–111},
16+
numpages = {15}
17+
}
18+
19+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# References {.unnumbered}
2+
3+
::: {#refs}
4+
:::

0 commit comments

Comments
 (0)