From 9efbd7377a0e41c23fa5521672c0b7451ea51203 Mon Sep 17 00:00:00 2001 From: shawnsarwar Date: Fri, 23 Aug 2024 20:31:55 +0100 Subject: [PATCH] feat: support relative links in markdown for docs (#2375) Co-authored-by: Anoop M D --- package-lock.json | 100 ++++++++++++++++++ packages/bruno-app/package.json | 1 + .../CollectionSettings/Docs/index.js | 2 +- .../src/components/Documentation/index.js | 2 +- .../src/components/MarkDown/index.jsx | 11 +- 5 files changed, 112 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index f5dc9b3b3..35cf012ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12502,6 +12502,105 @@ "markdown-it": "bin/markdown-it.js" } }, + "node_modules/markdown-it-replace-link": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/markdown-it-replace-link/-/markdown-it-replace-link-1.2.1.tgz", + "integrity": "sha512-pRcJ1Gym1bwTqAJDB5n3JurF02aDRa5B+Jlvwg9zdBGza0zH7LJWxu2jD5mnTdt2LoSyuvqw+AgzTe5IVEWIYQ==", + "optionalDependencies": { + "dom-serializer": "^2.0.0", + "htmlparser2": "^8.0.1" + }, + "peerDependencies": { + "@types/markdown-it": "*", + "markdown-it": "*" + } + }, + "node_modules/markdown-it-replace-link/node_modules/dom-serializer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz", + "integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==", + "optional": true, + "dependencies": { + "domelementtype": "^2.3.0", + "domhandler": "^5.0.2", + "entities": "^4.2.0" + }, + "funding": { + "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" + } + }, + "node_modules/markdown-it-replace-link/node_modules/domelementtype": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz", + "integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + ], + "optional": true + }, + "node_modules/markdown-it-replace-link/node_modules/domhandler": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz", + "integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==", + "optional": true, + "dependencies": { + "domelementtype": "^2.3.0" + }, + "engines": { + "node": ">= 4" + }, + "funding": { + "url": "https://github.com/fb55/domhandler?sponsor=1" + } + }, + "node_modules/markdown-it-replace-link/node_modules/domutils": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-3.1.0.tgz", + "integrity": "sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==", + "optional": true, + "dependencies": { + "dom-serializer": "^2.0.0", + "domelementtype": "^2.3.0", + "domhandler": "^5.0.3" + }, + "funding": { + "url": "https://github.com/fb55/domutils?sponsor=1" + } + }, + "node_modules/markdown-it-replace-link/node_modules/entities": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz", + "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==", + "optional": true, + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/markdown-it-replace-link/node_modules/htmlparser2": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-8.0.2.tgz", + "integrity": "sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==", + "funding": [ + "https://github.com/fb55/htmlparser2?sponsor=1", + { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + ], + "optional": true, + "dependencies": { + "domelementtype": "^2.3.0", + "domhandler": "^5.0.3", + "domutils": "^3.0.1", + "entities": "^4.4.0" + } + }, "node_modules/markdown-it/node_modules/entities": { "version": "3.0.1", "license": "BSD-2-Clause", @@ -18266,6 +18365,7 @@ "know-your-http-well": "^0.5.0", "lodash": "^4.17.21", "markdown-it": "^13.0.2", + "markdown-it-replace-link": "^1.2.0", "mousetrap": "^1.6.5", "nanoid": "3.3.4", "next": "12.3.3", diff --git a/packages/bruno-app/package.json b/packages/bruno-app/package.json index 0a793779e..60596fd19 100644 --- a/packages/bruno-app/package.json +++ b/packages/bruno-app/package.json @@ -47,6 +47,7 @@ "know-your-http-well": "^0.5.0", "lodash": "^4.17.21", "markdown-it": "^13.0.2", + "markdown-it-replace-link": "^1.2.0", "mousetrap": "^1.6.5", "nanoid": "3.3.4", "next": "12.3.3", diff --git a/packages/bruno-app/src/components/CollectionSettings/Docs/index.js b/packages/bruno-app/src/components/CollectionSettings/Docs/index.js index d449d12d3..eb88926bf 100644 --- a/packages/bruno-app/src/components/CollectionSettings/Docs/index.js +++ b/packages/bruno-app/src/components/CollectionSettings/Docs/index.js @@ -48,7 +48,7 @@ const Docs = ({ collection }) => { font={get(preferences, 'font.codeFont', 'default')} /> ) : ( - + )} ); diff --git a/packages/bruno-app/src/components/Documentation/index.js b/packages/bruno-app/src/components/Documentation/index.js index 5a391db8a..9039ccea5 100644 --- a/packages/bruno-app/src/components/Documentation/index.js +++ b/packages/bruno-app/src/components/Documentation/index.js @@ -53,7 +53,7 @@ const Documentation = ({ item, collection }) => { mode="application/text" /> ) : ( - + )} ); diff --git a/packages/bruno-app/src/components/MarkDown/index.jsx b/packages/bruno-app/src/components/MarkDown/index.jsx index c3994e398..3c778c5a6 100644 --- a/packages/bruno-app/src/components/MarkDown/index.jsx +++ b/packages/bruno-app/src/components/MarkDown/index.jsx @@ -1,10 +1,15 @@ import MarkdownIt from 'markdown-it'; +import * as MarkdownItReplaceLink from 'markdown-it-replace-link'; import StyledWrapper from './StyledWrapper'; import React from 'react'; -const md = new MarkdownIt(); +const Markdown = ({ collectionPath, onDoubleClick, content }) => { + const markdownItOptions = { + replaceLink: function (link, env) { + return link.replace(/^\./, collectionPath); + } + }; -const Markdown = ({ onDoubleClick, content }) => { const handleOnClick = (event) => { const target = event.target; if (target.tagName === 'A') { @@ -23,6 +28,8 @@ const Markdown = ({ onDoubleClick, content }) => { } }; + const md = new MarkdownIt(markdownItOptions).use(MarkdownItReplaceLink); + const htmlFromMarkdown = md.render(content || ''); return (