fix markdown image rendering (#636)

This commit is contained in:
ABevier 2023-05-30 19:55:41 -04:00 committed by GitHub
parent 7c63a1f570
commit a5a37d138a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View file

@ -2,11 +2,13 @@
import SvelteMarkdown from "svelte-markdown"; import SvelteMarkdown from "svelte-markdown";
import Link from "./link.svelte"; import Link from "./link.svelte";
import rst2html from "./rst2html"; import rst2html from "./rst2html";
import "./styles.css"; import "./styles.css";
import { onMount } from "svelte";
export let source: { data: string; type: "md" | "rst" }; export let source: { data: string; type: "md" | "rst" };
let markDownRoot: HTMLElement;
export let hook = (node: HTMLElement): { destroy: () => void } => { export let hook = (node: HTMLElement): { destroy: () => void } => {
console.log("hook", node); console.log("hook", node);
return { return {
@ -21,11 +23,31 @@
}; };
$: html = source.type === "rst" ? rst2html(source.data) : ""; $: html = source.type === "rst" ? rst2html(source.data) : "";
onMount(() => {
// Need to override the height/width STYLE with the old-school height/width ATTRIBUTE to make it work with the markdown
if (markDownRoot) {
const images = markDownRoot.querySelectorAll("img");
images.forEach((element: HTMLImageElement) => {
const height = element.getAttribute("height");
if (height) {
element.style.height = height;
}
const width = element.getAttribute("width");
if (width) {
element.style.width = width;
}
});
}
});
</script> </script>
<section use:hook class="markdown-body py-4"> <section use:hook class="markdown-body py-4">
{#if source.type === "md"} {#if source.type === "md"}
<SvelteMarkdown source={source.data} {renderers} /> <div bind:this={markDownRoot}>
<SvelteMarkdown source={source.data} {renderers} />
</div>
{:else if source.type === "rst"} {:else if source.type === "rst"}
{@html html} {@html html}
{/if} {/if}

View file

@ -114,6 +114,7 @@
max-width: 100%; max-width: 100%;
box-sizing: content-box; box-sizing: content-box;
background-color: #0d1117; background-color: #0d1117;
display: inline;
} }
.markdown-body code, .markdown-body code,