From f38b307a669b582b8571fd2430b4454b49323c85 Mon Sep 17 00:00:00 2001 From: weareoutman Date: Thu, 19 May 2022 14:57:50 +0800 Subject: [PATCH] fix: fix indexing content between h1 and h2 Closes #154, #109 --- package.json | 8 +++- .../theme/SearchBar/SuggestionTemplate.ts | 2 +- src/server/utils/parseDocument.spec.ts | 5 ++ src/server/utils/parseDocument.ts | 45 ++++++++++++----- yarn.lock | 48 ++++++++++--------- 5 files changed, 71 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index db753cce..1d9a7860 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "license": "MIT", "dependencies": { "@docusaurus/utils": "^2.0.0-beta.4", + "@docusaurus/utils-common": "^2.0.0-beta.20", "@docusaurus/utils-validation": "^2.0.0-beta.4", "@easyops-cn/autocomplete.js": "^0.38.1", "@node-rs/jieba": "^1.6.0", @@ -38,7 +39,7 @@ "lunr": "^2.3.9", "lunr-languages": "^1.4.0", "mark.js": "^8.11.1", - "tslib": "^2.2.0" + "tslib": "^2.4.0" }, "devDependencies": { "@babel/core": "^7.12.3", @@ -79,6 +80,9 @@ "prettier": "^2.1.2", "rimraf": "^3.0.2", "standard-version": "^9.0.0", - "typescript": "^4.0.3" + "typescript": "^4.6.4" + }, + "resolutions": { + "joi": "17.6.0" } } diff --git a/src/client/theme/SearchBar/SuggestionTemplate.ts b/src/client/theme/SearchBar/SuggestionTemplate.ts index 9bc73e84..72942775 100644 --- a/src/client/theme/SearchBar/SuggestionTemplate.ts +++ b/src/client/theme/SearchBar/SuggestionTemplate.ts @@ -49,7 +49,7 @@ export function SuggestionTemplate({ !isInterOfTree && !isLastOfTree && explicitSearchResultPath; if (needsExplicitHitPath) { const pathItems = page - ? (page.b ?? []).concat(page.t).concat(document.s ?? []) + ? (page.b ?? []).concat(page.t).concat(!document.s || document.s === page.t ? [] : document.s) : document.b; wrapped.push( `${concatDocumentPath( diff --git a/src/server/utils/parseDocument.spec.ts b/src/server/utils/parseDocument.spec.ts index 8deaa7d7..c5522d65 100644 --- a/src/server/utils/parseDocument.spec.ts +++ b/src/server/utils/parseDocument.spec.ts @@ -51,6 +51,11 @@ describe("parseDocument", () => { { pageTitle: "Hello World", sections: [ + { + title: "Hello World", + hash: "", + content: "", + }, { title: "Hello fruits.", hash: "#hello-fruits", diff --git a/src/server/utils/parseDocument.ts b/src/server/utils/parseDocument.ts index 5b8bef7d..bd536f48 100644 --- a/src/server/utils/parseDocument.ts +++ b/src/server/utils/parseDocument.ts @@ -1,7 +1,9 @@ +import {blogPostContainerID} from '@docusaurus/utils-common'; import { ParsedDocument, ParsedDocumentSection } from "../../shared/interfaces"; import { getCondensedText } from "./getCondensedText"; const HEADINGS = "h1, h2, h3"; +// const SUB_HEADINGS = "h2, h3"; export function parseDocument($: cheerio.Root): ParsedDocument { const $pageTitle = $("article h1").first(); @@ -37,18 +39,39 @@ export function parseDocument($: cheerio.Root): ParsedDocument { const title = $h.contents().not("a.hash-link").text().trim(); const hash = $h.find("a.hash-link").attr("href") || ""; - let $sectionElements; - let $firstElement = $("article").children().first(); - if ($h.is($pageTitle) && $firstElement.filter("header").length > 0) { - $firstElement = $("article") - .children() // div.markdown, header - .not("header") // div.markdown - .children() // h1, p, p, h2, ... - .first(); // h1 || p - if ($firstElement.filter(HEADINGS).length > 0) { - return; + // Find all content between h1 and h2/h3, + // which is considered as the content section of page title. + let $sectionElements = $([]); + if ($h.is($pageTitle)) { + const $header = $h.parent(); + let $firstElement; + if ($header.is("header")) { + $firstElement = $header; + } else { + $firstElement = $h; + } + const blogPost = $(`#${blogPostContainerID}`); + if (blogPost.length) { + // Simplify blog post. + $firstElement = blogPost.children().first(); + $sectionElements = $firstElement.nextUntil(HEADINGS).addBack(); + } else { + const $nextElements = $firstElement.nextAll(); + const $headings = $nextElements.filter(HEADINGS); + if ($headings.length) { + $sectionElements = $firstElement.nextUntil(HEADINGS); + } else { + for (const next of $nextElements.get()) { + const $heading = $(next).find(HEADINGS); + if ($heading.length) { + $sectionElements = $sectionElements.add($heading.first().prevAll()); + break; + } else { + $sectionElements = $sectionElements.add(next); + } + } + } } - $sectionElements = $firstElement.nextUntil(HEADINGS).addBack(); } else { $sectionElements = $h.nextUntil(HEADINGS); } diff --git a/yarn.lock b/yarn.lock index b314fc60..f1132cd4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -973,6 +973,13 @@ webpack "^5.40.0" webpack-merge "^5.8.0" +"@docusaurus/utils-common@^2.0.0-beta.20": + version "2.0.0-beta.20" + resolved "https://registry.yarnpkg.com/@docusaurus/utils-common/-/utils-common-2.0.0-beta.20.tgz#adb914c331d711a3c0ef2ba3f64139acdf4cd781" + integrity sha512-HabHh23vOQn6ygs0PjuCSF/oZaNsYTFsxB2R6EwHNyw01nWgBC3QAcGVmyIWQhlb9p8V3byKgbzVS68hZX5t9A== + dependencies: + tslib "^2.4.0" + "@docusaurus/utils-validation@^2.0.0-beta.4": version "2.0.0-beta.4" resolved "https://registry.yarnpkg.com/@docusaurus/utils-validation/-/utils-validation-2.0.0-beta.4.tgz#417ff389d61aab4c6544f169e31bb86573b518df" @@ -1402,10 +1409,10 @@ "@nodelib/fs.scandir" "2.1.3" fastq "^1.6.0" -"@sideway/address@^4.1.0": - version "4.1.0" - resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.0.tgz#0b301ada10ac4e0e3fa525c90615e0b61a72b78d" - integrity sha512-wAH/JYRXeIFQRsxerIuLjgUu2Xszam+O5xKeatJ4oudShOOirfmsQ1D6LL54XOU2tizpCYku+s1wmU0SYdpoSA== +"@sideway/address@^4.1.3": + version "4.1.4" + resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0" + integrity sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw== dependencies: "@hapi/hoek" "^9.0.0" @@ -4473,14 +4480,14 @@ jest@^28.0.0: import-local "^3.0.2" jest-cli "^28.1.0" -joi@^17.4.0: - version "17.4.0" - resolved "https://registry.yarnpkg.com/joi/-/joi-17.4.0.tgz#b5c2277c8519e016316e49ababd41a1908d9ef20" - integrity sha512-F4WiW2xaV6wc1jxete70Rw4V/VuMd6IN+a5ilZsxG4uYtUXWu2kq9W5P2dz30e7Gmw8RCbY/u/uk+dMPma9tAg== +joi@17.6.0, joi@^17.4.0: + version "17.6.0" + resolved "https://registry.yarnpkg.com/joi/-/joi-17.6.0.tgz#0bb54f2f006c09a96e75ce687957bd04290054b2" + integrity sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw== dependencies: "@hapi/hoek" "^9.0.0" "@hapi/topo" "^5.0.0" - "@sideway/address" "^4.1.0" + "@sideway/address" "^4.1.3" "@sideway/formula" "^3.0.0" "@sideway/pinpoint" "^2.0.0" @@ -4775,12 +4782,7 @@ lodash.templatesettings@^4.0.0: dependencies: lodash._reinterpolate "^3.0.0" -lodash@^4.15.0, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20: - version "4.17.20" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" - integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA== - -lodash@^4.17.21: +lodash@^4.15.0, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21: version "4.17.21" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== @@ -6444,10 +6446,10 @@ tslib@^1.8.1, tslib@^1.9.0: resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== -tslib@^2.1.0, tslib@^2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.2.0.tgz#fb2c475977e35e241311ede2693cee1ec6698f5c" - integrity sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w== +tslib@^2.1.0, tslib@^2.2.0, tslib@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3" + integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ== tsutils@^3.21.0: version "3.21.0" @@ -6505,10 +6507,10 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= -typescript@^4.0.3: - version "4.0.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5" - integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg== +typescript@^4.6.4: + version "4.6.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9" + integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg== uglify-js@^3.1.4: version "3.11.2"