@@ -155,45 +155,52 @@ export class UrlPlugin {
155155 }
156156 this . oldLocationHash = urlStrPath ;
157157 }
158-
158+ /**
159+ * Get the hash out of the current URL. Also augments it with the text
160+ * from the main part of the URL, since that is not readable by JS
161+ * from the actual hash
162+ * @returns
163+ */
159164 getHash = ( ) => {
160- const text = window . location . search . match ( / (?< = [ & ? ] t e x t = ) [ ^ & ] * / ) ;
161- if ( text ) {
162- return `${ window . location . hash . slice ( 1 ) } :~:text=${ text [ 0 ] } ` ;
163- }
164- return window . location . hash . slice ( 1 ) ;
165+ const text = this . retrieveTextFragment ( window . location . search ) ;
166+ const textFragment = text ? `:~:text=${ text [ 0 ] } ` : '' ;
167+ return `${ window . location . hash . slice ( 1 ) } ${ textFragment } ` ;
165168 }
166169
167- /**
170+ /**
168171 * Get the url and check if it has changed
169172 * If it was changeed, update the urlState
170173 */
171- listenForHashChanges ( ) {
172- this . oldLocationHash = this . getHash ( ) ;
173- if ( this . urlLocationPollId ) {
174- clearInterval ( this . urlLocationPollId ) ;
175- this . urlLocationPollId = null ;
176- }
174+ listenForHashChanges ( ) {
175+ this . oldLocationHash = this . getHash ( ) ;
176+ if ( this . urlLocationPollId ) {
177+ clearInterval ( this . urlLocationPollId ) ;
178+ this . urlLocationPollId = null ;
179+ }
177180
178- // check if the URL changes
179- const updateHash = ( ) => {
180- const newFragment = this . getHash ( ) ;
181- const hasFragmentChange = newFragment != this . oldLocationHash ;
181+ // check if the URL changes
182+ const updateHash = ( ) => {
183+ const newFragment = this . getHash ( ) ;
184+ const hasFragmentChange = newFragment != this . oldLocationHash ;
182185
183- if ( ! hasFragmentChange ) { return ; }
186+ if ( ! hasFragmentChange ) { return ; }
184187
185- this . urlState = this . urlStringToUrlState ( newFragment ) ;
186- } ;
187- this . urlLocationPollId = setInterval ( updateHash , 500 ) ;
188- }
188+ this . urlState = this . urlStringToUrlState ( newFragment ) ;
189+ } ;
190+ this . urlLocationPollId = setInterval ( updateHash , 500 ) ;
191+ }
189192
190- /**
193+ /**
191194 * Will read either the hash or URL and return the bookreader fragment
192195 */
193- pullFromAddressBar ( location = window . location ) {
194- const path = this . urlMode === 'history'
195- ? ( location . pathname . substr ( this . urlHistoryBasePath . length ) + location . search )
196- : location . hash . substr ( 1 ) ;
197- this . urlState = this . urlStringToUrlState ( path ) ;
198- }
196+ pullFromAddressBar ( location = window . location ) {
197+ const path = this . urlMode === 'history'
198+ ? ( location . pathname . substr ( this . urlHistoryBasePath . length ) + location . search )
199+ : location . hash . substr ( 1 ) ;
200+ this . urlState = this . urlStringToUrlState ( path ) ;
201+ }
202+
203+ retrieveTextFragment ( urlString ) {
204+ return urlString . match ( / (?< = & t e x t = ) [ ^ & ] * / ) ;
205+ }
199206}
0 commit comments