lbry-desktop/src/ui/component/common/markdown-preview-internal.jsx

58 lines
1.3 KiB
React
Raw Normal View History

2019-04-03 07:56:58 +02:00
// @flow
import * as React from 'react';
import remark from 'remark';
2019-05-30 22:51:23 +02:00
import remarkLBRY from 'util/remark-lbry';
2019-04-03 07:56:58 +02:00
import remarkEmoji from 'remark-emoji';
2019-05-30 22:51:23 +02:00
import reactRenderer from 'remark-react';
2019-04-03 07:56:58 +02:00
import ExternalLink from 'component/externalLink';
import defaultSchema from 'hast-util-sanitize/lib/github.json';
type MarkdownProps = {
content: ?string,
promptLinks?: boolean,
};
type SimpleLinkProps = {
href?: string,
title?: string,
children?: React.Node,
};
const SimpleLink = (props: SimpleLinkProps) => {
const { href, title, children } = props;
return (
<a href={href} title={title}>
{children}
</a>
);
};
// Use github sanitation schema
const schema = { ...defaultSchema };
// Extend sanitation schema to support lbry protocol
2019-05-30 22:51:23 +02:00
schema.protocols.href.push('lbry');
2019-04-03 07:56:58 +02:00
const MarkdownPreview = (props: MarkdownProps) => {
const { content, promptLinks } = props;
const remarkOptions = {
sanitize: schema,
remarkReactComponents: {
a: promptLinks ? ExternalLink : SimpleLink,
},
};
return (
<div className="markdown-preview">
{
remark()
2019-05-30 22:51:23 +02:00
.use(remarkLBRY)
2019-04-03 07:56:58 +02:00
.use(remarkEmoji)
.use(reactRenderer, remarkOptions)
.processSync(content).contents
}
</div>
);
};
export default MarkdownPreview;