From 19c8e55d081658b59485a91823a0be90c3980467 Mon Sep 17 00:00:00 2001 From: btzr-io Date: Thu, 13 Jun 2019 01:18:30 -0600 Subject: [PATCH] simplify auto-embed syntax --- .../common/markdown-preview-internal.jsx | 4 +- src/ui/component/externalLink/view.jsx | 7 ++-- src/ui/util/remark-lbry.js | 38 ++++++++++++------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/ui/component/common/markdown-preview-internal.jsx b/src/ui/component/common/markdown-preview-internal.jsx index abee23ae0..2f52d1e41 100644 --- a/src/ui/component/common/markdown-preview-internal.jsx +++ b/src/ui/component/common/markdown-preview-internal.jsx @@ -43,7 +43,7 @@ const schema = { ...defaultSchema }; // Extend sanitation schema to support lbry protocol schema.protocols.href.push('lbry'); -schema.attributes.a.push('data-preview'); +schema.attributes.a.push('embed'); const MarkdownPreview = (props: MarkdownProps) => { const { content, strip, promptLinks } = props; @@ -61,7 +61,7 @@ const MarkdownPreview = (props: MarkdownProps) => { const remarkAttrOpts = { scope: 'extended', elements: ['link'], - extend: { link: ['data-preview'] }, + extend: { link: ['embed'] }, defaultValue: true, }; diff --git a/src/ui/component/externalLink/view.jsx b/src/ui/component/externalLink/view.jsx index fc8701c75..23d34f90d 100644 --- a/src/ui/component/externalLink/view.jsx +++ b/src/ui/component/externalLink/view.jsx @@ -9,19 +9,20 @@ import ClaimLink from 'component/claimLink'; type Props = { href: string, title?: string, + embed?: boolean, children: React.Node, openModal: (id: string, { uri: string }) => void, - 'data-preview'?: boolean, }; class ExternalLink extends React.PureComponent { static defaultProps = { href: null, title: null, + embed: false, }; createLink() { - const { href, title, children, openModal } = this.props; + const { href, title, embed, children, openModal } = this.props; // Regex for url protocol const protocolRegex = new RegExp('^(https?|lbry|mailto)+:', 'i'); const protocol = href ? protocolRegex.exec(href) : null; @@ -43,7 +44,7 @@ class ExternalLink extends React.PureComponent { // Return local link if protocol is lbry uri if (protocol && protocol[0] === 'lbry:' && isURIValid(href)) { element = ( - + {children} ); diff --git a/src/ui/util/remark-lbry.js b/src/ui/util/remark-lbry.js index 2dc2de596..1cdf4b965 100644 --- a/src/ui/util/remark-lbry.js +++ b/src/ui/util/remark-lbry.js @@ -6,12 +6,12 @@ const locateURI = (value, fromIndex) => value.indexOf(protocol, fromIndex); const locateMention = (value, fromIndex) => value.indexOf('@', fromIndex); // Generate a valid markdown link -const createURI = (text, uri, autoEmbed = false) => ({ +const createURI = (text, uri, embed = false) => ({ type: 'link', url: (uri.startsWith(protocol) ? '' : protocol) + uri, data: { // Custom attribute - hProperties: { 'data-preview': autoEmbed }, + hProperties: { embed }, }, children: [{ type: 'text', value: text }], }); @@ -21,12 +21,17 @@ const validateURI = (match, eat) => { try { const text = match[0]; const uri = parseURI(text); - // Create channel link - if (uri.isChannel && !uri.path) { - return eat(text)(createURI(uri.claimName, text, false)); + const isValid = uri && uri.claimName; + const isChannel = uri.isChannel && !uri.path; + + if (isValid) { + // Create channel link + if (isChannel) { + return eat(text)(createURI(uri.claimName, text, false)); + } + // Create claim link + return eat(text)(createURI(text, text, true)); } - // Create uri link - return eat(text)(createURI(text, text, true)); } catch (err) { // Silent errors: console.error(err) } @@ -60,13 +65,20 @@ tokenizeMention.notInBlock = true; const visitor = (node, index, parent) => { if (node.type === 'link' && parent && parent.type === 'paragraph') { try { - const url = parseURI(node.url); - // Handle lbry link - if (!url.isChannel || (url.isChannel && url.path)) { - // Auto-embed lbry url - if (!node.data) { + const uri = parseURI(node.url); + const isValid = uri && uri.claimName; + const isChannel = uri.isChannel && !uri.path; + if (isValid && !isChannel) { + if (!node.data || !node.data.hProperties) { + // Create new node data node.data = { - hProperties: { 'data-preview': true }, + hProperties: { embed: true }, + }; + } else if (node.data.hProperties) { + // Don't overwrite current attributes + node.data.hProperties = { + embed: true, + ...node.data.hProperties, }; } }