base path refactor to better support subpath hosting

This commit is contained in:
Jacky Zhao
2023-08-19 15:52:25 -07:00
parent 3201f83b70
commit c874e7e937
29 changed files with 257 additions and 389 deletions

View File

@ -1,10 +1,4 @@
import {
CanonicalSlug,
FilePath,
ServerSlug,
canonicalizeServer,
resolveRelative,
} from "../../util/path"
import { FilePath, FullSlug, resolveRelative, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
@ -17,10 +11,10 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
const fps: FilePath[] = []
for (const [_tree, file] of content) {
const ogSlug = canonicalizeServer(file.data.slug!)
const ogSlug = simplifySlug(file.data.slug!)
const dir = path.posix.relative(argv.directory, file.dirname ?? argv.directory)
let aliases: CanonicalSlug[] = []
let aliases: FullSlug[] = []
if (file.data.frontmatter?.aliases) {
aliases = file.data.frontmatter?.aliases
} else if (file.data.frontmatter?.alias) {
@ -28,9 +22,8 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
}
for (const alias of aliases) {
const slug = path.posix.join(dir, alias) as ServerSlug
const redirUrl = resolveRelative(canonicalizeServer(slug), ogSlug)
const slug = path.posix.join(dir, alias) as FullSlug
const redirUrl = resolveRelative(slug, file.data.slug!)
const fp = await emit({
content: `
<!DOCTYPE html>

View File

@ -1,4 +1,4 @@
import { FilePath, ServerSlug } from "../../util/path"
import { FilePath, FullSlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
// @ts-ignore
@ -154,7 +154,7 @@ export const ComponentResources: QuartzEmitterPlugin<Options> = (opts?: Partial<
const postscript = joinScripts(componentResources.afterDOMLoaded)
const fps = await Promise.all([
emit({
slug: "index" as ServerSlug,
slug: "index" as FullSlug,
ext: ".css",
content: transform({
filename: "index.css",
@ -171,12 +171,12 @@ export const ComponentResources: QuartzEmitterPlugin<Options> = (opts?: Partial<
}).code.toString(),
}),
emit({
slug: "prescript" as ServerSlug,
slug: "prescript" as FullSlug,
ext: ".js",
content: prescript,
}),
emit({
slug: "postscript" as ServerSlug,
slug: "postscript" as FullSlug,
ext: ".js",
content: postscript,
}),

View File

@ -1,18 +1,12 @@
import { GlobalConfiguration } from "../../cfg"
import {
CanonicalSlug,
ClientSlug,
FilePath,
ServerSlug,
canonicalizeServer,
} from "../../util/path"
import { FilePath, FullSlug, SimpleSlug, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
export type ContentIndex = Map<CanonicalSlug, ContentDetails>
export type ContentIndex = Map<FullSlug, ContentDetails>
export type ContentDetails = {
title: string
links: CanonicalSlug[]
links: SimpleSlug[]
tags: string[]
content: string
date?: Date
@ -33,21 +27,21 @@ const defaultOptions: Options = {
function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
const base = cfg.baseUrl ?? ""
const createURLEntry = (slug: CanonicalSlug, content: ContentDetails): string => `<url>
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<url>
<loc>https://${base}/${slug}</loc>
<lastmod>${content.date?.toISOString()}</lastmod>
</url>`
const urls = Array.from(idx)
.map(([slug, content]) => createURLEntry(slug, content))
.map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
.join("")
return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>`
}
function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
const base = cfg.baseUrl ?? ""
const root = `https://${base}` as ClientSlug
const root = `https://${base}`
const createURLEntry = (slug: CanonicalSlug, content: ContentDetails): string => `<items>
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<items>
<title>${content.title}</title>
<link>${root}/${slug}</link>
<guid>${root}/${slug}</guid>
@ -56,7 +50,7 @@ function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
</items>`
const items = Array.from(idx)
.map(([slug, content]) => createURLEntry(slug, content))
.map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
.join("")
return `<rss xmlns:atom="http://www.w3.org/2005/atom" version="2.0">
<channel>
@ -79,7 +73,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
const emitted: FilePath[] = []
const linkIndex: ContentIndex = new Map()
for (const [_tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const slug = file.data.slug!
const date = file.data.dates?.modified ?? new Date()
if (opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) {
linkIndex.set(slug, {
@ -97,7 +91,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
emitted.push(
await emit({
content: generateSiteMap(cfg, linkIndex),
slug: "sitemap" as ServerSlug,
slug: "sitemap" as FullSlug,
ext: ".xml",
}),
)
@ -107,13 +101,13 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
emitted.push(
await emit({
content: generateRSSFeed(cfg, linkIndex),
slug: "index" as ServerSlug,
slug: "index" as FullSlug,
ext: ".xml",
}),
)
}
const fp = path.join("static", "contentIndex") as ServerSlug
const fp = path.join("static", "contentIndex") as FullSlug
const simplifiedIndex = Object.fromEntries(
Array.from(linkIndex).map(([slug, content]) => {
// remove description and from content index as nothing downstream

View File

@ -4,7 +4,7 @@ import HeaderConstructor from "../../components/Header"
import BodyConstructor from "../../components/Body"
import { pageResources, renderPage } from "../../components/renderPage"
import { FullPageLayout } from "../../cfg"
import { FilePath, canonicalizeServer } from "../../util/path"
import { FilePath } from "../../util/path"
import { defaultContentPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { Content } from "../../components"
@ -30,7 +30,7 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
const fps: FilePath[] = []
const allFiles = content.map((c) => c[1].data)
for (const [tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const slug = file.data.slug!
const externalResources = pageResources(slug, resources)
const componentData: QuartzComponentProps = {
fileData: file.data,
@ -44,7 +44,7 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
const content = renderPage(slug, componentData, opts, externalResources)
const fp = await emit({
content,
slug: file.data.slug!,
slug,
ext: ".html",
})

View File

@ -6,13 +6,7 @@ import { pageResources, renderPage } from "../../components/renderPage"
import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { FullPageLayout } from "../../cfg"
import path from "path"
import {
CanonicalSlug,
FilePath,
ServerSlug,
canonicalizeServer,
joinSegments,
} from "../../util/path"
import { FilePath, FullSlug, SimpleSlug, joinSegments, simplifySlug } from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { FolderContent } from "../../components"
@ -38,10 +32,10 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
const allFiles = content.map((c) => c[1].data)
const cfg = ctx.cfg.configuration
const folders: Set<CanonicalSlug> = new Set(
const folders: Set<SimpleSlug> = new Set(
allFiles.flatMap((data) => {
const slug = data.slug
const folderName = path.dirname(slug ?? "") as CanonicalSlug
const folderName = path.dirname(slug ?? "") as SimpleSlug
if (slug && folderName !== "." && folderName !== "tags") {
return [folderName]
}
@ -53,21 +47,21 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
[...folders].map((folder) => [
folder,
defaultProcessedContent({
slug: joinSegments(folder, "index") as ServerSlug,
slug: joinSegments(folder, "index") as FullSlug,
frontmatter: { title: `Folder: ${folder}`, tags: [] },
}),
]),
)
for (const [tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const slug = simplifySlug(file.data.slug!)
if (folders.has(slug)) {
folderDescriptions[slug] = [tree, file]
}
}
for (const folder of folders) {
const slug = folder
const slug = joinSegments(folder, "index") as FullSlug
const externalResources = pageResources(slug, resources)
const [tree, file] = folderDescriptions[folder]
const componentData: QuartzComponentProps = {
@ -82,7 +76,7 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
const content = renderPage(slug, componentData, opts, externalResources)
const fp = await emit({
content,
slug: file.data.slug!,
slug,
ext: ".html",
})

View File

@ -5,13 +5,7 @@ import BodyConstructor from "../../components/Body"
import { pageResources, renderPage } from "../../components/renderPage"
import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { FullPageLayout } from "../../cfg"
import {
CanonicalSlug,
FilePath,
ServerSlug,
getAllSegmentPrefixes,
joinSegments,
} from "../../util/path"
import { FilePath, FullSlug, getAllSegmentPrefixes, joinSegments } from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { TagContent } from "../../components"
@ -49,7 +43,7 @@ export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
return [
tag,
defaultProcessedContent({
slug: joinSegments("tags", tag) as ServerSlug,
slug: joinSegments("tags", tag) as FullSlug,
frontmatter: { title, tags: [] },
}),
]
@ -67,7 +61,7 @@ export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
}
for (const tag of tags) {
const slug = joinSegments("tags", tag) as CanonicalSlug
const slug = joinSegments("tags", tag) as FullSlug
const externalResources = pageResources(slug, resources)
const [tree, file] = tagDescriptions[tag]
const componentData: QuartzComponentProps = {

View File

@ -1,5 +1,5 @@
import { StaticResources } from "../util/resources"
import { FilePath, ServerSlug } from "../util/path"
import { FilePath, FullSlug } from "../util/path"
import { BuildCtx } from "../util/ctx"
export function getStaticResourcesFromPlugins(ctx: BuildCtx) {
@ -28,7 +28,7 @@ export * from "./emitters"
declare module "vfile" {
// inserted in processors.ts
interface DataMap {
slug: ServerSlug
slug: FullSlug
filePath: FilePath
}
}

View File

@ -1,11 +1,12 @@
import { QuartzTransformerPlugin } from "../types"
import {
CanonicalSlug,
FullSlug,
RelativeURL,
SimpleSlug,
TransformOptions,
_stripSlashes,
canonicalizeServer,
joinSegments,
simplifySlug,
splitAnchor,
transformLink,
} from "../../util/path"
@ -33,8 +34,8 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
return [
() => {
return (tree, file) => {
const curSlug = canonicalizeServer(file.data.slug!)
const outgoing: Set<CanonicalSlug> = new Set()
const curSlug = simplifySlug(file.data.slug!)
const outgoing: Set<SimpleSlug> = new Set()
const transformOptions: TransformOptions = {
strategy: opts.markdownLinkResolution,
@ -54,10 +55,15 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
// don't process external links or intra-document anchors
if (!(isAbsoluteUrl(dest) || dest.startsWith("#"))) {
dest = node.properties.href = transformLink(curSlug, dest, transformOptions)
dest = node.properties.href = transformLink(
file.data.slug!,
dest,
transformOptions,
)
const canonicalDest = path.posix.normalize(joinSegments(curSlug, dest))
const [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
outgoing.add(destCanonical as CanonicalSlug)
const simple = simplifySlug(destCanonical as FullSlug)
outgoing.add(simple)
}
// rewrite link internals if prettylinks is on
@ -79,7 +85,11 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
) {
if (!isAbsoluteUrl(node.properties.src)) {
let dest = node.properties.src as RelativeURL
dest = node.properties.src = transformLink(curSlug, dest, transformOptions)
dest = node.properties.src = transformLink(
file.data.slug!,
dest,
transformOptions,
)
node.properties.src = dest
}
}
@ -95,6 +105,6 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
declare module "vfile" {
interface DataMap {
links: CanonicalSlug[]
links: SimpleSlug[]
}
}

View File

@ -9,7 +9,7 @@ import path from "path"
import { JSResource } from "../../util/resources"
// @ts-ignore
import calloutScript from "../../components/scripts/callout.inline.ts"
import { FilePath, canonicalizeServer, pathToRoot, slugTag, slugifyFilePath } from "../../util/path"
import { FilePath, pathToRoot, slugTag, slugifyFilePath } from "../../util/path"
import { toHast } from "mdast-util-to-hast"
import { toHtml } from "hast-util-to-html"
import { PhrasingContent } from "mdast-util-find-and-replace/lib"
@ -381,8 +381,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
if (opts.parseTags) {
plugins.push(() => {
return (tree: Root, file) => {
const slug = canonicalizeServer(file.data.slug!)
const base = pathToRoot(slug)
const base = pathToRoot(file.data.slug!)
findAndReplace(tree, tagRegex, (value: string, tag: string) => {
if (file.data.frontmatter) {
file.data.frontmatter.tags.push(tag)

View File

@ -2,7 +2,7 @@ import { PluggableList } from "unified"
import { StaticResources } from "../util/resources"
import { ProcessedContent } from "./vfile"
import { QuartzComponent } from "../components/types"
import { FilePath, ServerSlug } from "../util/path"
import { FilePath, FullSlug } from "../util/path"
import { BuildCtx } from "../util/ctx"
export interface PluginTypes {
@ -46,7 +46,7 @@ export type QuartzEmitterPluginInstance = {
}
export interface EmitOptions {
slug: ServerSlug
slug: FullSlug
ext: `.${string}` | ""
content: string
}