| | |
| | | import { PluggableList } from "unified" |
| | | import { QuartzTransformerPlugin } from "../types" |
| | | import { relative, relativeToRoot, slugify } from "../../path" |
| | | import path from "path" |
| | |
| | | prettyLinks: true |
| | | } |
| | | |
| | | export class ResolveLinks extends QuartzTransformerPlugin { |
| | | name = "LinkProcessing" |
| | | opts: Options |
| | | |
| | | constructor(opts?: Partial<Options>) { |
| | | super() |
| | | this.opts = { ...defaultOptions, ...opts } |
| | | } |
| | | |
| | | markdownPlugins(): PluggableList { |
| | | export const ResolveLinks: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => { |
| | | const opts = { ...defaultOptions, ...userOpts } |
| | | return { |
| | | name: "LinkProcessing", |
| | | markdownPlugins() { |
| | | return [] |
| | | } |
| | | |
| | | htmlPlugins(): PluggableList { |
| | | }, |
| | | htmlPlugins() { |
| | | return [() => { |
| | | return (tree, file) => { |
| | | const curSlug = file.data.slug! |
| | | const transformLink = (target: string) => { |
| | | const targetSlug = slugify(decodeURI(target).trim()) |
| | | if (this.opts.markdownLinkResolution === 'relative' && !path.isAbsolute(targetSlug)) { |
| | | if (opts.markdownLinkResolution === 'relative' && !path.isAbsolute(targetSlug)) { |
| | | return './' + relative(curSlug, targetSlug) |
| | | } else { |
| | | return './' + relativeToRoot(curSlug, targetSlug) |
| | |
| | | } |
| | | |
| | | // rewrite link internals if prettylinks is on |
| | | if (this.opts.prettyLinks && node.children.length === 1 && node.children[0].type === 'text') { |
| | | if (opts.prettyLinks && node.children.length === 1 && node.children[0].type === 'text') { |
| | | node.children[0].value = path.basename(node.children[0].value) |
| | | } |
| | | } |
| | |
| | | }] |
| | | } |
| | | } |
| | | } |