From 99523a5864d9ad2ad3573d6d33a949e6044c68ac Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Sat, 20 Apr 2024 00:39:57 -0700 Subject: [PATCH] refactor: move types to top --- src/index.ts | 7 +++++-- src/inlineFavicon.ts | 27 +++++++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 771d615..31f54d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,7 @@ import { inlineFavicon } from "./inlineFavicon"; -export default inlineFavicon; -export { inlineFavicon, inlineFavicon as posthtmlInlineFavicon }; +export { + inlineFavicon as default, + inlineFavicon, + inlineFavicon as posthtmlInlineFavicon, +}; diff --git a/src/inlineFavicon.ts b/src/inlineFavicon.ts index 9b15f54..c4c7208 100644 --- a/src/inlineFavicon.ts +++ b/src/inlineFavicon.ts @@ -2,16 +2,29 @@ import fs from "node:fs"; import path from "node:path"; import { type Plugin } from "posthtml"; -function inlineFavicon(options: IOptions = { path: "" }): Plugin { +type Options = { + /** + * The relative path to the folder containing the favicon. + * @example "public" + */ + path?: string; +}; + +export function inlineFavicon(options?: Options): Plugin { + const relativePath = options?.path ?? ""; + return function plugin(tree) { tree.match( { tag: "link", - attrs: { rel: new RegExp(/icon/), href: new RegExp(/\S+/) }, + attrs: { + rel: new RegExp(/icon/), + href: new RegExp(/\S+/), + }, }, (node) => { const href = node.attrs.href; - const file = path.join(process.cwd(), options.path || "", href); + const file = path.join(process.cwd(), relativePath, href); const source = fs.readFileSync(file); const base64 = Buffer.from(source).toString("base64"); @@ -20,13 +33,7 @@ function inlineFavicon(options: IOptions = { path: "" }): Plugin { node.attrs.href = `data:image/png;base64,${base64}`; return node; - }, + } ); }; } - -interface IOptions { - path?: string; -} - -export { inlineFavicon };