Merge of Insiders features tied to 'Aji Panca' funding goal

This commit is contained in:
squidfunk
2022-02-17 17:20:36 +01:00
parent 90137011e9
commit b5e71dfcb1
57 changed files with 1670 additions and 13500 deletions

View File

@@ -23,4 +23,5 @@
export * from "./clipboard"
export * from "./instant"
export * from "./search"
export * from "./sitemap"
export * from "./version"

View File

@@ -50,13 +50,14 @@ import {
getElements,
getOptionalElement,
request,
requestXML,
setLocation,
setLocationHash
} from "~/browser"
import { getComponentElement } from "~/components"
import { h } from "~/utilities"
import { fetchSitemap } from "../sitemap"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
@@ -82,44 +83,6 @@ interface SetupOptions {
viewport$: Observable<Viewport> /* Viewport observable */
}
/* ----------------------------------------------------------------------------
* Helper functions
* ------------------------------------------------------------------------- */
/**
* Preprocess a list of URLs
*
* This function replaces the `site_url` in the sitemap with the actual base
* URL, to allow instant loading to work in occasions like Netlify previews.
*
* @param urls - URLs
*
* @returns Processed URLs
*/
function preprocess(urls: string[]): string[] {
if (urls.length < 2)
return urls
/* Take the first two URLs and remove everything after the last slash */
const [root, next] = urls
.sort((a, b) => a.length - b.length)
.map(url => url.replace(/[^/]+$/, ""))
/* Compute common prefix */
let index = 0
if (root === next)
index = root.length
else
while (root.charCodeAt(index) === next.charCodeAt(index))
index++
/* Replace common prefix (i.e. base) with effective base */
const config = configuration()
return urls.map(url => (
url.replace(root.slice(0, index), config.base)
))
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
@@ -169,17 +132,13 @@ export function setupInstantLoading(
favicon.href = favicon.href
/* Intercept internal navigation */
const push$ = requestXML(new URL("sitemap.xml", config.base))
const push$ = fetchSitemap()
.pipe(
map(sitemap => preprocess(getElements("loc", sitemap)
.map(node => node.textContent!)
)),
map(paths => paths.map(path => `${new URL(path, config.base)}`)),
switchMap(urls => fromEvent<MouseEvent>(document.body, "click")
.pipe(
filter(ev => !ev.metaKey && !ev.ctrlKey),
switchMap(ev => {
/* Handle HTML and SVG elements */
if (ev.target instanceof Element) {
const el = ev.target.closest("a")
if (el && !el.target) {

View File

@@ -55,6 +55,7 @@ export interface SearchIndexDocument {
location: string /* Document location */
title: string /* Document title */
text: string /* Document text */
tags?: string[] /* Document tags */
}
/* ------------------------------------------------------------------------- */
@@ -202,6 +203,7 @@ export class Search {
/* Set up fields */
this.field("title", { boost: 1e3 })
this.field("text")
this.field("tags", { boost: 1e6 })
/* Index documents */
for (const doc of docs)
@@ -243,7 +245,7 @@ export class Search {
.reduce<SearchResultItem>((item, { ref, score, matchData }) => {
const document = this.documents.get(ref)
if (typeof document !== "undefined") {
const { location, title, text, parent } = document
const { location, title, text, tags, parent } = document
/* Compute and analyze search query terms */
const terms = getSearchQueryTerms(
@@ -257,6 +259,7 @@ export class Search {
location,
title: highlight(title),
text: highlight(text),
...tags && { tags: tags.map(highlight) },
score: score * (1 + boost),
terms
})

View File

@@ -61,9 +61,10 @@ export function setupSearchDocumentMap(
for (const doc of docs) {
const [path, hash] = doc.location.split("#")
/* Extract location and title */
/* Extract location, title and tags */
const location = doc.location
const title = doc.title
const tags = doc.tags
/* Escape and cleanup text */
const text = escapeHTML(doc.text)
@@ -97,7 +98,8 @@ export function setupSearchDocumentMap(
documents.set(location, {
location,
title,
text
text,
...tags && { tags }
})
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (c) 2016-2022 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import {
Observable,
defaultIfEmpty,
map,
of,
tap
} from "rxjs"
import { configuration } from "~/_"
import { getElements, requestXML } from "~/browser"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Sitemap, i.e. a list of URLs
*/
export type Sitemap = string[]
/* ----------------------------------------------------------------------------
* Helper functions
* ------------------------------------------------------------------------- */
/**
* Preprocess a list of URLs
*
* This function replaces the `site_url` in the sitemap with the actual base
* URL, to allow instant loading to work in occasions like Netlify previews.
*
* @param urls - URLs
*
* @returns URL path parts
*/
function preprocess(urls: Sitemap): Sitemap {
if (urls.length < 2)
return [""]
/* Take the first two URLs and remove everything after the last slash */
const [root, next] = [...urls]
.sort((a, b) => a.length - b.length)
.map(url => url.replace(/[^/]+$/, ""))
/* Compute common prefix */
let index = 0
if (root === next)
index = root.length
else
while (root.charCodeAt(index) === next.charCodeAt(index))
index++
/* Remove common prefix and return in original order */
return urls.map(url => url.replace(root.slice(0, index), ""))
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Fetch the sitemap for the given base URL
*
* @param base - Base URL
*
* @returns Sitemap observable
*/
export function fetchSitemap(base?: URL): Observable<Sitemap> {
const cached = __md_get<Sitemap>("__sitemap", sessionStorage, base)
if (cached) {
return of(cached)
} else {
const config = configuration()
return requestXML(new URL("sitemap.xml", base || config.base))
.pipe(
map(sitemap => preprocess(getElements("loc", sitemap)
.map(node => node.textContent!)
)),
defaultIfEmpty([]),
tap(sitemap => __md_set("__sitemap", sitemap, sessionStorage, base))
)
}
}

View File

@@ -20,12 +20,22 @@
* IN THE SOFTWARE.
*/
import { combineLatest, map } from "rxjs"
import {
EMPTY,
combineLatest,
filter,
fromEvent,
map,
of,
switchMap
} from "rxjs"
import { configuration } from "~/_"
import {
getElement,
requestJSON
getLocation,
requestJSON,
setLocation
} from "~/browser"
import { getComponentElements } from "~/components"
import {
@@ -33,6 +43,8 @@ import {
renderVersionSelector
} from "~/templates"
import { fetchSitemap } from "../sitemap"
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
@@ -57,6 +69,47 @@ export function setupVersionSelector(): void {
})
)
/* Intercept inter-version navigation */
combineLatest([versions$, current$])
.pipe(
map(([versions, current]) => new Map(versions
.filter(version => version !== current)
.map(version => [
`${new URL(`../${version.version}/`, config.base)}`,
version
])
)),
switchMap(urls => fromEvent<MouseEvent>(document.body, "click")
.pipe(
filter(ev => !ev.metaKey && !ev.ctrlKey),
switchMap(ev => {
if (ev.target instanceof Element) {
const el = ev.target.closest("a")
if (el && !el.target && urls.has(el.href)) {
ev.preventDefault()
return of(el.href)
}
}
return EMPTY
}),
switchMap(url => {
const { version } = urls.get(url)!
return fetchSitemap(new URL(url))
.pipe(
map(sitemap => {
const location = getLocation()
const path = location.href.replace(config.base, "")
return sitemap.includes(path)
? new URL(`../${version}/${path}`, config.base)
: new URL(url)
})
)
})
)
)
)
.subscribe(url => setLocation(url))
/* Render version selector and warning */
combineLatest([versions$, current$])
.subscribe(([versions, current]) => {