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
quartz/plugins/transformers

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)