Skip to content

Commit d00c828

Browse files
authored
feat: image support in Markdown text (leanprover-community#359)
1 parent 8daae2f commit d00c828

File tree

11 files changed

+65
-19
lines changed

11 files changed

+65
-19
lines changed

client/src/components/hints.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GameHint, InteractiveGoalsWithHints, ProofState } from "./infoview/rpc_api";
22
import * as React from 'react';
3-
import Markdown from './markdown';
3+
import { Markdown } from './markdown';
44
import { DeletedChatContext, ProofContext } from "./infoview/context";
55
import { lastStepHasErrors } from "./infoview/goals";
66
import { Button } from "./button";

client/src/components/infoview/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { GameIdContext } from '../../app';
2222
import { useAppDispatch, useAppSelector } from '../../hooks';
2323
import { LevelInfo, useGetGameInfoQuery } from '../../state/api';
2424
import { changedInventory, levelCompleted, selectCode, selectCompleted, selectInventory } from '../../state/progress';
25-
import Markdown from '../markdown';
25+
import { Markdown } from '../markdown';
2626

2727
import { Infos } from './infos';
2828
import { AllMessages, Errors, WithLspDiagnosticsContext } from './messages';

client/src/components/inventory.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
55
import { faLock, faBan, faCheck } from '@fortawesome/free-solid-svg-icons'
66
import { faClipboard } from '@fortawesome/free-regular-svg-icons'
77
import { GameIdContext } from '../app';
8-
import Markdown from './markdown';
8+
import { Markdown } from './markdown';
99
import { useLoadDocQuery, InventoryTile, LevelInfo, InventoryOverview, useLoadInventoryOverviewQuery } from '../state/api';
1010
import { selectDifficulty, selectInventory } from '../state/progress';
1111
import { store } from '../state/store';

client/src/components/landing_page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import '@fontsource/roboto/700.css';
1010
import '../css/landing_page.css'
1111
import bgImage from '../assets/bg.jpg'
1212

13-
import Markdown from './markdown';
13+
import { Markdown } from './markdown';
1414
import {PrivacyPolicyPopup} from './popup/privacy_policy'
1515
import { GameTile, useGetGameInfoQuery } from '../state/api'
1616
import path from 'path';

client/src/components/level.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { changedSelection, codeEdited, selectCode, selectSelections, selectCompl
2525
selectHelp, selectDifficulty, selectInventory, selectTypewriterMode, changeTypewriterMode } from '../state/progress'
2626
import { store } from '../state/store'
2727
import { Button } from './button'
28-
import Markdown from './markdown'
28+
import { Markdown } from './markdown'
2929
import {InventoryPanel} from './inventory'
3030
import { hasInteractiveErrors } from './infoview/typewriter'
3131
import { DeletedChatContext, InputModeContext, PreferencesContext, MonacoEditorContext,

client/src/components/markdown.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import * as React from 'react';
2+
import { useContext } from 'react';
23
import ReactMarkdown from 'react-markdown';
34
import remarkMath from 'remark-math'
45
import rehypeKatex from 'rehype-katex'
56
import 'katex/dist/katex.min.css' // `rehype-katex` does not import the CSS for you
67
import gfm from "remark-gfm";
8+
import { GameIdContext } from '../app';
79

8-
function Markdown(props) {
9-
const newProps = {
10-
...props,
11-
remarkPlugins: [...props.remarkPlugins ?? [], remarkMath, gfm],
12-
rehypePlugins: [...props.remarkPlugins ?? [], rehypeKatex],
13-
};
14-
return (
15-
<ReactMarkdown {...newProps} className="markdown" />
16-
);
17-
}
10+
export function Markdown(props: any) {
11+
const gameId = useContext(GameIdContext)
12+
// Prefix image URLs of the form `![...](images/...)` with the game ID
13+
let children = props?.children
14+
15+
if (typeof children === "string") {
16+
children = children.replace(
17+
/!\[([^\]]+)\]\((images\/[^\)]+)\)/g,
18+
(match, text, url) => `![${text}](data/${gameId}/${url})`
19+
);
20+
}
1821

19-
export default Markdown
22+
const newProps = {
23+
...props,
24+
children,
25+
remarkPlugins: [...props.remarkPlugins ?? [], remarkMath, gfm],
26+
rehypePlugins: [...props.remarkPlugins ?? [], rehypeKatex],
27+
};
28+
return (
29+
<ReactMarkdown {...newProps} className="markdown" />
30+
);
31+
}

client/src/components/message.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '@fontsource/roboto/500.css';
55
import '@fontsource/roboto/700.css';
66

77
import { Button, Dialog, DialogContent, DialogContentText, DialogActions } from '@mui/material';
8-
import Markdown from './markdown';
8+
import { Markdown } from './markdown';
99

1010
function Message({ isOpen, content, close }) {
1111

client/src/components/popup/game_info.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import * as React from 'react'
55
import { Typography } from '@mui/material'
6-
import Markdown from '../markdown'
6+
import { Markdown } from '../markdown'
77
import { useTranslation } from 'react-i18next'
88
import { GameIdContext } from '../../app'
99

client/src/components/popup/preferences.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import { Input, MenuItem, Select, SelectChangeEvent, Typography } from '@mui/material'
3-
import Markdown from '../markdown'
3+
import { Markdown } from '../markdown'
44
import { Switch, Button, ButtonGroup } from '@mui/material';
55
import Box from '@mui/material/Box';
66
import Slider from '@mui/material/Slider';

doc/create_game.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ in one of the files you created in 2), 3), or 4) (i.e. game/world/level).
282282

283283
NOTE: At present, only the images for a world are displayed. They appear in the introduction of the world.
284284

285+
You can also embed images in the text as descriped in [Markdown](markdown.md).
286+
285287
## 7. Update your game
286288

287289
In principle, it is as simple as modifying `lean-toolchain` to update your game to a new Lean version. However, you should read about the details in [Update An Existing Game](update_game.md).
@@ -311,6 +313,15 @@ CoverImage "images/cover.png"
311313

312314
## 10. Advanced Topics
313315

316+
### Markdown
317+
318+
Texts should generally support markdown. For some tips on how to use markdown, see
319+
[Markdown styling](markdown.md).
320+
321+
In particular, you can embed images in texts, see the specific instructions in that file.
322+
323+
324+
314325
### Escaping
315326

316327
Inside strings, you need to escape backslashes, but not inside doc-strings, therefore you

0 commit comments

Comments
 (0)