2020-04-29 22:50:06 +02:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
2020-09-17 17:32:31 +02:00
|
|
|
import classnames from 'classnames';
|
2020-04-29 22:50:06 +02:00
|
|
|
import remark from 'remark';
|
|
|
|
import remarkAttr from 'remark-attr';
|
|
|
|
import remarkStrip from 'strip-markdown';
|
|
|
|
import remarkEmoji from 'remark-emoji';
|
2020-05-10 06:36:33 +02:00
|
|
|
import remarkBreaks from 'remark-breaks';
|
2020-12-17 23:44:16 +01:00
|
|
|
import remarkFrontMatter from 'remark-frontmatter';
|
2020-04-29 22:50:06 +02:00
|
|
|
import reactRenderer from 'remark-react';
|
2020-10-20 19:10:02 +02:00
|
|
|
import MarkdownLink from 'component/markdownLink';
|
2020-04-29 22:50:06 +02:00
|
|
|
import defaultSchema from 'hast-util-sanitize/lib/github.json';
|
|
|
|
import { formatedLinks, inlineLinks } from 'util/remark-lbry';
|
2020-12-15 08:15:02 +01:00
|
|
|
import { formattedTimestamp, inlineTimestamp } from 'util/remark-timestamp';
|
2021-01-27 04:24:14 +01:00
|
|
|
import ZoomableImage from 'component/zoomableImage';
|
2021-03-09 10:33:42 +01:00
|
|
|
import { CHANNEL_STAKED_LEVEL_VIDEO_COMMENTS } from 'config';
|
2019-11-07 20:39:22 +01:00
|
|
|
|
2020-04-29 22:50:06 +02:00
|
|
|
type SimpleTextProps = {
|
|
|
|
children?: React.Node,
|
|
|
|
};
|
|
|
|
|
|
|
|
type SimpleLinkProps = {
|
|
|
|
href?: string,
|
|
|
|
title?: string,
|
|
|
|
children?: React.Node,
|
|
|
|
};
|
|
|
|
|
|
|
|
type MarkdownProps = {
|
|
|
|
strip?: boolean,
|
|
|
|
content: ?string,
|
2020-10-22 20:16:42 +02:00
|
|
|
simpleLinks?: boolean,
|
2020-09-29 05:46:18 +02:00
|
|
|
noDataStore?: boolean,
|
2020-09-17 17:32:31 +02:00
|
|
|
className?: string,
|
2020-10-20 19:10:02 +02:00
|
|
|
parentCommentId?: string,
|
|
|
|
isMarkdownPost?: boolean,
|
2021-03-09 10:33:42 +01:00
|
|
|
stakedLevel?: number,
|
2020-04-29 22:50:06 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 04:24:14 +01:00
|
|
|
// ****************************************************************************
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2020-04-29 22:50:06 +02:00
|
|
|
const SimpleText = (props: SimpleTextProps) => {
|
|
|
|
return <span>{props.children}</span>;
|
|
|
|
};
|
|
|
|
|
2021-01-27 04:24:14 +01:00
|
|
|
// ****************************************************************************
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2020-04-29 22:50:06 +02:00
|
|
|
const SimpleLink = (props: SimpleLinkProps) => {
|
2020-10-20 19:10:02 +02:00
|
|
|
const { title, children, href } = props;
|
2020-04-29 22:50:06 +02:00
|
|
|
|
|
|
|
if (!href) {
|
|
|
|
return children || null;
|
|
|
|
}
|
|
|
|
|
2020-10-20 19:10:02 +02:00
|
|
|
if (!href.startsWith('lbry:/')) {
|
2020-04-29 22:50:06 +02:00
|
|
|
return (
|
2020-05-01 09:29:10 +02:00
|
|
|
<a href={href} title={title} target={'_blank'} rel={'noreferrer noopener'}>
|
2020-04-29 22:50:06 +02:00
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [uri, search] = href.split('?');
|
|
|
|
const urlParams = new URLSearchParams(search);
|
|
|
|
const embed = urlParams.get('embed');
|
|
|
|
|
|
|
|
if (embed) {
|
2020-07-30 23:20:18 +02:00
|
|
|
// Decode this since users might just copy it from the url bar
|
|
|
|
const decodedUri = decodeURI(uri);
|
2020-10-20 19:10:02 +02:00
|
|
|
return (
|
2020-09-15 18:19:54 +02:00
|
|
|
<div className="embed__inline-button-preview">
|
|
|
|
<pre>{decodedUri}</pre>
|
|
|
|
</div>
|
|
|
|
);
|
2020-04-29 22:50:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 19:10:02 +02:00
|
|
|
// Dummy link (no 'href')
|
|
|
|
return <a title={title}>{children}</a>;
|
2020-04-29 22:50:06 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 04:24:14 +01:00
|
|
|
// ****************************************************************************
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2020-04-29 22:50:06 +02:00
|
|
|
// Use github sanitation schema
|
|
|
|
const schema = { ...defaultSchema };
|
|
|
|
|
|
|
|
// Extend sanitation schema to support lbry protocol
|
|
|
|
schema.protocols.href.push('lbry');
|
|
|
|
schema.attributes.a.push('embed');
|
|
|
|
|
2020-04-29 23:37:14 +02:00
|
|
|
const REPLACE_REGEX = /(<iframe\s+src=["'])(.*?(?=))(["']\s*><\/iframe>)/g;
|
2020-04-29 22:50:06 +02:00
|
|
|
|
2021-01-27 04:24:14 +01:00
|
|
|
// ****************************************************************************
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2020-04-29 22:50:06 +02:00
|
|
|
const MarkdownPreview = (props: MarkdownProps) => {
|
2021-03-09 10:33:42 +01:00
|
|
|
const { content, strip, simpleLinks, noDataStore, className, parentCommentId, isMarkdownPost, stakedLevel } = props;
|
2020-04-29 22:50:06 +02:00
|
|
|
const strippedContent = content
|
2020-04-29 23:37:14 +02:00
|
|
|
? content.replace(REPLACE_REGEX, (iframeHtml, y, iframeSrc) => {
|
|
|
|
// Let the browser try to create an iframe to see if the markup is valid
|
|
|
|
const outer = document.createElement('div');
|
|
|
|
outer.innerHTML = iframeHtml;
|
|
|
|
const iframe = ((outer.querySelector('iframe'): any): ?HTMLIFrameElement);
|
|
|
|
|
|
|
|
if (iframe) {
|
|
|
|
const src = iframe.src;
|
|
|
|
|
|
|
|
if (src && src.startsWith('lbry://')) {
|
2020-10-20 19:10:02 +02:00
|
|
|
return src;
|
2020-04-29 23:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return iframeHtml;
|
2020-04-29 22:50:06 +02:00
|
|
|
})
|
|
|
|
: '';
|
|
|
|
|
|
|
|
const remarkOptions: Object = {
|
|
|
|
sanitize: schema,
|
|
|
|
fragment: React.Fragment,
|
|
|
|
remarkReactComponents: {
|
2020-10-20 19:10:02 +02:00
|
|
|
a: noDataStore
|
|
|
|
? SimpleLink
|
2021-03-09 10:33:42 +01:00
|
|
|
: (linkProps) => (
|
2020-10-20 19:10:02 +02:00
|
|
|
<MarkdownLink
|
|
|
|
{...linkProps}
|
|
|
|
parentCommentId={parentCommentId}
|
|
|
|
isMarkdownPost={isMarkdownPost}
|
2020-10-22 20:16:42 +02:00
|
|
|
simpleLinks={simpleLinks}
|
2021-03-09 10:33:42 +01:00
|
|
|
allowPreview={stakedLevel && stakedLevel >= CHANNEL_STAKED_LEVEL_VIDEO_COMMENTS}
|
2020-10-20 19:10:02 +02:00
|
|
|
/>
|
|
|
|
),
|
2020-04-29 22:50:06 +02:00
|
|
|
// Workaraund of remarkOptions.Fragment
|
|
|
|
div: React.Fragment,
|
2021-01-27 04:24:14 +01:00
|
|
|
img: ZoomableImage,
|
2020-04-29 22:50:06 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const remarkAttrOpts = {
|
|
|
|
scope: 'extended',
|
2020-12-18 01:37:58 +01:00
|
|
|
elements: ['link'],
|
2020-04-29 22:50:06 +02:00
|
|
|
extend: { link: ['embed'] },
|
|
|
|
defaultValue: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Strip all content and just render text
|
|
|
|
if (strip) {
|
|
|
|
// Remove new lines and extra space
|
|
|
|
remarkOptions.remarkReactComponents.p = SimpleText;
|
|
|
|
return (
|
2020-08-20 13:27:28 +02:00
|
|
|
<span dir="auto" className="markdown-preview">
|
2020-04-29 22:50:06 +02:00
|
|
|
{
|
|
|
|
remark()
|
|
|
|
.use(remarkStrip)
|
2020-12-17 23:44:16 +01:00
|
|
|
.use(remarkFrontMatter, ['yaml'])
|
2020-04-29 22:50:06 +02:00
|
|
|
.use(reactRenderer, remarkOptions)
|
|
|
|
.processSync(content).contents
|
|
|
|
}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-09-17 17:32:31 +02:00
|
|
|
<div dir="auto" className={classnames('markdown-preview', className)}>
|
2020-04-29 22:50:06 +02:00
|
|
|
{
|
|
|
|
remark()
|
|
|
|
.use(remarkAttr, remarkAttrOpts)
|
|
|
|
// Remark plugins for lbry urls
|
|
|
|
// Note: The order is important
|
|
|
|
.use(formatedLinks)
|
|
|
|
.use(inlineLinks)
|
2020-12-15 08:15:02 +01:00
|
|
|
.use(isMarkdownPost ? null : inlineTimestamp)
|
|
|
|
.use(isMarkdownPost ? null : formattedTimestamp)
|
2020-04-29 22:50:06 +02:00
|
|
|
// Emojis
|
|
|
|
.use(remarkEmoji)
|
2020-05-10 06:36:33 +02:00
|
|
|
// Render new lines without needing spaces.
|
|
|
|
.use(remarkBreaks)
|
2020-12-17 23:44:16 +01:00
|
|
|
.use(remarkFrontMatter, ['yaml'])
|
2020-04-29 22:50:06 +02:00
|
|
|
.use(reactRenderer, remarkOptions)
|
|
|
|
.processSync(strippedContent).contents
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
2019-11-07 20:39:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MarkdownPreview;
|