From d91530b0c85e3f2c82e9ec57bdb10b92a334226c Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Sat, 13 Mar 2021 22:00:39 +0800 Subject: [PATCH] Markdown: don't convert '[label](uri)' to embeds. ## Issue Closes 4936: Don't process markdown formatting as lbry:// url previews ## Approach Preamble: - We want to convert plain `https://lbry.tv/befreeonlbry` to an embed, but not `[blah](https://lbry.tv/befreeonlbry)`. - At the markdown/remark level, both formats resolve to the same node type, having a `link` and a `text`, with the 'text' being auto-filled with the `href` if there is no label. Fix by assuming the link is the non-labelled format if the `text` is the same as `href`. This opens up one corner-case that we can't handle, which is when the user explicitly set the label using the href, e.g. `[https://lbry.tv/befreeonlbry](https://lbry.tv/befreeonlbry)`. This will still resolve to an embed. There's not enough data at the parsed level differentiate this case -- we would need to parse the content ourself before `remark`, which I think is not worth it. ## Aside/Reminder If you see that the link doesn't resolve to an embed regardless of the format used, that's probably just due to `5636: Disable video previews in comments/posts made by channels below a certain channel staked level` --- ui/component/markdownLink/view.jsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ui/component/markdownLink/view.jsx b/ui/component/markdownLink/view.jsx index 83e730171..8267c823c 100644 --- a/ui/component/markdownLink/view.jsx +++ b/ui/component/markdownLink/view.jsx @@ -67,7 +67,10 @@ function MarkdownLink(props: Props) { const possibleLbryUrl = linkPathPlusHash ? `lbry://${linkPathPlusHash.replace(/:/g, '#')}` : undefined; const lbryLinkIsValid = possibleLbryUrl && isURIValid(possibleLbryUrl); - if (lbryLinkIsValid) { + const isMarkdownLinkWithLabel = + children && Array.isArray(children) && React.Children.count(children) === 1 && children.toString() !== href; + + if (lbryLinkIsValid && !isMarkdownLinkWithLabel) { lbryUrlFromLink = possibleLbryUrl; } }