Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: Add hook for rendering images #977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions examples/MarkdownExample.re
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ let example = () =>
An h2 header
------------

![Test Image](https://raw.githubusercontent.com/revery-ui/revery/master/assets/logo.png)

Here's a numbered list:

1. first item
Expand Down
25 changes: 23 additions & 2 deletions src/UI_Components/Markdown.re
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type style = {
codeBlockFontSize: float,
baseFontSize: float,
codeBlock: list(Style.viewStyleProps),
imageElement: (~url: string) => Revery_UI.element,
};

module SyntaxHighlight = {
Expand Down Expand Up @@ -143,7 +144,13 @@ type inlineAttrs =
| Bolded
| Monospaced;

type kind = [ | `Paragraph | `Heading(int) | `Link(string) | `InlineCode];
type kind = [
| `Paragraph
| `Heading(int)
| `Image(string)
| `Link(string)
| `InlineCode
];

let selectStyleFromKind = (kind: kind, styles) =>
switch (kind) {
Expand Down Expand Up @@ -223,6 +230,7 @@ let generateText = (text, styles, attrs, dispatch, state) => {
italic={isItalicized(attrs)}
/>
</View>
| `Image(url) => styles.imageElement(~url)
| _ =>
<Text
text
Expand Down Expand Up @@ -269,7 +277,12 @@ let rec generateInline' = (inline, styles, attrs, dispatch, state) => {
generateInline'(
l.def.label,
styles,
{...attrs, kind: `Link(l.def.destination)},
{
...attrs,
kind:
l.kind == Url
? `Link(l.def.destination) : `Image(l.def.destination),
},
dispatch,
state,
)
Expand Down Expand Up @@ -464,6 +477,12 @@ let generateMarkdown = (mdText: string, styles, highlighter, dispatch, state) =>
|> React.listToElement;
};

let defaultImageRenderer = (~url as _) => {
<View
style=Style.[width(64), height(64), backgroundColor(Colors.gray)]
/>;
};

let%component make =
(
~markdown as mdText="",
Expand All @@ -482,6 +501,7 @@ let%component make =
~h6Style=Style.emptyTextStyle,
~inlineCodeStyle=Style.emptyTextStyle,
~codeBlockStyle=Styles.Code.defaultBlock,
~imageElement=defaultImageRenderer,
~syntaxHighlighter=SyntaxHighlight.default,
(),
) => {
Expand All @@ -508,6 +528,7 @@ let%component make =
baseFontSize,
codeBlockFontSize,
codeBlock: codeBlockStyle,
imageElement,
},
syntaxHighlighter,
dispatch,
Expand Down
3 changes: 2 additions & 1 deletion src/UI_Components/Markdown.rei
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let make:
~h6Style: list(Revery_UI.Style.textStyleProps)=?,
~inlineCodeStyle: list(Revery_UI.Style.textStyleProps)=?,
~codeBlockStyle: list(Revery_UI.Style.viewStyleProps)=?,
~imageElement: (~url: string) => Revery_UI.element=?,
~syntaxHighlighter: SyntaxHighlight.t=?,
unit
) =>
Brisk_reconciler.element(Revery_UI.React.node);
Revery_UI.element;