File tree Expand file tree Collapse file tree 2 files changed +52
-2
lines changed
docusaurus-search-local/src/server/utils Expand file tree Collapse file tree 2 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,16 @@ describe("scanDocuments", () => {
6969 hash : "" ,
7070 content : "" ,
7171 } ,
72+ {
73+ title : "Second heading" ,
74+ hash : "/2#second-heading" ,
75+ content : "Second content." ,
76+ } ,
77+ {
78+ title : "Third heading" ,
79+ hash : "/3#third-heading" ,
80+ content : "Third content." ,
81+ } ,
7282 ] ,
7383 breadcrumb : [ ] ,
7484 } ;
@@ -106,6 +116,13 @@ describe("scanDocuments", () => {
106116 "t": "First heading",
107117 "u": "/1",
108118 },
119+ Object {
120+ "h": "#second-heading",
121+ "i": 6,
122+ "p": 5,
123+ "t": "Second heading",
124+ "u": "/2",
125+ },
109126 ],
110127 Array [
111128 Object {
@@ -142,6 +159,14 @@ describe("scanDocuments", () => {
142159 "t": "First content.",
143160 "u": "/1",
144161 },
162+ Object {
163+ "h": "#second-heading",
164+ "i": 7,
165+ "p": 5,
166+ "s": "Second heading",
167+ "t": "Second content.",
168+ "u": "/2",
169+ },
145170 ],
146171 ]
147172 ` ) ;
Original file line number Diff line number Diff line change @@ -81,23 +81,33 @@ export async function scanDocuments(
8181 }
8282
8383 for ( const section of sections ) {
84+ const trimmedHash = getTrimmedHash ( section . hash , url ) ;
85+
8486 if ( section . title !== pageTitle ) {
87+ if ( trimmedHash === false ) {
88+ continue ;
89+ }
90+
8591 headingDocuments . push ( {
8692 i : getNextDocId ( ) ,
8793 t : section . title ,
8894 u : url ,
89- h : section . hash ,
95+ h : trimmedHash ,
9096 p : titleId ,
9197 } ) ;
9298 }
9399
94100 if ( section . content ) {
101+ if ( trimmedHash === false ) {
102+ continue ;
103+ }
104+
95105 contentDocuments . push ( {
96106 i : getNextDocId ( ) ,
97107 t : section . content ,
98108 s : section . title || pageTitle ,
99109 u : url ,
100- h : section . hash ,
110+ h : trimmedHash ,
101111 p : titleId ,
102112 } ) ;
103113 }
@@ -106,3 +116,18 @@ export async function scanDocuments(
106116 ) ;
107117 return allDocuments ;
108118}
119+
120+ function getTrimmedHash ( hash : string , url : string ) {
121+ if ( hash && ! hash . startsWith ( "#" ) && hash . includes ( "#" ) ) {
122+ // The hash link may contains URL path, we need to remove it.
123+ if ( hash . startsWith ( url ) && hash [ url . length ] === "#" ) {
124+ return hash . slice ( url . length ) ;
125+ }
126+
127+ // If the hash doesn't start with the URL, it's likely an external link.
128+ // Don't know this will happen or not, but just in case.
129+ return false ;
130+ }
131+
132+ return hash ;
133+ }
You can’t perform that action at this time.
0 commit comments