Skip to content

Commit

Permalink
refactor: move types to top
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Apr 20, 2024
1 parent 2a8a62a commit 99523a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { inlineFavicon } from "./inlineFavicon";

export default inlineFavicon;
export { inlineFavicon, inlineFavicon as posthtmlInlineFavicon };
export {
inlineFavicon as default,
inlineFavicon,
inlineFavicon as posthtmlInlineFavicon,
};
27 changes: 17 additions & 10 deletions src/inlineFavicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
type Options = {
/**
* The relative path to the folder containing the favicon.
* @example "public"
*/
path?: string;
};

export function inlineFavicon(options?: Options): Plugin<void> {
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");

Expand All @@ -20,13 +33,7 @@ function inlineFavicon(options: IOptions = { path: "" }): Plugin<void> {
node.attrs.href = `data:image/png;base64,${base64}`;

return node;
},
}
);
};
}

interface IOptions {
path?: string;
}

export { inlineFavicon };

0 comments on commit 99523a5

Please sign in to comment.