links.ts: move to newtype model

This commit is contained in:
bfahrenfort 2024-12-23 13:09:44 -06:00
parent 11db328af1
commit 9519492a38

View File

@ -13,7 +13,30 @@ import path from "path"
import { visit } from "unist-util-visit" import { visit } from "unist-util-visit"
import { ElementContent, Root } from "hast" import { ElementContent, Root } from "hast"
type Sub = [RegExp, string | ElementContent] type Sub = [RegExp, Appendable]
type Appendable = (Image | Emoji | Path) & Tagged
type Tagged = { type: "image" | "emoji" | "path" }
type Image = { src: string }
type Emoji = { text: string }
type Path = {
code: string
viewbox: string
}
export function Image(src: string | Image): Appendable {
if (typeof src == "object") {
return src as Image & { type: "image" }
}
return { src: src, type: "image" }
}
export function Emoji(text: string | Emoji): Appendable {
if (typeof text == "object") {
return text as Emoji & { type: "emoji" }
}
return { text: text, type: "emoji" }
}
export function Path(path: Path): Appendable {
return path as Path & { type: "path" } // This errors if path is uncast. what
}
interface Options { interface Options {
/** How to resolve Markdown paths */ /** How to resolve Markdown paths */
@ -59,26 +82,60 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts)
) { ) {
const href = node.properties.href const href = node.properties.href
let dest = href as RelativeURL let dest = href as RelativeURL
let refIcon: string | ElementContent | null = null let refIcon: ElementContent | null = null
let matched = false let matched = false
// bfahrenfort: the 'every' lambda is like a foreach that allows continue/break
opts.substitutions?.every(([regex, sub]) => { opts.substitutions?.every(([regex, sub]) => {
const parts = href.match(regex) const parts = href.match(regex)
if (parts != null) { if (parts == null) return true // continue
dest = parts.slice(1).join("") as RelativeURL
if (typeof sub == "object") {
refIcon = sub
} else {
refIcon = { type: "text", value: sub }
}
matched = true matched = true
return false // break equivalent dest = parts.slice(1).join("") as RelativeURL
switch (sub.type) {
case "image":
refIcon = {
type: "element",
tagName: "img",
properties: {
src: (sub as Image).src as FullSlug,
style: "max-width:1em;max-height:1em",
},
children: [],
} }
return true break
case "emoji":
refIcon = { type: "text", value: (sub as Emoji).text }
break
case "path":
let vector = sub as Path
refIcon = {
type: "element",
tagName: "svg",
properties: {
"aria-hidden": "true",
class: "external-icon",
style: "max-width:1em;max-height:1em",
viewBox: vector.viewbox,
},
children: [
{
type: "element",
tagName: "path",
properties: {
d: vector.code,
},
children: [],
},
],
}
break
}
return false // break
}) })
node.properties.href = dest node.properties.href = dest
const classes = (node.properties.className ?? []) as string[] const classes = (node.properties.className ?? []) as string[]
const isExternal = isAbsoluteUrl(dest) const isExternal = URL.canParse(dest)
classes.push(isExternal ? "external" : "internal") classes.push(isExternal || matched ? "external" : "internal")
if ((isExternal && opts.externalLinkIcon) || matched) { if ((isExternal && opts.externalLinkIcon) || matched) {
node.children.push( node.children.push(
@ -177,6 +234,7 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts)
dest, dest,
transformOptions, transformOptions,
) )
console.log(dest)
node.properties.src = dest node.properties.src = dest
} }
} }