Add /raw route for getting images

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-03-14 11:13:15 +01:00
parent 3ea6a07dc4
commit 09ceeed136
10 changed files with 40 additions and 101 deletions
+2 -3
View File
@@ -1,5 +1,4 @@
<script lang="ts">
import type { MaybeBlob } from "@app/lib/project";
import type { Comment, Thread } from "@app/lib/issue";
import Authorship from "@app/components/Authorship.svelte";
@@ -8,7 +7,7 @@
export let comment: Comment | Thread;
export let caption = "commented";
export let getImage: (path: string) => Promise<MaybeBlob>;
export let rawPath: string;
$: source = comment.author.id;
$: title = comment.author.id;
@@ -57,7 +56,7 @@
{#if comment.body.trim() === ""}
<span class="txt-missing">No description.</span>
{:else}
<Markdown content={comment.body} {getImage} />
<Markdown {rawPath} content={comment.body} />
{/if}
</div>
</div>
+12 -17
View File
@@ -1,6 +1,4 @@
<script lang="ts">
import type * as proj from "@app/lib/project";
import dompurify from "dompurify";
import matter from "@radicle/gray-matter";
import { marked } from "marked";
@@ -8,7 +6,7 @@
import { toDom } from "hast-util-to-dom";
import { base } from "@app/lib/router";
import { getImageMime, isUrl, twemoji, scrollIntoView } from "@app/lib/utils";
import { isUrl, twemoji, scrollIntoView, canonicalize } from "@app/lib/utils";
import { highlight } from "@app/lib/syntax";
import {
markdownExtensions as extensions,
@@ -17,8 +15,9 @@
export let content: string;
export let doc = matter(content);
export let getImage: (path: string) => Promise<proj.MaybeBlob>;
export let hash: string | null = null;
export let path: string = "/";
export let rawPath: string;
const frontMatter = Object.entries(doc.data).filter(
([, val]) => typeof val === "string" || typeof val === "number",
@@ -43,22 +42,18 @@
if (hash) scrollIntoView(hash);
// Iterate over all images, and fetch their data from the API, then
// replace the source with a Data-URL. We do this due to the absence
// of a static file server.
// Iterate over all images, and replace the source with a canonicalized URL
// pointing at the projects /raw endpoint.
for (const i of container.querySelectorAll("img")) {
const path = i.getAttribute("src");
const imagePath = i.getAttribute("src");
// Make sure the source isn't a URL before trying to fetch it from the repo
if (path && !isUrl(path) && !path.startsWith(`${base}twemoji`)) {
getImage(path).then(blob => {
if (blob?.content) {
const mime = getImageMime(path);
if (mime) {
i.setAttribute("src", `data:${mime};base64,${blob.content}`);
}
}
});
if (
imagePath &&
!isUrl(imagePath) &&
!imagePath.startsWith(`${base}twemoji`)
) {
i.setAttribute("src", `${rawPath}/${canonicalize(imagePath, path)}`);
}
}