Skip to content

Commit

Permalink
feat: support relative links in markdown for docs (usebruno#2375)
Browse files Browse the repository at this point in the history
Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
  • Loading branch information
2 people authored and Its-treason committed Aug 24, 2024
1 parent 21d12be commit 5a1191b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/bruno-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"markdown-it": "^14.1.0",
"monaco-editor": "^0.50.0",
"monaco-themes": "^0.4.4",
"markdown-it-replace-link": "^1.2.1",
"mousetrap": "^1.6.5",
"nanoid": "3.3.4",
"path": "^0.12.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const Docs = ({ collection }) => {
/>
)
) : (
<Markdown onDoubleClick={toggleViewMode} content={docs} />
<Markdown collectionPath={collection.pathname} onDoubleClick={toggleViewMode} content={docs} />
)}
</StyledWrapper>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/bruno-app/src/components/Documentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const Documentation = ({ item, collection }) => {
<CodeEditor value={docs || ''} onChange={onEdit} onSave={onSave} mode="markdown" height={'100%'} />
)
) : (
<Markdown onDoubleClick={toggleViewMode} content={docs} />
<Markdown collectionPath={collection.pathname} onDoubleClick={toggleViewMode} content={docs} />
)}
</StyledWrapper>
);
Expand Down
19 changes: 14 additions & 5 deletions packages/bruno-app/src/components/MarkDown/index.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import MarkdownIt from 'markdown-it';
import * as MarkdownItReplaceLink from 'markdown-it-replace-link';
import StyledWrapper from './StyledWrapper';
import React from 'react';
import { useMemo } from 'react';

const md = new MarkdownIt();
const Markdown = ({ collectionPath, onDoubleClick, content }) => {
htmlFromMarkdown = useMemo(() => {
const markdownItOptions = {
replaceLink: function (link) {
return link.replace(/^\./, collectionPath);
}
};

const md = new MarkdownIt(markdownItOptions).use(MarkdownItReplaceLink);

return md.render(content || '');
});

const Markdown = ({ onDoubleClick, content }) => {
const handleClick = (event) => {
if (event.target.href) {
event.preventDefault();
Expand All @@ -17,8 +28,6 @@ const Markdown = ({ onDoubleClick, content }) => {
}
};

const htmlFromMarkdown = md.render(content || '');

return (
<StyledWrapper>
<div className="markdown-body" dangerouslySetInnerHTML={{ __html: htmlFromMarkdown }} onClick={handleClick} />
Expand Down

0 comments on commit 5a1191b

Please sign in to comment.