From 437583e5c2891331e6a45ab4dddb7b12de6906da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sat, 15 Mar 2025 17:39:08 +0100 Subject: [PATCH 1/7] Search results with breadcrumbs. Search with filters by section. --- docs/_static/searchtools.js | 644 +++++++++++++++++++ docs/_templates/components/search-field.html | 42 ++ docs/_templates/search.html | 42 ++ 3 files changed, 728 insertions(+) create mode 100644 docs/_static/searchtools.js create mode 100644 docs/_templates/components/search-field.html create mode 100644 docs/_templates/search.html diff --git a/docs/_static/searchtools.js b/docs/_static/searchtools.js new file mode 100644 index 0000000000..f420560dda --- /dev/null +++ b/docs/_static/searchtools.js @@ -0,0 +1,644 @@ +/* + * searchtools.js + * ~~~~~~~~~~~~~~~~ + * + * Sphinx JavaScript utilities for the full-text search. + * + * :copyright: Copyright 2007-2023 by the Sphinx team, see AUTHORS. + * :license: BSD, see LICENSE for details. + * + */ +"use strict"; + +var title_repository = 'Plone training'; + + +/** + * Return array with titles of ancestors of file. + * @param {number} idx - The index of the result item in global list of files + * @returns array + */ +function _getParentTitles(idx, docNames, titles) { + let path = docNames[idx] + let parentpathtokens = path.split('/').slice(0, -1); + + let parentTitles = parentpathtokens.map((el, index) => { + let foo = `${parentpathtokens.slice(0, index+1).join('/')}` + let parentId = docNames.indexOf(foo); + if (parentId === -1) { + foo = `${parentpathtokens.slice(0, index+1).join('/')}/index` + parentId = docNames.indexOf(foo); + } + let title = parentId === -1 ? title_repository : titles[parentId]; + return title + }) + + return parentTitles +} + +/** + * Simple result scoring code. + */ +if (typeof Scorer === "undefined") { + var Scorer = { + // Implement the following function to further tweak the score for each result + // The function takes a result array [docname, title, anchor, descr, score, filename] + // and returns the new score. + /* + score: result => { + const [docname, title, anchor, descr, score, filename] = result + return score + }, + */ + + // query matches the full name of an object + objNameMatch: 11, + // or matches in the last dotted part of the object name + objPartialMatch: 6, + // Additive scores depending on the priority of the object + objPrio: { + 0: 15, // used to be importantResults + 1: 5, // used to be objectResults + 2: -5, // used to be unimportantResults + }, + // Used when the priority is not in the mapping. + objPrioDefault: 0, + + // query found in title + title: 15, + partialTitle: 7, + // query found in terms + term: 5, + partialTerm: 2, + }; +} + +const _removeChildren = (element) => { + while (element && element.lastChild) element.removeChild(element.lastChild); +}; + +/** + * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping + */ +const _escapeRegExp = (string) => + string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string + + +function _getBreadcrumbs(item, linkUrl) { + // No breadcrumbs for top level pages + if (item[0].split('/')[1] == 'index') { + return null + } + let parentTitles = item[6]; + + let parentDefaultTitles = [ + "Plone trainings", + "Training for Plone developers" + ]; + parentTitles = Array.isArray(parentTitles) ? parentTitles : parentDefaultTitles; + let pathTokens = item[0].split('/') + .slice(0, -1); + let pathArray = pathTokens.map((el, index) => { + return { + "path": pathTokens.slice(0, index+1).join('/'), + "title": parentTitles[index] + } + }) + let markup = pathArray + .map((el, idx) => { + return `${el.title}` + }) + markup.push(`${item[1]}`) + return markup.join('>'); +} + +const _displayItem = (item, searchTerms) => { + const docBuilder = DOCUMENTATION_OPTIONS.BUILDER; + const docUrlRoot = DOCUMENTATION_OPTIONS.URL_ROOT; + const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX; + const docLinkSuffix = DOCUMENTATION_OPTIONS.LINK_SUFFIX; + const showSearchSummary = DOCUMENTATION_OPTIONS.SHOW_SEARCH_SUMMARY; + + const [docName, title, anchor, descr, score, _filename] = item; + + let listItem = document.createElement("li"); + let requestUrl; + let linkUrl; + if (docBuilder === "dirhtml") { + // dirhtml builder + let dirname = docName + "/"; + if (dirname.match(/\/index\/$/)) + dirname = dirname.substring(0, dirname.length - 6); + else if (dirname === "index/") dirname = ""; + requestUrl = docUrlRoot + dirname; + linkUrl = requestUrl; + } else { + // normal html builders + requestUrl = docUrlRoot + docName + docFileSuffix; + linkUrl = docName + docLinkSuffix; + } + + let breadcrumbs = _getBreadcrumbs(item, linkUrl); + if (breadcrumbs) { + let breadcrumbsNode = document.createElement("div"); + breadcrumbsNode.innerHTML = breadcrumbs; + breadcrumbsNode.classList.add("breadcrumbs"); + listItem.appendChild(breadcrumbsNode); + } + + // Title links to chapter + let linkEl = listItem.appendChild(document.createElement("a")); + linkEl.href = linkUrl + anchor; + linkEl.dataset.score = score; + linkEl.innerHTML = title; + if (descr) + listItem.appendChild(document.createElement("span")).innerHTML = + " (" + descr + ")"; + else if (showSearchSummary) + fetch(requestUrl) + .then((responseData) => responseData.text()) + .then((data) => { + if (data) + listItem.appendChild( + Search.makeSearchSummary(data, searchTerms) + ); + }); + Search.output.appendChild(listItem); +}; + +const _finishSearch = (resultCount) => { + Search.stopPulse(); + Search.title.innerText = _("Search Results"); + if (!resultCount) + Search.status.innerText = Documentation.gettext( + "Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories." + ); + else + // Search.status.innerText = _( + // `Search finished, found ${resultCount} page(s) matching the search query.` + // ); + Search.status.innerText = `${resultCount} page(s) found.` +}; + +const _displayNextItem = ( + results, + resultCount, + searchTerms +) => { + // results left, load the summary and display it + // this is intended to be dynamic (don't sub resultsCount) + if (results.length) { + _displayItem(results.pop(), searchTerms); + setTimeout( + () => _displayNextItem(results, resultCount, searchTerms), + 5 + ); + } + // search finished, update title and status message + else _finishSearch(resultCount); +}; + +/** + * Default splitQuery function. Can be overridden in ``sphinx.search`` with a + * custom function per language. + * + * The regular expression works by splitting the string on consecutive characters + * that are not Unicode letters, numbers, underscores, or emoji characters. + * This is the same as ``\W+`` in Python, preserving the surrogate pair area. + */ +if (typeof splitQuery === "undefined") { + var splitQuery = (query) => query + .split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu) + .filter(term => term) // remove remaining empty strings +} + +/** + * Search Module + */ +const Search = { + _index: null, + _queued_query: null, + _pulse_status: -1, + + htmlToText: (htmlString) => { + const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html'); + htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() }); + const docContent = htmlElement.querySelector('[role="main"]'); + if (docContent !== undefined) return docContent.textContent; + console.warn( + "Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template." + ); + return ""; + }, + + init: () => { + const query = new URLSearchParams(window.location.search).get("q"); + const training = new URLSearchParams(window.location.search).get("training"); + const select = document + .querySelector('select[name="training"]') + document + .querySelectorAll('input[name="q"]') + .forEach((el) => (el.value = query)); + if (training) select.value = training; + else select.value = "all"; + if (query) Search.performSearch(query, training); + }, + + loadIndex: (url) => + (document.body.appendChild(document.createElement("script")).src = url), + + setIndex: (index) => { + Search._index = index; + if (Search._queued_query !== null) { + const query = Search._queued_query; + Search._queued_query = null; + Search.query(query); + } + }, + + hasIndex: () => Search._index !== null, + + deferQuery: (query) => (Search._queued_query = query), + + stopPulse: () => (Search._pulse_status = -1), + + startPulse: () => { + if (Search._pulse_status >= 0) return; + + const pulse = () => { + Search._pulse_status = (Search._pulse_status + 1) % 4; + Search.dots.innerText = ".".repeat(Search._pulse_status); + if (Search._pulse_status >= 0) window.setTimeout(pulse, 500); + }; + pulse(); + }, + + /** + * perform a search for something (or wait until index is loaded) + */ + performSearch: (query, training) => { + // create the required interface elements + const searchText = document.createElement("h2"); + searchText.textContent = _("Searching"); + const searchSummary = document.createElement("p"); + searchSummary.classList.add("search-summary"); + searchSummary.innerText = ""; + const searchList = document.createElement("ul"); + searchList.classList.add("search"); + + const out = document.getElementById("search-results"); + Search.title = out.appendChild(searchText); + Search.dots = Search.title.appendChild(document.createElement("span")); + Search.status = out.appendChild(searchSummary); + Search.output = out.appendChild(searchList); + + const searchProgress = document.getElementById("search-progress"); + // Some themes don't use the search progress node + if (searchProgress) { + searchProgress.innerText = _("Preparing search..."); + } + Search.startPulse(); + + // index already loaded, the browser was quick! + if (Search.hasIndex()) Search.query(query, training); + else Search.deferQuery(query); + }, + + /** + * execute search (requires search index to be loaded) + */ + query: (query, training) => { + const filenames = Search._index.filenames; + const docNames = Search._index.docnames; + const titles = Search._index.titles; + const allTitles = Search._index.alltitles; + const indexEntries = Search._index.indexentries; + + // stem the search terms and add them to the correct list + const stemmer = new Stemmer(); + const searchTerms = new Set(); + const excludedTerms = new Set(); + const highlightTerms = new Set(); + const objectTerms = new Set(splitQuery(query.toLowerCase().trim())); + splitQuery(query.trim()).forEach((queryTerm) => { + const queryTermLower = queryTerm.toLowerCase(); + + // maybe skip this "word" + // stopwords array is from language_data.js + if ( + stopwords.indexOf(queryTermLower) !== -1 || + queryTerm.match(/^\d+$/) + ) + return; + + // stem the word + let word = stemmer.stemWord(queryTermLower); + // select the correct list + if (word[0] === "-") excludedTerms.add(word.substr(1)); + else { + searchTerms.add(word); + highlightTerms.add(queryTermLower); + } + }); + + if (SPHINX_HIGHLIGHT_ENABLED) { // set in sphinx_highlight.js + localStorage.setItem("sphinx_highlight_terms", [...highlightTerms].join(" ")) + } + + // array of [docname, title, anchor, descr, score, filename] + let results = []; + _removeChildren(document.getElementById("search-progress")); + + // query matches title + const queryLower = query.toLowerCase(); + for (const [title, foundTitles] of Object.entries(allTitles)) { + if (title.toLowerCase().includes(queryLower) && (queryLower.length >= title.length/2)) { + for (const [file, id] of foundTitles) { + let score = Math.round(100 * queryLower.length / title.length) + results.push([ + docNames[file], + titles[file] !== title ? `${titles[file]} > ${title}` : title, + id !== null ? "#" + id : "", + null, + score, + filenames[file], + _getParentTitles(file, docNames, titles), + ]); + } + } + } + + // search for explicit entries in index directives + for (const [entry, foundEntries] of Object.entries(indexEntries)) { + if (entry.includes(queryLower) && (queryLower.length >= entry.length/2)) { + for (const [file, id] of foundEntries) { + let score = Math.round(100 * queryLower.length / entry.length) + results.push([ + docNames[file], + titles[file], + id ? "#" + id : "", + null, + score, + filenames[file], + ]); + } + } + } + + // lookup as object + objectTerms.forEach((term) => + results.push(...Search.performObjectSearch(term, objectTerms)) + ); + + // lookup as search terms in fulltext + results.push(...Search.performTermsSearch(searchTerms, excludedTerms)); + + // let the scorer override scores with a custom scoring function + if (Scorer.score) results.forEach((item) => (item[4] = Scorer.score(item))); + + // Filter results by training + if (training && training !== 'all') { + results = results.filter(result => { + let condition = result[0].split('/')[0] === training; + return condition + }) + } + + // now sort the results by score (in opposite order of appearance, since the + // display function below uses pop() to retrieve items) and then + // alphabetically + results.sort((a, b) => { + const leftScore = a[4]; + const rightScore = b[4]; + if (leftScore === rightScore) { + // same score: sort alphabetically + const leftTitle = a[1].toLowerCase(); + const rightTitle = b[1].toLowerCase(); + if (leftTitle === rightTitle) return 0; + return leftTitle > rightTitle ? -1 : 1; // inverted is intentional + } + return leftScore > rightScore ? 1 : -1; + }); + + // remove duplicate search results + // note the reversing of results, so that in the case of duplicates, the highest-scoring entry is kept + let seen = new Set(); + results = results.reverse().reduce((acc, result) => { + let resultStr = result.slice(0, 4).concat([result[5]]).map(v => String(v)).join(','); + if (!seen.has(resultStr)) { + acc.push(result); + seen.add(resultStr); + } + return acc; + }, []); + + results = results.reverse(); + + // print the results + _displayNextItem(results, results.length, searchTerms); + }, + + /** + * search for object names + */ + performObjectSearch: (object, objectTerms) => { + const filenames = Search._index.filenames; + const docNames = Search._index.docnames; + const objects = Search._index.objects; + const objNames = Search._index.objnames; + const titles = Search._index.titles; + + const results = []; + + const objectSearchCallback = (prefix, match) => { + const name = match[4] + const fullname = (prefix ? prefix + "." : "") + name; + const fullnameLower = fullname.toLowerCase(); + if (fullnameLower.indexOf(object) < 0) return; + + let score = 0; + const parts = fullnameLower.split("."); + + // check for different match types: exact matches of full name or + // "last name" (i.e. last dotted part) + if (fullnameLower === object || parts.slice(-1)[0] === object) + score += Scorer.objNameMatch; + else if (parts.slice(-1)[0].indexOf(object) > -1) + score += Scorer.objPartialMatch; // matches in last name + + const objName = objNames[match[1]][2]; + const title = titles[match[0]]; + + // If more than one term searched for, we require other words to be + // found in the name/title/description + const otherTerms = new Set(objectTerms); + otherTerms.delete(object); + if (otherTerms.size > 0) { + const haystack = `${prefix} ${name} ${objName} ${title}`.toLowerCase(); + if ( + [...otherTerms].some((otherTerm) => haystack.indexOf(otherTerm) < 0) + ) + return; + } + + let anchor = match[3]; + if (anchor === "") anchor = fullname; + else if (anchor === "-") anchor = objNames[match[1]][1] + "-" + fullname; + + const descr = objName + _(", in ") + title; + + // add custom score for some objects according to scorer + if (Scorer.objPrio.hasOwnProperty(match[2])) + score += Scorer.objPrio[match[2]]; + else score += Scorer.objPrioDefault; + + results.push([ + docNames[match[0]], + fullname, + "#" + anchor, + descr, + score, + filenames[match[0]], + ]); + }; + Object.keys(objects).forEach((prefix) => + objects[prefix].forEach((array) => + objectSearchCallback(prefix, array) + ) + ); + return results; + }, + + /** + * search for full-text terms in the index + */ + performTermsSearch: (searchTerms, excludedTerms) => { + // prepare search + const terms = Search._index.terms; + const titleTerms = Search._index.titleterms; + const filenames = Search._index.filenames; + const docNames = Search._index.docnames; + const titles = Search._index.titles; + + const scoreMap = new Map(); + const fileMap = new Map(); + + + // perform the search on the required terms + searchTerms.forEach((word) => { + const files = []; + const arr = [ + { files: terms[word], score: Scorer.term }, + { files: titleTerms[word], score: Scorer.title }, + ]; + // add support for partial matches + if (word.length > 2) { + const escapedWord = _escapeRegExp(word); + Object.keys(terms).forEach((term) => { + if (term.match(escapedWord) && !terms[word]) + arr.push({ files: terms[term], score: Scorer.partialTerm }); + }); + Object.keys(titleTerms).forEach((term) => { + if (term.match(escapedWord) && !titleTerms[word]) + arr.push({ files: titleTerms[word], score: Scorer.partialTitle }); + }); + } + + // no match but word was a required one + if (arr.every((record) => record.files === undefined)) return; + + // found search word in contents + arr.forEach((record) => { + if (record.files === undefined) return; + + let recordFiles = record.files; + if (recordFiles.length === undefined) recordFiles = [recordFiles]; + files.push(...recordFiles); + + // set score for the word in each file + recordFiles.forEach((file) => { + if (!scoreMap.has(file)) scoreMap.set(file, {}); + scoreMap.get(file)[word] = record.score; + }); + }); + + // create the mapping + files.forEach((file) => { + if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1) + fileMap.get(file).push(word); + else fileMap.set(file, [word]); + }); + }); + + // now check if the files don't contain excluded terms + const results = []; + for (const [file, wordList] of fileMap) { + // check if all requirements are matched + + // as search terms with length < 3 are discarded + const filteredTermCount = [...searchTerms].filter( + (term) => term.length > 2 + ).length; + if ( + wordList.length !== searchTerms.size && + wordList.length !== filteredTermCount + ) + continue; + + // ensure that none of the excluded terms is in the search result + if ( + [...excludedTerms].some( + (term) => + terms[term] === file || + titleTerms[term] === file || + (terms[term] || []).includes(file) || + (titleTerms[term] || []).includes(file) + ) + ) + break; + + // select one (max) score for the file. + const score = Math.max(...wordList.map((w) => scoreMap.get(file)[w])); + + // add result to the result list + results.push([ + docNames[file], + titles[file], + "", + null, + score, + filenames[file], + _getParentTitles(file, docNames, titles) + ]); + } + return results; + }, + + /** + * helper function to return a node containing the + * search summary for a given text. keywords is a list + * of stemmed words. + */ + makeSearchSummary: (htmlText, keywords) => { + const text = Search.htmlToText(htmlText); + if (text === "") return null; + + const textLower = text.toLowerCase(); + const actualStartPosition = [...keywords] + .map((k) => textLower.indexOf(k.toLowerCase())) + .filter((i) => i > -1) + .slice(-1)[0]; + const startWithContext = Math.max(actualStartPosition - 120, 0); + + const top = startWithContext === 0 ? "" : "..."; + const tail = startWithContext + 240 < text.length ? "..." : ""; + + let summary = document.createElement("p"); + summary.classList.add("context"); + summary.textContent = top + text.substr(startWithContext, 240).trim() + tail; + + return summary; + }, +}; + +_ready(Search.init); diff --git a/docs/_templates/components/search-field.html b/docs/_templates/components/search-field.html new file mode 100644 index 0000000000..5c55819447 --- /dev/null +++ b/docs/_templates/components/search-field.html @@ -0,0 +1,42 @@ +{# A bootstrap-styled field that will direct to the `search.html` page when submitted #} + diff --git a/docs/_templates/search.html b/docs/_templates/search.html new file mode 100644 index 0000000000..c17e5f1b50 --- /dev/null +++ b/docs/_templates/search.html @@ -0,0 +1,42 @@ +{%- extends "page.html" %} +{# Over-ride the body to be custom search structure we want #} +{% block docs_body %} +
+

{{ _("Search") }}

+ + {% block searchtext %} +
+ {% trans %}Searching for multiple words only shows matches that contain + all words.{% endtrans %} +
+ {% endblock %} + {% include "components/search-field.html" %} +
+
+ +{% endblock docs_body %} +{# Below sections just re-create the behavior of Sphinx default search #} +{# Page metadata #} +{%- block htmltitle -%} + {{ _("Search") }} - {{ title or docstitle }} +{%- endblock htmltitle -%} +{# Manually include the search JS that Sphinx includes #} +{% block scripts -%} + {{ super() }} + + + +{%- endblock scripts %} From dc241f5304e973f98a4eecca10f8956006b8ea27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sat, 15 Mar 2025 19:03:52 +0100 Subject: [PATCH 2/7] Change label: training -> documentation --- docs/_static/searchtools.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_static/searchtools.js b/docs/_static/searchtools.js index f420560dda..86ce259ec8 100644 --- a/docs/_static/searchtools.js +++ b/docs/_static/searchtools.js @@ -10,7 +10,7 @@ */ "use strict"; -var title_repository = 'Plone training'; +var title_repository = 'Plone documentation'; /** From ec5ecb1b3c06e47b0f4e3eeef1966116d9de9bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sat, 15 Mar 2025 19:04:32 +0100 Subject: [PATCH 3/7] fix search filter: plone.restapi and plone.api --- docs/_templates/components/search-field.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/_templates/components/search-field.html b/docs/_templates/components/search-field.html index 5c55819447..fb586f0667 100644 --- a/docs/_templates/components/search-field.html +++ b/docs/_templates/components/search-field.html @@ -29,8 +29,9 @@ ['deployment', 'Deployment'], ['volto', 'Volto UI'], ['classic-ui', 'Classic UI'], - ['plone.restapi/docs/source', 'REST API'], ['backend', 'Backend'], + ['plone.restapi', 'REST API'], + ['plone.api', 'plone.api'], ['i18n-l10n', 'Internationalization and Localization'], ['conceptual-guides', 'Conceptual guides'], ['contributing', 'Contributing to Plone'], From b103af361010120ea933899a4cf36dc1ac8e8233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20S=C3=BCss?= Date: Sun, 16 Mar 2025 14:49:22 +0100 Subject: [PATCH 4/7] breadcrumbs: space via html Co-authored-by: Steve Piercy --- docs/_static/searchtools.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_static/searchtools.js b/docs/_static/searchtools.js index 86ce259ec8..4d5590fb70 100644 --- a/docs/_static/searchtools.js +++ b/docs/_static/searchtools.js @@ -109,7 +109,7 @@ function _getBreadcrumbs(item, linkUrl) { return `${el.title}` }) markup.push(`${item[1]}`) - return markup.join('>'); + return markup.join(' > '); } const _displayItem = (item, searchTerms) => { From 6a1e5a4612fbd89370a8af53f68e8d4bb918e0ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sun, 16 Mar 2025 15:23:15 +0100 Subject: [PATCH 5/7] Rip out search filter by category --- docs/_templates/components/search-field.html | 43 -------------------- 1 file changed, 43 deletions(-) delete mode 100644 docs/_templates/components/search-field.html diff --git a/docs/_templates/components/search-field.html b/docs/_templates/components/search-field.html deleted file mode 100644 index fb586f0667..0000000000 --- a/docs/_templates/components/search-field.html +++ /dev/null @@ -1,43 +0,0 @@ -{# A bootstrap-styled field that will direct to the `search.html` page when submitted #} - From 6829a944151908b1e998c6698ec983b5d2e84ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sun, 16 Mar 2025 15:23:23 +0100 Subject: [PATCH 6/7] Update searchtools.js --- docs/_static/searchtools.js | 337 ++++++++++++++++++------------------ 1 file changed, 164 insertions(+), 173 deletions(-) diff --git a/docs/_static/searchtools.js b/docs/_static/searchtools.js index 4d5590fb70..91f4be57fc 100644 --- a/docs/_static/searchtools.js +++ b/docs/_static/searchtools.js @@ -1,41 +1,8 @@ /* - * searchtools.js - * ~~~~~~~~~~~~~~~~ - * * Sphinx JavaScript utilities for the full-text search. - * - * :copyright: Copyright 2007-2023 by the Sphinx team, see AUTHORS. - * :license: BSD, see LICENSE for details. - * */ "use strict"; -var title_repository = 'Plone documentation'; - - -/** - * Return array with titles of ancestors of file. - * @param {number} idx - The index of the result item in global list of files - * @returns array - */ -function _getParentTitles(idx, docNames, titles) { - let path = docNames[idx] - let parentpathtokens = path.split('/').slice(0, -1); - - let parentTitles = parentpathtokens.map((el, index) => { - let foo = `${parentpathtokens.slice(0, index+1).join('/')}` - let parentId = docNames.indexOf(foo); - if (parentId === -1) { - foo = `${parentpathtokens.slice(0, index+1).join('/')}/index` - parentId = docNames.indexOf(foo); - } - let title = parentId === -1 ? title_repository : titles[parentId]; - return title - }) - - return parentTitles -} - /** * Simple result scoring code. */ @@ -46,7 +13,7 @@ if (typeof Scorer === "undefined") { // and returns the new score. /* score: result => { - const [docname, title, anchor, descr, score, filename] = result + const [docname, title, anchor, descr, score, filename, kind] = result return score }, */ @@ -73,6 +40,14 @@ if (typeof Scorer === "undefined") { }; } +// Global search result kind enum, used by themes to style search results. +class SearchResultKind { + static get index() { return "index"; } + static get object() { return "object"; } + static get text() { return "text"; } + static get title() { return "title"; } +} + const _removeChildren = (element) => { while (element && element.lastChild) element.removeChild(element.lastChild); }; @@ -83,45 +58,20 @@ const _removeChildren = (element) => { const _escapeRegExp = (string) => string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string - -function _getBreadcrumbs(item, linkUrl) { - // No breadcrumbs for top level pages - if (item[0].split('/')[1] == 'index') { - return null - } - let parentTitles = item[6]; - - let parentDefaultTitles = [ - "Plone trainings", - "Training for Plone developers" - ]; - parentTitles = Array.isArray(parentTitles) ? parentTitles : parentDefaultTitles; - let pathTokens = item[0].split('/') - .slice(0, -1); - let pathArray = pathTokens.map((el, index) => { - return { - "path": pathTokens.slice(0, index+1).join('/'), - "title": parentTitles[index] - } - }) - let markup = pathArray - .map((el, idx) => { - return `${el.title}` - }) - markup.push(`${item[1]}`) - return markup.join(' > '); -} - -const _displayItem = (item, searchTerms) => { +const _displayItem = (item, searchTerms, highlightTerms) => { const docBuilder = DOCUMENTATION_OPTIONS.BUILDER; - const docUrlRoot = DOCUMENTATION_OPTIONS.URL_ROOT; const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX; const docLinkSuffix = DOCUMENTATION_OPTIONS.LINK_SUFFIX; const showSearchSummary = DOCUMENTATION_OPTIONS.SHOW_SEARCH_SUMMARY; + const contentRoot = document.documentElement.dataset.content_root; - const [docName, title, anchor, descr, score, _filename] = item; + const [docName, title, anchor, descr, score, _filename, kind] = item; let listItem = document.createElement("li"); + // Add a class representing the item's type: + // can be used by a theme's CSS selector for styling + // See SearchResultKind for the class names. + listItem.classList.add(`kind-${kind}`); let requestUrl; let linkUrl; if (docBuilder === "dirhtml") { @@ -130,42 +80,38 @@ const _displayItem = (item, searchTerms) => { if (dirname.match(/\/index\/$/)) dirname = dirname.substring(0, dirname.length - 6); else if (dirname === "index/") dirname = ""; - requestUrl = docUrlRoot + dirname; + requestUrl = contentRoot + dirname; linkUrl = requestUrl; } else { // normal html builders - requestUrl = docUrlRoot + docName + docFileSuffix; + requestUrl = contentRoot + docName + docFileSuffix; linkUrl = docName + docLinkSuffix; } - - let breadcrumbs = _getBreadcrumbs(item, linkUrl); - if (breadcrumbs) { - let breadcrumbsNode = document.createElement("div"); - breadcrumbsNode.innerHTML = breadcrumbs; - breadcrumbsNode.classList.add("breadcrumbs"); - listItem.appendChild(breadcrumbsNode); - } - - // Title links to chapter let linkEl = listItem.appendChild(document.createElement("a")); linkEl.href = linkUrl + anchor; linkEl.dataset.score = score; linkEl.innerHTML = title; - if (descr) + if (descr) { listItem.appendChild(document.createElement("span")).innerHTML = " (" + descr + ")"; + // highlight search terms in the description + if (SPHINX_HIGHLIGHT_ENABLED) // set in sphinx_highlight.js + highlightTerms.forEach((term) => _highlightText(listItem, term, "highlighted")); + } else if (showSearchSummary) fetch(requestUrl) .then((responseData) => responseData.text()) .then((data) => { if (data) listItem.appendChild( - Search.makeSearchSummary(data, searchTerms) + Search.makeSearchSummary(data, searchTerms, anchor) ); + // highlight search terms in the summary + if (SPHINX_HIGHLIGHT_ENABLED) // set in sphinx_highlight.js + highlightTerms.forEach((term) => _highlightText(listItem, term, "highlighted")); }); Search.output.appendChild(listItem); }; - const _finishSearch = (resultCount) => { Search.stopPulse(); Search.title.innerText = _("Search Results"); @@ -174,29 +120,46 @@ const _finishSearch = (resultCount) => { "Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories." ); else - // Search.status.innerText = _( - // `Search finished, found ${resultCount} page(s) matching the search query.` - // ); - Search.status.innerText = `${resultCount} page(s) found.` + Search.status.innerText = Documentation.ngettext( + "Search finished, found one page matching the search query.", + "Search finished, found ${resultCount} pages matching the search query.", + resultCount, + ).replace('${resultCount}', resultCount); }; - const _displayNextItem = ( results, resultCount, - searchTerms + searchTerms, + highlightTerms, ) => { // results left, load the summary and display it // this is intended to be dynamic (don't sub resultsCount) if (results.length) { - _displayItem(results.pop(), searchTerms); + _displayItem(results.pop(), searchTerms, highlightTerms); setTimeout( - () => _displayNextItem(results, resultCount, searchTerms), + () => _displayNextItem(results, resultCount, searchTerms, highlightTerms), 5 ); } // search finished, update title and status message else _finishSearch(resultCount); }; +// Helper function used by query() to order search results. +// Each input is an array of [docname, title, anchor, descr, score, filename, kind]. +// Order the results by score (in opposite order of appearance, since the +// `_displayNextItem` function uses pop() to retrieve items) and then alphabetically. +const _orderResultsByScoreThenName = (a, b) => { + const leftScore = a[4]; + const rightScore = b[4]; + if (leftScore === rightScore) { + // same score: sort alphabetically + const leftTitle = a[1].toLowerCase(); + const rightTitle = b[1].toLowerCase(); + if (leftTitle === rightTitle) return 0; + return leftTitle > rightTitle ? -1 : 1; // inverted is intentional + } + return leftScore > rightScore ? 1 : -1; +}; /** * Default splitQuery function. Can be overridden in ``sphinx.search`` with a @@ -220,28 +183,36 @@ const Search = { _queued_query: null, _pulse_status: -1, - htmlToText: (htmlString) => { + htmlToText: (htmlString, anchor) => { const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html'); - htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() }); + for (const removalQuery of [".headerlink", "script", "style"]) { + htmlElement.querySelectorAll(removalQuery).forEach((el) => { el.remove() }); + } + if (anchor) { + const anchorContent = htmlElement.querySelector(`[role="main"] ${anchor}`); + if (anchorContent) return anchorContent.textContent; + + console.warn( + `Anchored content block not found. Sphinx search tries to obtain it via DOM query '[role=main] ${anchor}'. Check your theme or template.` + ); + } + + // if anchor not specified or not found, fall back to main content const docContent = htmlElement.querySelector('[role="main"]'); - if (docContent !== undefined) return docContent.textContent; + if (docContent) return docContent.textContent; + console.warn( - "Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template." + "Content block not found. Sphinx search tries to obtain it via DOM query '[role=main]'. Check your theme or template." ); return ""; }, init: () => { const query = new URLSearchParams(window.location.search).get("q"); - const training = new URLSearchParams(window.location.search).get("training"); - const select = document - .querySelector('select[name="training"]') document .querySelectorAll('input[name="q"]') .forEach((el) => (el.value = query)); - if (training) select.value = training; - else select.value = "all"; - if (query) Search.performSearch(query, training); + if (query) Search.performSearch(query); }, loadIndex: (url) => @@ -276,7 +247,7 @@ const Search = { /** * perform a search for something (or wait until index is loaded) */ - performSearch: (query, training) => { + performSearch: (query) => { // create the required interface elements const searchText = document.createElement("h2"); searchText.textContent = _("Searching"); @@ -284,6 +255,7 @@ const Search = { searchSummary.classList.add("search-summary"); searchSummary.innerText = ""; const searchList = document.createElement("ul"); + searchList.setAttribute("role", "list"); searchList.classList.add("search"); const out = document.getElementById("search-results"); @@ -300,20 +272,11 @@ const Search = { Search.startPulse(); // index already loaded, the browser was quick! - if (Search.hasIndex()) Search.query(query, training); + if (Search.hasIndex()) Search.query(query); else Search.deferQuery(query); }, - /** - * execute search (requires search index to be loaded) - */ - query: (query, training) => { - const filenames = Search._index.filenames; - const docNames = Search._index.docnames; - const titles = Search._index.titles; - const allTitles = Search._index.alltitles; - const indexEntries = Search._index.indexentries; - + _parseQuery: (query) => { // stem the search terms and add them to the correct list const stemmer = new Stemmer(); const searchTerms = new Set(); @@ -345,24 +308,44 @@ const Search = { localStorage.setItem("sphinx_highlight_terms", [...highlightTerms].join(" ")) } - // array of [docname, title, anchor, descr, score, filename] - let results = []; + // console.debug("SEARCH: searching for:"); + // console.info("required: ", [...searchTerms]); + // console.info("excluded: ", [...excludedTerms]); + + return [query, searchTerms, excludedTerms, highlightTerms, objectTerms]; + }, + + /** + * execute search (requires search index to be loaded) + */ + _performSearch: (query, searchTerms, excludedTerms, highlightTerms, objectTerms) => { + const filenames = Search._index.filenames; + const docNames = Search._index.docnames; + const titles = Search._index.titles; + const allTitles = Search._index.alltitles; + const indexEntries = Search._index.indexentries; + + // Collect multiple result groups to be sorted separately and then ordered. + // Each is an array of [docname, title, anchor, descr, score, filename, kind]. + const normalResults = []; + const nonMainIndexResults = []; + _removeChildren(document.getElementById("search-progress")); - // query matches title - const queryLower = query.toLowerCase(); + const queryLower = query.toLowerCase().trim(); for (const [title, foundTitles] of Object.entries(allTitles)) { - if (title.toLowerCase().includes(queryLower) && (queryLower.length >= title.length/2)) { + if (title.toLowerCase().trim().includes(queryLower) && (queryLower.length >= title.length/2)) { for (const [file, id] of foundTitles) { - let score = Math.round(100 * queryLower.length / title.length) - results.push([ + const score = Math.round(Scorer.title * queryLower.length / title.length); + const boost = titles[file] === title ? 1 : 0; // add a boost for document titles + normalResults.push([ docNames[file], titles[file] !== title ? `${titles[file]} > ${title}` : title, id !== null ? "#" + id : "", null, - score, + score + boost, filenames[file], - _getParentTitles(file, docNames, titles), + SearchResultKind.title, ]); } } @@ -371,54 +354,48 @@ const Search = { // search for explicit entries in index directives for (const [entry, foundEntries] of Object.entries(indexEntries)) { if (entry.includes(queryLower) && (queryLower.length >= entry.length/2)) { - for (const [file, id] of foundEntries) { - let score = Math.round(100 * queryLower.length / entry.length) - results.push([ + for (const [file, id, isMain] of foundEntries) { + const score = Math.round(100 * queryLower.length / entry.length); + const result = [ docNames[file], titles[file], id ? "#" + id : "", null, score, filenames[file], - ]); + SearchResultKind.index, + ]; + if (isMain) { + normalResults.push(result); + } else { + nonMainIndexResults.push(result); + } } } } // lookup as object objectTerms.forEach((term) => - results.push(...Search.performObjectSearch(term, objectTerms)) + normalResults.push(...Search.performObjectSearch(term, objectTerms)) ); // lookup as search terms in fulltext - results.push(...Search.performTermsSearch(searchTerms, excludedTerms)); + normalResults.push(...Search.performTermsSearch(searchTerms, excludedTerms)); // let the scorer override scores with a custom scoring function - if (Scorer.score) results.forEach((item) => (item[4] = Scorer.score(item))); - - // Filter results by training - if (training && training !== 'all') { - results = results.filter(result => { - let condition = result[0].split('/')[0] === training; - return condition - }) + if (Scorer.score) { + normalResults.forEach((item) => (item[4] = Scorer.score(item))); + nonMainIndexResults.forEach((item) => (item[4] = Scorer.score(item))); } - - // now sort the results by score (in opposite order of appearance, since the - // display function below uses pop() to retrieve items) and then - // alphabetically - results.sort((a, b) => { - const leftScore = a[4]; - const rightScore = b[4]; - if (leftScore === rightScore) { - // same score: sort alphabetically - const leftTitle = a[1].toLowerCase(); - const rightTitle = b[1].toLowerCase(); - if (leftTitle === rightTitle) return 0; - return leftTitle > rightTitle ? -1 : 1; // inverted is intentional - } - return leftScore > rightScore ? 1 : -1; - }); + + // Sort each group of results by score and then alphabetically by name. + normalResults.sort(_orderResultsByScoreThenName); + nonMainIndexResults.sort(_orderResultsByScoreThenName); + + // Combine the result groups in (reverse) order. + // Non-main index entries are typically arbitrary cross-references, + // so display them after other results. + let results = [...nonMainIndexResults, ...normalResults]; // remove duplicate search results // note the reversing of results, so that in the case of duplicates, the highest-scoring entry is kept @@ -432,10 +409,19 @@ const Search = { return acc; }, []); - results = results.reverse(); + return results.reverse(); + }, + + query: (query) => { + const [searchQuery, searchTerms, excludedTerms, highlightTerms, objectTerms] = Search._parseQuery(query); + const results = Search._performSearch(searchQuery, searchTerms, excludedTerms, highlightTerms, objectTerms); + + // for debugging + //Search.lastresults = results.slice(); // a copy + // console.info("search results:", Search.lastresults); // print the results - _displayNextItem(results, results.length, searchTerms); + _displayNextItem(results, results.length, searchTerms, highlightTerms); }, /** @@ -499,6 +485,7 @@ const Search = { descr, score, filenames[match[0]], + SearchResultKind.object, ]); }; Object.keys(objects).forEach((prefix) => @@ -523,25 +510,30 @@ const Search = { const scoreMap = new Map(); const fileMap = new Map(); - // perform the search on the required terms searchTerms.forEach((word) => { const files = []; + // find documents, if any, containing the query word in their text/title term indices + // use Object.hasOwnProperty to avoid mismatching against prototype properties const arr = [ - { files: terms[word], score: Scorer.term }, - { files: titleTerms[word], score: Scorer.title }, + { files: terms.hasOwnProperty(word) ? terms[word] : undefined, score: Scorer.term }, + { files: titleTerms.hasOwnProperty(word) ? titleTerms[word] : undefined, score: Scorer.title }, ]; // add support for partial matches if (word.length > 2) { const escapedWord = _escapeRegExp(word); - Object.keys(terms).forEach((term) => { - if (term.match(escapedWord) && !terms[word]) - arr.push({ files: terms[term], score: Scorer.partialTerm }); - }); - Object.keys(titleTerms).forEach((term) => { - if (term.match(escapedWord) && !titleTerms[word]) - arr.push({ files: titleTerms[word], score: Scorer.partialTitle }); - }); + if (!terms.hasOwnProperty(word)) { + Object.keys(terms).forEach((term) => { + if (term.match(escapedWord)) + arr.push({ files: terms[term], score: Scorer.partialTerm }); + }); + } + if (!titleTerms.hasOwnProperty(word)) { + Object.keys(titleTerms).forEach((term) => { + if (term.match(escapedWord)) + arr.push({ files: titleTerms[term], score: Scorer.partialTitle }); + }); + } } // no match but word was a required one @@ -557,16 +549,16 @@ const Search = { // set score for the word in each file recordFiles.forEach((file) => { - if (!scoreMap.has(file)) scoreMap.set(file, {}); - scoreMap.get(file)[word] = record.score; + if (!scoreMap.has(file)) scoreMap.set(file, new Map()); + const fileScores = scoreMap.get(file); + fileScores.set(word, record.score); }); }); // create the mapping files.forEach((file) => { - if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1) - fileMap.get(file).push(word); - else fileMap.set(file, [word]); + if (!fileMap.has(file)) fileMap.set(file, [word]); + else if (fileMap.get(file).indexOf(word) === -1) fileMap.get(file).push(word); }); }); @@ -598,8 +590,7 @@ const Search = { break; // select one (max) score for the file. - const score = Math.max(...wordList.map((w) => scoreMap.get(file)[w])); - + const score = Math.max(...wordList.map((w) => scoreMap.get(file).get(w))); // add result to the result list results.push([ docNames[file], @@ -608,7 +599,7 @@ const Search = { null, score, filenames[file], - _getParentTitles(file, docNames, titles) + SearchResultKind.text, ]); } return results; @@ -619,8 +610,8 @@ const Search = { * search summary for a given text. keywords is a list * of stemmed words. */ - makeSearchSummary: (htmlText, keywords) => { - const text = Search.htmlToText(htmlText); + makeSearchSummary: (htmlText, keywords, anchor) => { + const text = Search.htmlToText(htmlText, anchor); if (text === "") return null; const textLower = text.toLowerCase(); From 309681a60d2e3a3466992509dd635c11a45f4126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sun, 16 Mar 2025 16:21:52 +0100 Subject: [PATCH 7/7] Add breadcrumbs to search results --- docs/_static/searchtools.js | 72 ++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/docs/_static/searchtools.js b/docs/_static/searchtools.js index 91f4be57fc..e6a1aafb7f 100644 --- a/docs/_static/searchtools.js +++ b/docs/_static/searchtools.js @@ -3,6 +3,31 @@ */ "use strict"; +var title_repository = 'Plone documentation'; + +/** + * Return array with titles of ancestors of file. + * @param {number} idx - The index of the result item in global list of files + * @returns array + */ +function _getParentTitles(idx, docNames, titles) { + let path = docNames[idx] + let parentpathtokens = path.split('/').slice(0, -1); + + let parentTitles = parentpathtokens.map((el, index) => { + let foo = `${parentpathtokens.slice(0, index+1).join('/')}` + let parentId = docNames.indexOf(foo); + if (parentId === -1) { + foo = `${parentpathtokens.slice(0, index+1).join('/')}/index` + parentId = docNames.indexOf(foo); + } + let title = parentId === -1 ? title_repository : titles[parentId]; + return title + }) + + return parentTitles +} + /** * Simple result scoring code. */ @@ -58,6 +83,34 @@ const _removeChildren = (element) => { const _escapeRegExp = (string) => string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string +function _getBreadcrumbs(item, linkUrl) { + // No breadcrumbs for top level pages + if (item[0].split('/')[1] == 'index') { + return null + } + let parentTitles = item[6]; + + let parentDefaultTitles = [ + "Plone documentation", + "Documentation for Plone developers" + ]; + parentTitles = Array.isArray(parentTitles) ? parentTitles : parentDefaultTitles; + let pathTokens = item[0].split('/') + .slice(0, -1); + let pathArray = pathTokens.map((el, index) => { + return { + "path": pathTokens.slice(0, index+1).join('/'), + "title": parentTitles[index] + } + }) + let markup = pathArray + .map((el, idx) => { + return `${el.title}` + }) + markup.push(`${item[1]}`) + return markup.join(' > '); +} + const _displayItem = (item, searchTerms, highlightTerms) => { const docBuilder = DOCUMENTATION_OPTIONS.BUILDER; const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX; @@ -71,7 +124,7 @@ const _displayItem = (item, searchTerms, highlightTerms) => { // Add a class representing the item's type: // can be used by a theme's CSS selector for styling // See SearchResultKind for the class names. - listItem.classList.add(`kind-${kind}`); + // listItem.classList.add(`kind-${kind}`); let requestUrl; let linkUrl; if (docBuilder === "dirhtml") { @@ -87,6 +140,16 @@ const _displayItem = (item, searchTerms, highlightTerms) => { requestUrl = contentRoot + docName + docFileSuffix; linkUrl = docName + docLinkSuffix; } + + let breadcrumbs = _getBreadcrumbs(item, linkUrl); + if (breadcrumbs) { + let breadcrumbsNode = document.createElement("div"); + breadcrumbsNode.innerHTML = breadcrumbs; + breadcrumbsNode.classList.add("breadcrumbs"); + listItem.appendChild(breadcrumbsNode); + } + + // Title links to chapter let linkEl = listItem.appendChild(document.createElement("a")); linkEl.href = linkUrl + anchor; linkEl.dataset.score = score; @@ -345,7 +408,7 @@ const Search = { null, score + boost, filenames[file], - SearchResultKind.title, + _getParentTitles(file, docNames, titles), ]); } } @@ -415,6 +478,7 @@ const Search = { query: (query) => { const [searchQuery, searchTerms, excludedTerms, highlightTerms, objectTerms] = Search._parseQuery(query); const results = Search._performSearch(searchQuery, searchTerms, excludedTerms, highlightTerms, objectTerms); + const qresults = results; // for debugging //Search.lastresults = results.slice(); // a copy @@ -598,8 +662,8 @@ const Search = { "", null, score, - filenames[file], - SearchResultKind.text, + filenames[file], + _getParentTitles(file, docNames, titles) ]); } return results;