From 965cec5abad224816e60b8eba6ebab3d3b2c8875 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 27 Jan 2023 18:37:04 -0300 Subject: [PATCH] make markdown more interoperable by disallowing the annoying link/image syntax. --- package.json | 1 + src/Element/Text.tsx | 26 ++++++++++++++++++++++++-- yarn.lock | 23 ++++++++++------------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 8fbdd4b2..918f9009 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "react-textarea-autosize": "^8.4.0", "react-twitter-embed": "^4.0.4", "typescript": "^4.9.4", + "unist-util-visit": "^4.1.2", "uuid": "^9.0.0", "workbox-background-sync": "^6.4.2", "workbox-broadcast-update": "^6.4.2", diff --git a/src/Element/Text.tsx b/src/Element/Text.tsx index dd8cf15b..8fae0e21 100644 --- a/src/Element/Text.tsx +++ b/src/Element/Text.tsx @@ -1,7 +1,8 @@ import './Text.css' -import { useMemo } from "react"; +import { useMemo, useCallback } from "react"; import { Link } from "react-router-dom"; import ReactMarkdown from "react-markdown"; +import { visit, SKIP } from "unist-util-visit"; import { TwitterTweetEmbed } from "react-twitter-embed"; import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex, TweetUrlRegex, HashtagRegex, TidalRegex, SoundCloudRegex, MixCloudRegex } from "Const"; @@ -216,5 +217,26 @@ export default function Text({ content, tags, users }: TextProps) { li: (x: any) => transformLi({ body: x.children ?? [], tags, users, pref }), }; }, [content]); - return {content} + const disableMarkdownLinks = useCallback(() => (tree: any) => { + visit(tree, (node, index, parent) => { + if ( + parent && + typeof index === 'number' && + (node.type === 'link' || + node.type === 'linkReference' || + node.type === 'image' || + node.type === 'imageReference' || + node.type === 'definition') + ) { + node.type = 'text'; + node.value = content.slice(node.position.start.offset, node.position.end.offset); + return SKIP; + } + }) + }, [content]); + return {content} } diff --git a/yarn.lock b/yarn.lock index 0ca094f0..b6ae8765 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6043,11 +6043,6 @@ js-sha3@0.8.0: resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840" integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q== -js-stylesheet@0.0.1: - version "0.0.1" - resolved "https://registry.yarnpkg.com/js-stylesheet/-/js-stylesheet-0.0.1.tgz#12cc1451220e454184b46de3b098c0d154762c38" - integrity sha512-jSPbDIaHlK8IFXEbE6MZkeAQshRHHxxOcQ+LCZejm3KpW+POpi2TE3GXWoFsOOG2+AhCTWHjD7AXRFt3FDm8Zw== - "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -7999,13 +7994,6 @@ react-markdown@^8.0.4: unist-util-visit "^4.0.0" vfile "^5.0.0" -react-menu@^0.0.10: - version "0.0.10" - resolved "https://registry.yarnpkg.com/react-menu/-/react-menu-0.0.10.tgz#c2b338cdd88e4c436325969239089e0752a142bc" - integrity sha512-SGl/OZljPUB1ITWBG2wt1p7hyE3Y449O9FUezxkKzu+JM5HWkPjrU/JRmG4ZguyegLAnZx3qyjhdFYqB44sqJw== - dependencies: - js-stylesheet "0.0.1" - react-query@^3.39.2: version "3.39.2" resolved "https://registry.yarnpkg.com/react-query/-/react-query-3.39.2.tgz#9224140f0296f01e9664b78ed6e4f69a0cc9216f" @@ -9354,6 +9342,15 @@ unist-util-visit@^4.0.0: unist-util-is "^5.0.0" unist-util-visit-parents "^5.1.1" +unist-util-visit@^4.1.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-4.1.2.tgz#125a42d1eb876283715a3cb5cceaa531828c72e2" + integrity sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg== + dependencies: + "@types/unist" "^2.0.0" + unist-util-is "^5.0.0" + unist-util-visit-parents "^5.1.1" + universalify@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.2.0.tgz#6451760566fa857534745ab1dde952d1b1761be0" @@ -10025,4 +10022,4 @@ yargs@^16.2.0: yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" - integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== \ No newline at end of file + integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==