Fixed search for use_directory_urls: false

This commit is contained in:
squidfunk 2020-02-29 10:47:01 +01:00
parent ff9ef6a4a7
commit 68c9cb6dac
9 changed files with 49 additions and 45 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"assets/javascripts/bundle.js": "assets/javascripts/bundle.55767211.min.js", "assets/javascripts/bundle.js": "assets/javascripts/bundle.63709496.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.55767211.min.js.map", "assets/javascripts/bundle.js.map": "assets/javascripts/bundle.63709496.min.js.map",
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.926ffd9e.min.js", "assets/javascripts/worker/search.js": "assets/javascripts/worker/search.926ffd9e.min.js",
"assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.926ffd9e.min.js.map", "assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.926ffd9e.min.js.map",
"assets/stylesheets/main.scss": "assets/stylesheets/main.0d8ec950.min.css", "assets/stylesheets/main.scss": "assets/stylesheets/main.0d8ec950.min.css",

View File

@ -195,7 +195,7 @@
{% endblock %} {% endblock %}
</div> </div>
{% block scripts %} {% block scripts %}
<script src="{{ 'assets/javascripts/bundle.55767211.min.js' | url }}"></script> <script src="{{ 'assets/javascripts/bundle.63709496.min.js' | url }}"></script>
{%- set translations = {} -%} {%- set translations = {} -%}
{%- for key in [ {%- for key in [
"clipboard.copy", "clipboard.copy",

View File

@ -52,7 +52,8 @@ import {
debounceTime, debounceTime,
distinctUntilKeyChanged, distinctUntilKeyChanged,
distinctUntilChanged, distinctUntilChanged,
bufferCount bufferCount,
startWith
} from "rxjs/operators" } from "rxjs/operators"
import { import {
@ -68,7 +69,8 @@ import {
useToggle, useToggle,
getElement, getElement,
setViewportOffset, setViewportOffset,
ViewportOffset ViewportOffset,
getLocation
} from "./observables" } from "./observables"
import { setupSearchWorker } from "./workers" import { setupSearchWorker } from "./workers"
@ -158,7 +160,7 @@ export function initialize(config: unknown) {
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
const worker = setupSearchWorker(config.worker.search, { const worker = setupSearchWorker(config.worker.search, {
base: config.base base: config.base, location$
}) })
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */

View File

@ -28,11 +28,10 @@ import {
pluck, pluck,
share, share,
skip, skip,
startWith,
switchMap switchMap
} from "rxjs/operators" } from "rxjs/operators"
import { getLocation, setLocation } from "../../location" import { setLocation } from "../../location"
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Helper types * Helper types
@ -52,7 +51,7 @@ interface WatchOptions {
/** /**
* Watch document switch * Watch document switch
* *
* This function returns an observables that fetches a document if the provided // TODO: update docs * This function returns an observables that fetches a document if the provided
* location observable emits a new value (i.e. URL). If the emitted URL points * location observable emits a new value (i.e. URL). If the emitted URL points
* to the same page, the request is effectively ignored (i.e. when only the * to the same page, the request is effectively ignored (i.e. when only the
* fragment identifier changes). * fragment identifier changes).
@ -68,7 +67,6 @@ export function watchDocumentSwitch(
): Observable<Document> { ): Observable<Document> {
return location$ return location$
.pipe( .pipe(
startWith(getLocation()),
distinctUntilKeyChanged("pathname"), distinctUntilKeyChanged("pathname"),
skip(1), skip(1),

View File

@ -20,7 +20,7 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Subject } from "rxjs" import { BehaviorSubject } from "rxjs"
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Functions * Functions
@ -56,6 +56,6 @@ export function setLocation(url: URL): void {
* *
* @return Location subject * @return Location subject
*/ */
export function watchLocation(): Subject<URL> { export function watchLocation(): BehaviorSubject<URL> {
return new Subject<URL>() return new BehaviorSubject<URL>(getLocation())
} }

View File

@ -20,14 +20,19 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Subject, Subscriber, from, fromEvent } from "rxjs" import { Observable, Subject, from } from "rxjs"
import { ajax } from "rxjs/ajax" import { ajax } from "rxjs/ajax"
import { map, pluck, shareReplay } from "rxjs/operators" import {
map,
pluck,
shareReplay,
take,
withLatestFrom
} from "rxjs/operators"
import { SearchIndexOptions } from "integrations/search" import { SearchIndexOptions } from "integrations/search"
import { import {
WorkerHandler, WorkerHandler,
getLocation,
watchWorker watchWorker
} from "observables" } from "observables"
@ -48,26 +53,7 @@ import {
interface SetupOptions { interface SetupOptions {
base: string /* Base url */ base: string /* Base url */
index?: Promise<SearchIndexOptions> /* Promise resolving with index */ index?: Promise<SearchIndexOptions> /* Promise resolving with index */
} location$: Observable<URL> /* Location observable */
/* ----------------------------------------------------------------------------
* Helper functions
* ------------------------------------------------------------------------- */
/**
* Resolve URL
* * TODO: document what's going on here + cache results
*
* @param origin - Base URL
* @param paths - Further URL paths
*
* @return Relative URL
*/
function resolve(origin: URL, ...paths: string[]) {
const path = location.pathname
.replace(origin.pathname, "")
.replace(/[^\/]+/g, "..")
return [path, ...paths].join("")
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
@ -88,21 +74,39 @@ function resolve(origin: URL, ...paths: string[]) {
* @return Worker handler * @return Worker handler
*/ */
export function setupSearchWorker( export function setupSearchWorker(
url: string, { base, index }: SetupOptions url: string, { base, index, location$ }: SetupOptions
): WorkerHandler<SearchMessage> { ): WorkerHandler<SearchMessage> {
const worker = new Worker(url) const worker = new Worker(url)
const origin = new URL(base, getLocation())
/* Compute new base URL when location changes */
const origin$ = location$
.pipe(
withLatestFrom(location$
.pipe(
take(1),
map(({ href }) => new URL(base, href))
)
),
map(([location, origin]) => location.href
.replace(origin.href, "")
.split("/")
.slice(1)
.map(() => "..")
.join("/")
)
)
/* Create communication channels and resolve relative links */ /* Create communication channels and resolve relative links */
const tx$ = new Subject<SearchMessage>() const tx$ = new Subject<SearchMessage>()
const rx$ = watchWorker(worker, { tx$ }) const rx$ = watchWorker(worker, { tx$ })
.pipe( .pipe(
map(message => { withLatestFrom(origin$),
map(([message, origin]) => {
if (isSearchResultMessage(message)) { if (isSearchResultMessage(message)) {
for (const { article, sections } of message.data) { for (const { article, sections } of message.data) {
article.location = resolve(origin, article.location) article.location = `${origin}/${article.location}`
for (const section of sections) for (const section of sections)
section.location = resolve(origin, section.location) section.location = `${origin}/${section.location}`
} }
} }
return message return message
@ -114,7 +118,7 @@ export function setupSearchWorker(
const index$ = typeof index !== "undefined" const index$ = typeof index !== "undefined"
? from(index) ? from(index)
: ajax({ : ajax({
url: resolve(origin, "search/search_index.json"), url: `${base}/search/search_index.json`,
responseType: "json", responseType: "json",
withCredentials: true withCredentials: true
}) })