Skip to content

Commit

Permalink
Fix twemojis display
Browse files Browse the repository at this point in the history
Text in Markdown with emojis has been tried to be fetched from the
/raw route as embeds.
This commit changes the renderer method to compute the hex string for
a unicode emoji to be able to use the /twemoji asset folder.
  • Loading branch information
sebastinez committed Oct 21, 2024
1 parent 2b058a1 commit 3f84059
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion public/typography.css
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ p {
.txt-missing {
color: var(--color-foreground-dim);
}
.txt-emoji {
.marked-emoji-img {
height: 1em;
width: 1em;
margin: 0 0.05em 0 0.1em;
Expand Down
8 changes: 7 additions & 1 deletion src/components/Markdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
// pointing at the repos /raw endpoint.
for (const i of container.querySelectorAll("img")) {
const imagePath = i.getAttribute("src");
const imageClass = i.getAttribute("class");
// If the image is an oid embed
if (imagePath && isCommit(imagePath)) {
Expand All @@ -160,7 +161,8 @@
}
// Make sure the source isn't a URL before trying to fetch it from the repo
if (imagePath && !isUrl(imagePath) && !imagePath.startsWith("/twemoji")) {
const emoji = imageClass && imageClass === "marked-emoji-img";
if (imagePath && !isUrl(imagePath) && !emoji) {
i.setAttribute("src", `${rawPath}/${canonicalize(imagePath, path)}`);
}
}
Expand Down Expand Up @@ -349,6 +351,10 @@
max-width: 100%;
}
.markdown :global(img.marked-emoji-img) {
height: 1rem;
}
.markdown :global(code) {
font-family: var(--font-family-monospace);
font-size: var(--font-size-small);
Expand Down
8 changes: 7 additions & 1 deletion src/lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ export const markdownWithExtensions = new Marked(
katexMarkedExtension({ throwOnError: false }),
markedLinkifyIt({}, { fuzzyLink: false }),
markedFootnote({ refMarkers: true }),
markedEmoji({ emojis }),
markedEmoji({
emojis,
renderer: (token: { name: string; emoji: string }) => {
const src = token.emoji.codePointAt(0)?.toString(16);
return `<img alt="${token.name}" src="/twemoji/${src}.svg" class="marked-emoji-img">`;
},
}),
((): MarkedExtension => ({
extensions: [anchorMarkedExtension],
}))(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export function twemoji(
base: "/",
folder: "twemoji",
ext: ".svg",
className: `txt-emoji`,
className: "marked-emoji-img",
});
}

Expand Down

0 comments on commit 3f84059

Please sign in to comment.