| | |
| | | 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" |
| | |
| | | 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, |
| | |
| | | |
| | | // 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 |
| | |
| | | ) { |
| | | 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 |
| | | } |
| | | } |
| | | }) |
| | |
| | | |
| | | declare module "vfile" { |
| | | interface DataMap { |
| | | links: CanonicalSlug[] |
| | | links: SimpleSlug[] |
| | | } |
| | | } |