mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-05-18 14:34:23 +02:00
links.ts: move to newtype model
This commit is contained in:
parent
11db328af1
commit
9519492a38
@ -13,7 +13,30 @@ import path from "path"
|
||||
import { visit } from "unist-util-visit"
|
||||
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 {
|
||||
/** How to resolve Markdown paths */
|
||||
@ -59,26 +82,60 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts)
|
||||
) {
|
||||
const href = node.properties.href
|
||||
let dest = href as RelativeURL
|
||||
let refIcon: string | ElementContent | null = null
|
||||
let refIcon: ElementContent | null = null
|
||||
let matched = false
|
||||
// bfahrenfort: the 'every' lambda is like a foreach that allows continue/break
|
||||
opts.substitutions?.every(([regex, sub]) => {
|
||||
const parts = href.match(regex)
|
||||
if (parts != null) {
|
||||
dest = parts.slice(1).join("") as RelativeURL
|
||||
if (typeof sub == "object") {
|
||||
refIcon = sub
|
||||
} else {
|
||||
refIcon = { type: "text", value: sub }
|
||||
}
|
||||
if (parts == null) return true // continue
|
||||
|
||||
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
|
||||
const classes = (node.properties.className ?? []) as string[]
|
||||
const isExternal = isAbsoluteUrl(dest)
|
||||
classes.push(isExternal ? "external" : "internal")
|
||||
const isExternal = URL.canParse(dest)
|
||||
classes.push(isExternal || matched ? "external" : "internal")
|
||||
|
||||
if ((isExternal && opts.externalLinkIcon) || matched) {
|
||||
node.children.push(
|
||||
@ -177,6 +234,7 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts)
|
||||
dest,
|
||||
transformOptions,
|
||||
)
|
||||
console.log(dest)
|
||||
node.properties.src = dest
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user