Jacky Zhao
2023-12-19 b44a79eebaf1664512ef5ff4e3246b3a6a416ff4
quartz/plugins/transformers/links.ts
@@ -5,7 +5,6 @@
  SimpleSlug,
  TransformOptions,
  _stripSlashes,
  joinSegments,
  simplifySlug,
  splitAnchor,
  transformLink,
@@ -19,11 +18,13 @@
  markdownLinkResolution: TransformOptions["strategy"]
  /** Strips folders from a link so that it looks nice */
  prettyLinks: boolean
  openLinksInNewTab: boolean
}
const defaultOptions: Options = {
  markdownLinkResolution: "absolute",
  prettyLinks: true,
  openLinksInNewTab: false,
}
export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
@@ -53,8 +54,23 @@
                node.properties.className ??= []
                node.properties.className.push(isAbsoluteUrl(dest) ? "external" : "internal")
                // Check if the link has alias text
                if (
                  node.children.length === 1 &&
                  node.children[0].type === "text" &&
                  node.children[0].value !== dest
                ) {
                  // Add the 'alias' class if the text content is not the same as the href
                  node.properties.className.push("alias")
                }
                if (opts.openLinksInNewTab) {
                  node.properties.target = "_blank"
                }
                // don't process external links or intra-document anchors
                if (!(isAbsoluteUrl(dest) || dest.startsWith("#"))) {
                const isInternal = !(isAbsoluteUrl(dest) || dest.startsWith("#"))
                if (isInternal) {
                  dest = node.properties.href = transformLink(
                    file.data.slug!,
                    dest,
@@ -63,20 +79,24 @@
                  // url.resolve is considered legacy
                  // WHATWG equivalent https://nodejs.dev/en/api/v18/url/#urlresolvefrom-to
                  const url = new URL(dest, `resolve://${curSlug}`)
                  const url = new URL(dest, `https://base.com/${curSlug}`)
                  const canonicalDest = url.pathname
                  const [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
                  let [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
                  if (destCanonical.endsWith("/")) {
                    destCanonical += "index"
                  }
                  // need to decodeURIComponent here as WHATWG URL percent-encodes everything
                  const simple = decodeURIComponent(
                    simplifySlug(destCanonical as FullSlug),
                  ) as SimpleSlug
                  const full = decodeURIComponent(_stripSlashes(destCanonical, true)) as FullSlug
                  const simple = simplifySlug(full)
                  outgoing.add(simple)
                  node.properties["data-slug"] = full
                }
                // rewrite link internals if prettylinks is on
                if (
                  opts.prettyLinks &&
                  isInternal &&
                  node.children.length === 1 &&
                  node.children[0].type === "text" &&
                  !node.children[0].value.startsWith("#")