Jacky Zhao
2023-08-23 eed4472aeecdcb0f2b233df69884f03bd45fc293
quartz/plugins/transformers/links.ts
@@ -1,14 +1,15 @@
import { QuartzTransformerPlugin } from "../types"
import {
  CanonicalSlug,
  FullSlug,
  RelativeURL,
  SimpleSlug,
  TransformOptions,
  _stripSlashes,
  canonicalizeServer,
  joinSegments,
  simplifySlug,
  splitAnchor,
  transformLink,
} from "../../path"
} from "../../util/path"
import path from "path"
import { visit } from "unist-util-visit"
import isAbsoluteUrl from "is-absolute-url"
@@ -33,8 +34,8 @@
      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,23 @@
                // don't process external links or intra-document anchors
                if (!(isAbsoluteUrl(dest) || dest.startsWith("#"))) {
                  dest = node.properties.href = transformLink(curSlug, dest, transformOptions)
                  const canonicalDest = path.posix.normalize(joinSegments(curSlug, dest))
                  dest = node.properties.href = transformLink(
                    file.data.slug!,
                    dest,
                    transformOptions,
                  )
                  // url.resolve is considered legacy
                  // WHATWG equivalent https://nodejs.dev/en/api/v18/url/#urlresolvefrom-to
                  const url = new URL(dest, `https://base.com/${curSlug}`)
                  const canonicalDest = url.pathname
                  const [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
                  outgoing.add(destCanonical as CanonicalSlug)
                  // need to decodeURIComponent here as WHATWG URL percent-encodes everything
                  const simple = decodeURIComponent(
                    simplifySlug(destCanonical as FullSlug),
                  ) as SimpleSlug
                  outgoing.add(simple)
                }
                // rewrite link internals if prettylinks is on
@@ -79,9 +93,12 @@
              ) {
                if (!isAbsoluteUrl(node.properties.src)) {
                  let dest = node.properties.src as RelativeURL
                  const ext = path.extname(node.properties.src)
                  dest = node.properties.src = transformLink(curSlug, dest, transformOptions)
                  node.properties.src = dest + ext
                  dest = node.properties.src = transformLink(
                    file.data.slug!,
                    dest,
                    transformOptions,
                  )
                  node.properties.src = dest
                }
              }
            })
@@ -96,6 +113,6 @@
declare module "vfile" {
  interface DataMap {
    links: CanonicalSlug[]
    links: SimpleSlug[]
  }
}