Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Added Img support to Markdown, added image formatting as webp #56

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PropsWithChildren } from "react"
import ReactMarkdown, { Components } from "react-markdown"
import remarkGfm from "remark-gfm"
import { A, H1, H2, H3, H4, P, Pre } from "./typography"
import { A, H1, H2, H3, H4, P, Pre, Img } from "./typography"

const markdownComponents: Components = {
h1: H1,
Expand All @@ -11,6 +11,7 @@ const markdownComponents: Components = {
p: P,
pre: Pre,
a: A,
img: Img,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add img: Ignore to plainTextComponents as well.

}

// Fragment is necessary for react-markdown typings
Expand Down
14 changes: 13 additions & 1 deletion src/components/typography.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classNames from "classnames"
import { AnchorHTMLAttributes, HTMLAttributes } from "react"
import { AnchorHTMLAttributes, HTMLAttributes, ImgHTMLAttributes } from "react"
import parseImageUrl from "../utils/parseImageUrl"
import Link from "./Link"

// Not helpful here...
Expand Down Expand Up @@ -85,3 +86,14 @@ export const A = ({ children, className, href, ...props }: AnchorProps) =>
{children}
</Link>
)

type ImgProps = ImgHTMLAttributes<HTMLImageElement>

export const Img = ({ className, alt, src, ...props }: ImgProps) => (
<img
alt={alt}
src={src && parseImageUrl(src)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider supporting srcset here since Strapi provides various sizes of images.

className={classNames("", className)}
{...props}
/>
)
2 changes: 1 addition & 1 deletion src/utils/parseImageUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const parseImageUrl = (url: string) => {
if (process.env.NODE_ENV === "development") {
return process.env.STRAPI_URL + url
return `${process.env.STRAPI_URL + url}?format=webp`
}
return url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up implementing Tietokilta/tikweb-cms#51 or similar in production, we should also add ?format=webp here.

If we do it that way, the image URL will also move to https://<strapi>/uploads, which will require adding STRAPI_URL to each.

}
Expand Down