This commit is contained in:
sam 2025-03-29 23:04:15 +01:00
commit e8a255ce1f
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
10 changed files with 1390 additions and 0 deletions

10
dist/index.d.ts vendored Normal file
View file

@ -0,0 +1,10 @@
import type { Plugin } from "unified";
import type { Root } from "hast";
interface RehypeCustomEmojiOptions {
sourceFile: string;
hidpiSourceFile?: string;
emojis: Record<string, [number, number]>;
size: number;
}
declare const plugin: Plugin<[Partial<RehypeCustomEmojiOptions>], Root>;
export default plugin;

1
dist/index.d.ts.map vendored Normal file
View file

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,EAAS,IAAI,EAAW,MAAM,MAAM,CAAC;AAOjD,UAAU,wBAAwB;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACzC,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxB;AAQD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,EAAE,IAAI,CA0B7D,CAAC;AAEF,eAAe,MAAM,CAAC"}

34
dist/index.js vendored Normal file
View file

@ -0,0 +1,34 @@
import { findAndReplace } from "hast-util-find-and-replace";
import { h } from "hastscript";
const RE_EMOJI = /:([\w-]+):/g;
const DEFAULT_OPTIONS = {
sourceFile: "/assets/emojis.png",
hidpiSourceFile: undefined,
emojis: {},
size: 64,
};
const plugin = (options) => {
const settings = Object.assign({}, DEFAULT_OPTIONS, options);
const srcset = settings.hidpiSourceFile
? `${settings.sourceFile} 1x, ${settings.hidpiSourceFile} 2x`
: undefined;
const emojiElement = (x, y, label) => {
return h("img", {
src: settings.sourceFile,
srcset,
style: `object-fit: none; object-position: -${x}px -${y}px; width: ${settings.size}px; height: ${settings.size}px`,
alt: label,
title: label,
});
};
const replaceEmoji = (_, match) => {
if (!(match in settings.emojis))
return false;
const emoji = settings.emojis[match];
return emojiElement(emoji[0], emoji[1], match);
};
return (tree) => {
findAndReplace(tree, [RE_EMOJI, replaceEmoji]);
};
};
export default plugin;